Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP setcookie problem.

Options
  • 20-05-2006 7:12pm
    #1
    Registered Users Posts: 689 ✭✭✭


    Hi

    I'm trying to save a variable onto the users machine using PHP and cookies.

    When I use something like below...

    $visitcount=$_cookie["visits"];
    if ($visitcount == "") $visitcount=0;
    else $visitcount++;

    setcookie("visits",$visitcount)
    echo "Visit number $visitcount"

    This works in that each time the page is refreshed the visitcount goes up.... the problem is that if I then open the page in another browser window the visitcount starts at the beginning again and I have two seperate variables in each browser window...

    How can I save variable information to the users computer in such a way as it will be available to all browser windows?

    Cheers
    Joe


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    When you don't specify a expiry time for a cookie, it's known as a session cookie. This means that the cookie is only valid for as long as that browser window is open. Once you open a new browser window, it's a new session and therefore a new cookie.

    Technically there's no way to make a cookie "permanent". Though most places keep cookies alive for a year or two. Just call
    setcookie("visits", $visitcount, mktime() + (60*60*24*365))
    and it will set a cookie that sits on the machine for a year (or until they clear their cookie cache).


  • Registered Users Posts: 689 ✭✭✭JoeB-


    thank you seamus, problem solved.


Advertisement