Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: cookies

  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts

    cookies

    in the script below i'm setting a cookie and then trying to print the cookies but I don't see the cookie i set

    what am I doing wrong

    PHP Code:

    <? 
    setcookie
    ('email',"testtest",time() + (86400 365));



    print_r($_COOKIE);



    ?>

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,306
    Thanks
    4
    Thanked 205 Times in 202 Posts
    The cookie will not be available until another page is loaded (or the same page is reloaded).
    Dave .... HostMonster for all of your hosting needs

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    refreshing didn't help -it's not keeping the cookie - is there anything else i'm doing wrong here?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If that's exactly what you have, you have a whitespace before the <?. Cookies are sent via header requests, so if you have output already its impossible to send the headers.
    Open your error reporting:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    If it complains of headers already sent on the setcookie call, that'll be your cause.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    that doesn't help

    it is the first line

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by esthera View Post
    that doesn't help

    it is the first line
    This response doesn't really clarify anything since you don't mention whether the first line is the <? or if you are referring to setcookie being the first instruction.
    Let me show you what I'm talking about:
    Code:
    1. 
    2. <? 
    3. setcookie('email',"testtest",time() + (86400 * 365));
    Your code as it is shows a whitespace on line 1. That is output. Its already been mentioned as well that $_COOKIE does not get populated until the next request is performed.

    One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.

    Finally, the cookie itself doesn't specify a path nor domain. This code all assumes that you are within the same directory. It is also assumed that you are directly attaching to this script and not including it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,415
    Thanks
    269
    Thanked 32 Times in 31 Posts
    Quote Originally Posted by Fou-Lu View Post
    One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.
    Very nice explanation on the whole cookie deal Fou-Lu, but in particular on the short tags which is one reason why every time i see a short tag i replace it with a full tag. The only time i use anything like a short tag is when im displaying something like
    Code:
    <?=$value;?>
    other than that its all full tags. For some reason i have a problem or i just dont like php echo ???? i even try to stay away from php print, i dont know why im a fussy guss i guess lol.

    Awhile back i had a tough time with cookie as well, fought it for days till Fou-Lou helped me out, same issue. In that case i pulled the value of the cookie to use on another value when the site loaded.

    What i had to do was when the site first loaded i had to hard code a value for the var. Then after that it was fine, because the page was reloaded by the visitor after that and the cookie was set. But at first load of the site if you need that cookie value it is not there yet. Thats just the way it is.

    You might have to use a session value for first load and then the cookie after that.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,358
    Thanks
    61
    Thanked 529 Times in 516 Posts
    Quote Originally Posted by Fou-Lu View Post
    This response doesn't really clarify anything since you don't mention whether the first line is the <? or if you are referring to setcookie being the first instruction.
    Let me show you what I'm talking about:
    Code:
    1. 
    2. <? 
    3. setcookie('email',"testtest",time() + (86400 * 365));
    Your code as it is shows a whitespace on line 1. That is output. Its already been mentioned as well that $_COOKIE does not get populated until the next request is performed.

    One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.

    Finally, the cookie itself doesn't specify a path nor domain. This code all assumes that you are within the same directory. It is also assumed that you are directly attaching to this script and not including it.
    Just to add to Fous cookie explanation..

    If you don't understand what request and reply headers are, how cookies are sent and recieved etc, I have a post here which will explain this:
    http://www.codingforums.com/showpost...64&postcount=7

    Read that, then you will start to understand what Fou is getting at. As you will see, you can set a cookie but until the client sends a new http request, the cookie will not be sent back to the server. If it isn't sent to the server then the $_COOKIE array will not contain your cookie. The $_COOKIE array only contains cookies that have been sent from the client to your server and script - not cookies that you set with setcookie().

    Whenever you output *anything* it causes the reply headers to be sent. If you have whitespace as Fou has shown, that will also send the reply headers. Once those headers have been sent, you can not send a cookie or any other header. This is because the headers can only be sent once - otherwise you would have mixed html and headers and the browser would not know what is what. There is another explanation of this in my signature - see the link about headers already sent.
    Last edited by tangoforce; 02-15-2013 at 12:20 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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