ChompGator Posted March 18, 2008 Share Posted March 18, 2008 Hey, Below I have put my script up, what it does is, show users their IP Address, and the date, and what visitor number they are. What Im requesting help with is, what would I add to this script that is say you visit the site, and click on 'overview' and say your visitor 54, when you click that 'overview link' your now visitor '55' if their IP address is the same, I dont want the visitor number to keep going up, so if anyone could add to my script to help me out, or give me any tips to help me out that would be great! Thanks <?php $host="***"; // Host name $username="**"; // Mysql username $password="**"; // Mysql password $db_name="**"; // Database name $tbl_name="counter"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); $counter=$rows['counter']; // if have no counter value set counter = 1 if(empty($counter)){ $counter=1; $sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')"; $result1=mysql_query($sql1); } echo "You are visitor number: $counter <br>Your IP Address: "; echo $_SERVER['REMOTE_ADDR'],"<br>\n"; echo date("m/d/y"); // count more value $addcounter=$counter+1; $sql2="update $tbl_name set counter='$addcounter'"; $result2=mysql_query($sql2); mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Share Posted March 18, 2008 Well, to prevent the user from being counted again, you'll probably want to set a cookie so that the user is not counted if that cookie exists. Try this: <?php $host="***"; // Host name $username="**"; // Mysql username $password="**"; // Mysql password $db_name="**"; // Database name $tbl_name="counter"; // Table name // Connect to server and select database. mysql_connect($host, $username, $password)or die("cannot connect to server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); $counter=$rows['counter']; // if have no counter value set counter = 1 if(empty($counter)){ $counter=1; $sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')"; $result1=mysql_query($sql1); } $addcounter=$counter+1; // If the cookie 'site_counter' does not exist, or is null; Set a new cookie and add 1 to the counter in the database. if(!isset($_COOKIE['site_counter']) || $_COOKIE['site_counter'] == null) { setcookie("site_counter", $addcounter, time()+5400); // set the cookie to expire in one and a half hours (change this to whatever you want) // count more value $sql2="update $tbl_name set counter='$addcounter'"; $result2=mysql_query($sql2); echo "You are visitor number: ".$addcounter." <br>Your IP Address: "; echo $_SERVER['REMOTE_ADDR'],"<br>\n"; echo date("m/d/y"); } // If the cookie exists, there is no need to create a cookie OR add a visit to the counter. Show the user their number. else { echo "You are visitor number: ".$_COOKIE['site_counter']." <br>Your IP Address: "; echo $_SERVER['REMOTE_ADDR'],"<br>\n"; echo date("m/d/y"); } mysql_close(); ?> Quote Link to comment 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.