Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP setcookie problem.

  • 20-05-2006 07:12PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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, Registered Users 2 Posts: 689 ✭✭✭JoeB-


    thank you seamus, problem solved.


Advertisement