Jump to content
xisto Community
Sign in to follow this  
FaLgoR

One More Tut 4 U :) Link counter

Recommended Posts

Well, in this tut, I'll show you how to do an simple link counter, with only one count for each IP and the date when the link were clicked. Just remember, this is only one way to do it.

First, create an mysql database called 'links', with 2 table:
first table will be called 'links', with the columns: id, title, ref and clicks. The other one will be 3 columns and will be called links_info: id, ip and date. Just remember that the columns 'id' of this second table IS NOT auto-increment.
Here is the code of the file which will count the clicks, link.php:

<?$host = 'localhost';$user = 'root';$pass = '';$db = 'links';mysql_connect($host,$user,$pass) or die (mysql_error()); // try to connect to databasemysql_select_db($db); // try to select the database with the link detailsif(!$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 clickmysql_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 linemysql_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 itheader("Location: $link[ref]"); // redirect to the page?>

Well, the page is done!! Now, you must enter all your links there, with the title, ref (the url to the file) and blah blah blah blah :D
Your links will now be just like this: <a href="links.php?id=1">Link txt</a>
Change id=1 for the id of the link inserted into the database. Now, let's make an page which shows the 10 more visited links, top10.php:

<?$host = 'localhost';$user = 'root';$pass = '';$db = 'links';mysql_connect($host,$user,$pass) or die (mysql_error()); // try to connect to databasemysql_select_db($db); // try to select the database with the link details?>More visited:<br><?$lquery = mysql_query("SELECT * FROM links LIMIT 10;"); // you can change this LIMIT 10 for any other limit u want$x = 1;while($link = mysql_fetch_array($lquery)){?><?=$x?>°: <a href="link.php?id=<?=$link[id]?>"><?=$link[title]?></a> Cliks: <?=$link[clicks]?><br><?$x++; // increase $x}?>

This page is not so beatiful, but you can change the HTML when you want, but don't change php lines, until you know what you are doing.
That's all, folks! I've tested this script and I know he is working fine. If, for any reason this script doesn't work in your page, just post here and I'll help you :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.