PHP cookie problem.

Discussion in 'HTML & Website Design' started by steven.menzies.5, Nov 19, 2012.

  1. #1
    Hello im new here so sorry if this is the wrong place to post this. Anyway I have a webpage that sets a cookie on a users computer then that activates a hit counter and counts up 1. The problem is it doesn't ever do the code that it's supposed to do if the cookies already set. It just keeps running the else statemement. here is the code that I have.

    
     <?php setcookie("user", "", time()+3600);
    
    if (isset($_COOKIE["user"]))
      echo "Welcome Back!";
    else
     $filename="counter.txt";$fd=fopen($filename,"r")or die("Cant open $filename");$fstring=fread($fd,filesize($filename));
    fclose($fd);echo "We have had ","$fstring", " visitors" ;$fd = fopen ($filename , "w") or die ("can't open $filename") ; $fcounted = $fstring + 1 ; $fout = fwrite ($fd , $fcounted) ;fclose($fd) ;?> 
    PHP:
    So basically it just keeps counting up everytime the user visits the page. Is there something that im missing??
    Thanks in advance!
     
    steven.menzies.5, Nov 19, 2012 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Why are you storing this in a cookie which the user can quite easily decline rather than storing the users IP and hit count in a database? If you are storing a cookie on the users' computer, they will be using the same IP right?

    I trust you have already read this:

    Source: http://www.ico.gov.uk/for_organisations/privacy_and_electronic_communications/the_guide/cookies.aspx
     
    scottlpool2003, Nov 23, 2012 IP