madhead29 Posted March 19, 2008 Share Posted March 19, 2008 Hi, People. I am currently using a script that counts the clicks from outside users. For example a user clicks a link (lets say click.php?=1) which loads checks there ip address (which ip addresses in the database) if its not in the database add there ip address to the database and add a +1 to the link count for that link. But say you have a different link say click.php?id=2 but this one goes to a totally different website if the same user clicks that link the script checks if the ip is in the database but because the user clicked it before using click.php?=1 it doesnt add a +1 for click.php?=2 i was wondering how i can make it so if someone clicks click.php?id=1 it will add a +1 for that link and also if they click.php?id=2 i will add a +1 for that link aswell but only if they not clicked each link before. Can some please help me with this? The current code i am using is below: <?php $host = 'l'; $user = ''; $pass = '; $db = ''; mysql_connect($host,$user,$pass) or die (mysql_error()); // try to connect to database mysql_select_db($db); // try to select the database with the link details $id = stripslashes($_GET['id']); if(!$id) exit; // if the link was not specified, stop runing $id = stripslashes($id); // for security reasons $linkquery = mysql_query("SELECT * FROM links WHERE id=$id LIMIT 1;"); // look for the specified link $link = mysql_fetch_array($linkquery); if(!$link) die("The specified ID does not exists!"); // if the ID exists in the database, start the count process $day = date("d"); $month = date("m"); $year = date("y"); $date = "$day/$month/$year"; $ip = $_SERVER["REMOTE_ADDR"]; $query = mysql_query("SELECT * FROM links_info WHERE ip='$ip';"); $lookvisitorsip = mysql_fetch_array($query); if(!$lookvisitorsip) // if the visitor's ip isn't already in the database add one more click mysql_query("UPDATE links SET clicks=(clicks+1) WHERE id=$id;"); // add 1 click to the specified id // if you just want to count clicks, without get members info delete this next line mysql_query("INSERT INTO links_info (id,ip,date) VALUES ($id,'$ip','$date');") or die(mysql_error()); // save the area which your visitor went, their IP and the date when they visit it header("Location: $link[ref]"); // redirect to the page// //echo "Link:".$link[ref]; ?> Also can some please let me know if this code is secure? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2008 Share Posted March 19, 2008 I think you should take a different approach to determining "which" links have been clicked - because you will need to keep a record of each uniqe link. In fact you don't need to keep a counter variable since you can easily get the count of "clicks" based upon the number of records. I'm not going to go through the code above and rewite it all, btu I will provide some mock code: First I would have a table with the following fields: user_clicks - ipaddress - link_id Then when a user clicks a link I would simply take the ID off of the query string and test if it is in the table for the current user's IP. If no, add a new record, if yes do nothing. <?php $query = "SELECT * FROM user_clicks where ip_address='$userIIP' AND link_id='$linkID'"; $result = mysql_query($query) or die (mysql_error()); if (mysql_num_rows($result)==0) { $query = "INSERT INTO user_clicks (ip_address, link_id) VALUES('$userIIP','$linkID')"; $result = mysql_query($query) or die (mysql_error()); } ?> You could then get the number of clicks for an IP address with the following: <?php $query = "SELECT COUNT(*) FROM user_clicks where ip_address='$userIIP' GROUP BY ip_address"; $result = mysql_query($query) or die (mysql_error()); $record = mysql_fetch_array($result); $count = $record[0]; echo "The user has click $count unique links"; ?> Quote Link to comment Share on other sites More sharing options...
madhead29 Posted March 19, 2008 Author Share Posted March 19, 2008 Hi, Thanks for the code i will take a look at it. 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.