Link Counter

June 28, 2009, Posted by admin at 6:00 am

Simple Text Counter of clicks on links ver. counts.

First they must deal with eg phpmyadmin a table with the following hierarchy:

mysql> describe counter;
+——-+——————+——+—–+——— +—————-+
| Field | Type | Null | Key | Default | Extra |
+——-+——————+——+—–+——— +—————-+
| Id | int (10) unsigned | | PRI | 0 | auto_increment |
| Url | char (255) | | | | |
| Count | int (11) | | | 0 | |
+——-+——————+——+—–+——— +—————-+
3 rows in set (0.00 sec)
The url field contains the URL that is called. The field count contains the number of clicks on url.
<? php
/ / Access data for the database
/ / This should be the safety’s sake
/ / In a directory outside of the
/ / Document Root are outsourced.
$ host = “localhost”;
$ user = “user”;
$ pass = “demo_password”;
$ datab = “demo_db”;
$ table = “counter”;
/ / The passed via GET URL database to read and make
$ url = addslashes ($ _GET [ 'url']);
/ / Connect to MySQL server
$ db = @ mysql_connect ($ host, $ user, $ pass);
if ($ db) (
if (@ mysql_select_db ($ datab, $ db)) (
/ / Entry for the specified URL by 1 increase.
$ query = “UPDATE $ table SET count = count + 1 WHERE url = ‘$ url’;
@ Mysql_query ($ query);
/ / Still no entry for the URL available?
if (mysql_affected_rows () == 0) (
$ sql_insert = “INSERT INTO $ table (url, count) VALUES ( ‘$ url’,'1 ‘)”;
@ Mysql_query ($ sql_insert);
)
)
)
/ / The passed URL forwared
header ( “Location:”. $ _GET [ 'url']);
?>
Example:
<a href=”count.php?url=http://www.webmaster-eye.de”> link </ a>

As parameter for the file count.php is the URL to which should be forwarded. In count.php is now the record in the table, the $ url as a value for the url field contains increased by 1 and it will be transferred to the new URL.

No comment yet.

Leave a Reply