Sign in to follow this  
ezacharyk

[web] Registering clicks on a link

Recommended Posts

I have an image link on one site that redirects to another web site on a seperate server. I need to figure out how to store the number of times people have clicked on the link. Does anyone have any ideas?

Share this post


Link to post
Share on other sites
Sure. Just have the link send the user to a php page that stores the hit in a database, then foward the user to the correct page from there. You'd have to know PHP and MySql (though, very little of it), but... its do-able.

The example would look like this.


This is the page with the link:


.....
<a href = "store_link.php?value=www.thispage.com><img src.....></a>




then, the store_link.php file would look something LIKE this (PS THIS IS A VERY CRUDE VERSION OF WHAT STORE_LINK.PHP SHOULD LOOK LIKE).



<?php

global $HTTP_GET_VARS;
require_once('all_my_php_functions.php');

$Link = $HTTP_GET_VARS['value'];
$Conn = db_connect();

mysql_query("Update DBTABLE set Hits = Hits + 1 where Link = '$Link'");

?>

<html>
<head>
<...insert whatever the code is here to foward the user ='<?php echo $Link; ?>' >

</head>
</html>




I hope that helps.

Share this post


Link to post
Share on other sites
Please do not just include outside data into a query string. This opens up an SQL injection attack. At the very least you'll want something like


$Conn = db_connect();
$Link = mysql_real_escape_string($HTTP_GET_VARS['value'], $Conn);

mysql_query("Update DBTABLE set Hits = Hits + 1 where Link = '$Link'");

Share this post


Link to post
Share on other sites
Oh and please use $_GET rather than $HTTP_GET_VARS, the latter is now officially deprecated, and there are a number of reasons why as well.

And the only thing you need to do to forward a user to another location is use the header() function, no HTML necessary.

Share this post


Link to post
Share on other sites
Hmm.. you're right. By definition, it is depricated. However, if you're running a version of php that is pre-4.1... then you can't user $_GET. But then agian, may God help you if you're host is running a pre-4.1 version of PHP.

Share this post


Link to post
Share on other sites
Quote:
Original post by Verminox
Quote:
Original post by Cygnus_X
But then agian, may God help you if you're host is running a pre-4.1 version of PHP.


Amen.

If these hosts still exist, there is no god.

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