Jump to content


Photo

Cookies - detect if a user has clicked a link today


  • Please log in to reply
5 replies to this topic

#1 MereCatfish

MereCatfish
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 April 2006 - 06:27 AM

Hi,

Just wondering if I could get some help with a problem.

I run a webring, which counts the amount of times a link is clicked. The user clicks it, the db value has 1 added to it. Then the user is redirected to the URL they wanted. Obviously, this allows people to continually refresh to get to the top.

So I decided to try to use cookies to see if a user has clicked the link today. If they have, the click will not be recorded. I cannot see why the clicks continue to be recorded. Here is my code:

<?php

# Connect to MySQL server
$conn = mysql_connect($host,$dbusername,$dbpassword)
   or die(mysql_error());

# Select the database
mysql_select_db("$database", $conn)
   or die(mysql_error());

$array = mysql_fetch_array(mysql_query("SELECT url, clicks FROM sites WHERE id='$id'"));

    if(isset($_COOKIE['$array[url]'])){

        header("Location: $array[url]");    
        exit;

    } else {

        $clicks = $array[clicks] + 1;
        mysql_query("UPDATE sites SET clicks='$clicks' WHERE id='$id'");
        setcookie("$array[url]", "halocewebring", time()+3600*24);

        header("Location: $array[url]");

    }

?>

Thanks for your time.

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 18 April 2006 - 09:08 AM

if(isset($_COOKIE['$array[url]'])){
needs to be
if(isset($_COOKIE[$array['url']])){
or
if(isset($_COOKIE["$array[url]"])){

using ' will print the variable how u wrote it so it is checking for a cookie called $array[url]


Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 MereCatfish

MereCatfish
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 April 2006 - 01:31 PM

[!--quoteo(post=365927:date=Apr 18 2006, 09:08 AM:name=shocker-z)--][div class=\'quotetop\']QUOTE(shocker-z @ Apr 18 2006, 09:08 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
if(isset($_COOKIE['$array[url]'])){
needs to be
if(isset($_COOKIE[$array['url']])){
or
if(isset($_COOKIE["$array[url]"])){

using ' will print the variable how u wrote it so it is checking for a cookie called $array[url]
Regards
Liam
[/quote]

Still didn't make a difference :(.

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 18 April 2006 - 02:24 PM

$array = mysql_fetch_array(mysql_query("SELECT url, clicks FROM sites WHERE id='$id'"));
try
$query=mysql_query("SELECT url, clicks FROM sites WHERE id='$id'") or die(mysql_error());
$array = mysql_fetch_array($query);


just want to check the statement is runnign properly...
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 MereCatfish

MereCatfish
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 April 2006 - 04:04 PM

Yeah, that didn't make a difference either :|

I am absolutely stumped, I can't see why it isn't working...

#6 MereCatfish

MereCatfish
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 April 2006 - 05:47 AM

Alright, it is definitely having a problem finding the cookie. I can see the cookie set on my computer, but replacing the if code with...

    if(isset($_COOKIE["$array[url]"])){

        echo("you have da cookie lolz");    
        exit;

    } else {

redirects to the site, which means header("Location: $array[url]"); would happen, which is in the else.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users