SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    cookie per browser

    PHP Code:
    setcookie("myCookie"1time()+1000 
    myCookie is alive for 1000 seconds with the code above.


    If I open the page with a browser , example, chrome,
    The cookie will be alive for 1000 seconds.

    If I reflesh or move to another page with the browser within 1000 seconds,
    the cookie is still alive.

    If I open the page with a new chrome within 1000 seconds,
    the cookie is still alive.

    I like to make it like the following.
    If I reflesh or move to another page with the browser within 1000 seconds,
    the cookie is still alive.
    BUT,
    If I open the page with a new chrome within 1000 seconds,
    the cookie is NOT alive because it is not the same browser.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't, and I really have no idea why you'd even want to do that. It is the same browser, regardless if you open a new tab or you detach the tab in a separate window. It's the same parent process running, it's the same configuration used, it is the same browser.
    Adapt your logic, not the way browser works.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If both browsers are chrome then they ARE the same browser and so what you are trying to do cannot apply. The only way to force multiple copies of the browser to actually run rather than the same copy just opening multiple windows would be to run the browser inside a virtual machine where it cannot see if there is another copy running outside of that VM.

    A simpler solution would be to actually use a different browser (eg Safari) and then since they are in fact different browsers they will not have access to the same session cookies.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by furicane View Post
    It is the same browser, regardless if you open a new tab or you detach the tab in a separate window.
    Okay I see, Thank you.

    Now I like to ask one more thing relating this issue.

    If I reflesh or move to another page with the browser within 1000 seconds,
    the cookie is still alive.

    If I open the page with a new chrome within 1000 seconds,
    the cookie is still alive.

    If I shut down my computer and reboot it and open a new chrome browser
    within 1000 seconds,
    the cookie is still alive.


    I like to make it like the following.

    If I reflesh or move to another page with the browser within 1000 seconds,
    the cookie is still alive.
    If I open the page with a new chrome within 1000 seconds,
    the cookie is still alive.
    BUT,
    If I shut down my computer and reboot it and open a new chrome browser
    within 1000 seconds,
    the cookie is NOT alive.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dotJoon View Post
    If I shut down my computer and reboot it and open a new chrome browser
    within 1000 seconds,
    the cookie is NOT alive.
    You have a choice.

    You can either have the cookie last 1000 seconds for that browser regardless of how many times the browser is opened and closed during that time

    OR

    You can have the cookie last for as long as the browser stays open and have it deleted when the browser is closed.

    To get the second of these simply remove the expiry time from the command writiing the cookie. It will then be retained in the browser itself rather than being saved and so will disappear as soon as the browser is closed.

    You could then save the time in the cookie data itself and compare the current time to the time in the cookie to see if the 1000 seconds has passed instead of simply checking if the cookie exists.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    You could then save the time in the cookie data itself and compare the current time to the time in the cookie to see if the 1000 seconds has passed instead of simply checking if the cookie exists.
    I feel that your suggestion looks very cool.

    Quote Originally Posted by felgall View Post
    simply remove the expiry time from the command writiing the cookie.
    As your say, the code below which is removed the expiry time works fine.
    Code:
    setcookie("myCookie", 1)
    I used the code below before I simply remove the expiry time. the red part below is for working the cookie with sub-domains.
    Code:
    setcookie("myCookie", 1 ,time()+100, "/", ".dot.kr");
    Now I simply remove the expiry time like the code below, but it doesn't work saying Warning: setcookie() expects parameter 3 to be long, string given in \cookie11-2.php on line 2
    Code:
    setcookie("myCookie", 1 , "/", ".dot.kr");
    How can I remove the warning and make it to work?

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You check www.php.net/setcookie to check which parameters are expected, then you see that you passed completely wrong parameters to your setcookie() and then you fix it.
    Documentation is available, and it's not gibberish so it's easy to understand even to novices.

    PHP Code:
    setcookie("myCookie""your value"0"/"".dot.kr"); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •