How to count impressions on referral link? [duplicate]
Possible Duplicate:
How to count clicks on a $_GET value with php?
Hello. I have a script which generates every new user a referral link which they can use to invite thier friends to my site. (http://blabla.com/?ref=121212). The actual script works in this manner,
http://blabla.com/?ref={$_SESSION['slogin']}
I'm wonde开发者_开发技巧ring how to count the amount of clicks their link gets so i can display it on the page. Ex.
Your link: http://blabla.com/?ref=121212 Clicks: 12
Anyone know how this can be done?
You should be use a stored value like as user id, not session id. And somone visit the link you should be update the number of visits for user id from link.
You should save the hit in a database.
Something like:
if (isset($_GET['ref'])) {
mysql_query("INSERT INTO hit ('-ref-','data')");
}
Then you can show the number of hits by doing:
mysql_query("SELECT COUNT(*) FROM hit WHERE ref = '*REF*'");
This should be pretty simple.
You just need a table with a row for each referer id and count column.
Then when someone hits the page you increment the count in the respective row.
You can log each hit to your database table, incrementing the existing value:
UPDATE hits SET counter=counter+1 WHERE slogin=$ref;
Be sure to properly clean the input before storing the value in $ref, and consider using prepared statements for added security.
on a high traffic site, you will want to parse your http access log and and update periodically on the backend rather then increment on every pageload.
精彩评论