forumnz Posted September 27, 2007 Share Posted September 27, 2007 How would I go about having a hit counter for a particular referral? Lets say I have a banner ad and I want to find out the amount of clicks it's getting. I want it to be something like mysite.com/ref.php?abc and then redirect to index.php. How would that work? Sam. Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/ Share on other sites More sharing options...
davidmuir Posted September 27, 2007 Share Posted September 27, 2007 Would you prefer to store the number of referrals in a text file or mySQL database? Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356757 Share on other sites More sharing options...
forumnz Posted September 27, 2007 Author Share Posted September 27, 2007 Database I guess. Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356758 Share on other sites More sharing options...
davidmuir Posted September 27, 2007 Share Posted September 27, 2007 This is simple then. I am sure that you could find a tutorial on this somewhere. All you would need to do is have one table with one value in it that each time the page is called you would select the value, increment it, then update it. Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356778 Share on other sites More sharing options...
dingus Posted September 27, 2007 Share Posted September 27, 2007 i would have 2 tables in a database ______________________________ | Site | Hits | ---------------------------------- then as you suggested have something like ref.php?site=abcd in ref just have a short script something like this $site = $_GET["site"]; mysql_query("UPDATE ref SET Hits = Hits+1 WHERE Site="$site; "); header('Location: ' . $site); its a simple in place update hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356827 Share on other sites More sharing options...
d22552000 Posted September 27, 2007 Share Posted September 27, 2007 A nice file alternative is: FOR PHP 5: $site = $_GET["site"]; $hits = file_get_contents('.\\hits\\'.$site); if (empty($hits)) {$hits=0;} $hits += 1; file_put_contents('.\\hits\\'.$site,$hits); header('Location: ' . $site); exit(); FOR PHP 4: $site = $_GET["site"]; $hit = fopen('.\\hits\\'.$site,'r'); $hits = fread($hit,64); fclose($hit); if (empty($hits)) {$hits=0;} $hits += 1; fopen('.\\hits\\'.$site,'w'); fwrite($hits); fclose(); header('Location: ' . $site); exit(); Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356830 Share on other sites More sharing options...
cooldude832 Posted September 27, 2007 Share Posted September 27, 2007 also you should set a session so you don't get refreshing the page getting false records Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356838 Share on other sites More sharing options...
d22552000 Posted September 27, 2007 Share Posted September 27, 2007 also you should set a session so you don't get refreshing the page getting false records if (!@session_id()) { session_start(); //CODE FOR COUNTER HERE } else { header('Location: '.$site); } Quote Link to comment https://forums.phpfreaks.com/topic/70961-hit-counter/#findComment-356842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.