To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-06-2003, 03:17 PM   #1
Manat
Senior Member
 
Join Date: Sep 2002
Posts: 399
affiliate counter

Hello all,

We will soon be having a banner on another site linking to ours.
I gave them an AFID=3243 query string.

I have a little counter that counts the number of users coming to our site with that affiliate ID.

Please critique my code and let me know if you would do anything differently.

Thanks.

<?php
$result = mysql_query ("SELECT * FROM counter");
//$rows = mysql_numrows($result);

for ($count = 1; $row = mysql_fetch_row ($result); ++$count)
{}
$now = getdate();
$month = $now[mon];
$mday = $now[mday];
$year = $now[year];
$hours = $now[hours];
$min = $now[minutes];
$sec = $now[seconds];
$id = $count++;
$browser = "$HTTP_USER_AGENT";
$referer = "$HTTP_REFERER";
$time = "$year-$month-$mday $hours:$min:$sec";
for ($i=0; $i < strlen($id); $i++)
{
$sign = substr($id, $i, 1);

}

if ($referer != "$PHP_SELF")
{
if ($afid == 3243)
{
$counter = "INSERT INTO counter (ID, browser, referer, time, afid) VALUES ('$id','$browser','$referer','$time','$afid')";
$result = mysql_query($counter);
}

}
?>

I put the if referer not equal to self so that refreshes don't count as an extra visit.

Let me know if this a good solution to affiliate tracking/counting.

Thanks
MK
Manat is offline   Reply With Quote
Old 10-06-2003, 05:08 PM   #2
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,124
First of all, I'd post the code in [php] tags and indent it so that it's easier to read. (See, I told ya!)

Second, I'd quote the associative array keys.

Third, I'd use SELECT COUNT(*) and return only the information I want from the table (the number of rows it contains), instead of the entire table.

Fourth, personally, I'd use date() instead of getdate()

Fifth, I'd be more likely to use $_SERVER['HTTP_USER_AGENT'] etc., instead of $HTTP_USER_AGENT.

Sixth, wrapping a single string variable in double quotes (e.g., "$HTTP_USER_AGENT") just wastes time: it creates a new string, searches it for variables, interpolates those variables with their string values, and returns the resulting string .... which in this case happens to be exactly the same as what the variable's string value was in the first place.

Seventh, since $sign is never used, the loop around it is a do-nothing. Maybe it's used elsewhere, but it's going to be the last character in the $id anyway, so the loop could be replaced with $sign=substr($id,-1);.

Eighth, if the id is an incrementing counter for the purposes of uniquely identifying rows, then MySQL (and most other DBMSs) have mechanisms for specifying such fields automatically (MySQL has AUTOINCREMENT, PostgreSQL has SERIAL, MS SQL Server has IDENTITY...)

Ninth, if the fields are numeric, then they oughtn't be quoted; if they're all strings then that will cause problems when ordering (because '10'<'2').

Tenth; what's $afid? Where does that come from? Oh, the querystring. So therefore it ought to be $_GET['afid'].
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 07:56 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.