only execute script if user comes from a specific page<

I have this link count script. I guess you know what I mean. Every time a user clicks a link on my website, a certain value in a mysql database is added by 1.

The user clicks a link:
count.php?url=http://www.test.com/mp3.mp3&ID=18

This is the count script:

<?php
session_start();
$rootdir="../";
include $rootdir . 'layout/functions.php';
$ID = $_GET['ID'];
$url = $_GET['url'];
db();
$sql2="UPDATE music SET clicks=clicks+1 WHERE (ID = '$ID')";
$result=mysql_query($sql2);
header("Location: $url");
exit();
?>

Now my problem.
I don't want users to be able to enter the url in their browser (<!-- m --><a class="postlink" href="http://mysite.com/count.php?url=http://www.test.com/mp3.mp3&ID=18">http://mysite.com/count.php?url=http:// ... .mp3&ID=18</a><!-- m -->) so the numer in my mysql database is growing.

How can I make the counter script only to be executed when the user actually clicked the link on my site? So no one can cheat?you can check for the referer and see if they came from the page with the link, but not all browsers send the referer.

that is your only choice that I am aware of.couldn't you use a session variable that is only set in the source page, and is destroyed in the target?yeah I suppose you can do that. and only if that variable is set.You can check for a referer like scoutt said, this would do exactly what you want. However, for added security you can log the IP in the database and then check to see if it exists. If it doesnt the user never clicked on the link and it is logged, if it does that means the user clicked on the link before and it is not logged.
 
Back
Top