SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: PHP cookies

  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP cookies

    Hi! I'm trying to create a PHP script but I don't get it working properly. Here is what I want it to do:

    1. Check if the cookie exists

    1.1 If it does, then do nothing - display the page as it normally would
    1.2 If it doesn't, then:

    1.2.1 Set a cookie that will auto-delete when visitor leaves the page
    1.2.2 Call in a script that does redirect a visitor to the same page

    Here's the code I created but it keeps redirecting no matter what:

    Code:
    <?php
    
    if(isset($HTTP_cookie_VARS['redirected'])){
    }else{
    setcookie("redirected", "", time()-3600);
    require_once("redirect.php");
    }
    
    ?>
    This code IS the first thing that will be loaded and executed. What am I doing wrong?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,033
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    First of all $HTTP_COOKIE_VARS is deprecated, use $_COOKIE instead.

    Second of all, look at the documentation for setcookie(), especially the notes on the $expire parameter
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I made a correction, checked the setcookie() $expire parameter, but discovered nothing incorrect in last one. So, now my code looks like this and still same problem - it just keeps redirecting like there is no cookie at all (even if I set the time() to +300 or something so it wouldn't self-delete).

    Code:
    <?php
    
    if (isset($_COOKIE['redirected'])) {
    }else{
    setcookie("redirected", "", time()-3600);
    require_once("redirect.php");
    }
    
    ?>

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check also the path on the server in which your cookie will be available on, it is included on the documentation about setcookie() on the link posted above. remember that the default value is the current directory that the cookie is being set in.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,033
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by kalaloom View Post
    Set a cookie that will auto-delete when visitor leaves the page
    Quote Originally Posted by Da Manual
    If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
    So there's that

    Another thing is that if you don't specify a value for a cookie it will actually tell the browser to delete the cookie. Why there is no mention of this in Da Manual is beyond me. I'm pretty sure it used to be there, but now it's not anymore?!

    Anyhow, the code you want to use is
    PHP Code:
    setcookie('redirected'true0'/'); 
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, RNEL, but it didn't help. I set all parameters available but the problem remains.

    // OMG! ScallioXTX, Thanks a lot! You just made my day right now! It works. Thanks.


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
  •