PHP setcookie() does not set cookie !

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by yemeth, Oct 30, 2011.

  1. yemeth

    yemeth New Member

    Hello all,
    I am sorry if there is already a thread on this issue but I didn't find anything...

    I would like to use a simple cookie to check the user session validity on each page of my website. So I tried to set the cookie as following :

    setcookie("userSession""true"time()+3600) or die('unable to create cookie');
    On my local server the cookie is created as expected, but this same code on the boru server return "unable to create cookie".
    So I guess I am doing something wrong :) maybe something to configure ?
  2. essellar

    essellar Community Advocate Community Support

    Are you sure that the setcookie() call is not preceded by any whitespace or a Unidode BOM (byte order mark -- a non-printable character that will occupy the first character position of a Unicode text file unless you deliberately create the file without the BOM)? Remember, setcookie() affects the header, so it can't happen after the headers have been sent.
  3. yemeth

    yemeth New Member

    Thank you for your answer. However...
    I tried what you said. I even tried to create a very simple index that should create a cookie :
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    	<?php setcookie("userSession", "true", time()+3600) or die('unable to create cookie');  ?>
    On my local server the cookie is well created but on the boru server, it continues to return the error message.

    Perhaps there's an error in my script, but then how would it work on my local server ?
  4. essellar

    essellar Community Advocate Community Support

    You can't put any HTML before the setcookie() call at all. This will work (although it doesn't do anything except set a cookie -- there is nothing like a real "session" involved):

    <?php setcookie("userSession""true"time()+3600) or die('unable to create cookie');  ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
       <title>Testing PHP setcookie()</title>
       <h1>Testing PHP setcookie()</h1>
    Notice that the call to setcookie() happens before anything else except the opening <?php tag. And notice as well that the opening <?php tag is the very first thing in the file -- there are no spaces or blank lines before it. That is the way things have to be if you are setting any HTTP header values (like cookies).
  5. cybrax

    cybrax Community Advocate Community Support

    $expire time()+3600// 1 hour expiry
    $value "cookie value - string or interger";

    Give the above code a try, and replace with your own websites name. Now that you are working on a shared server you have to tell it what domain the cookie is being set for.

    Read more here about PHP cookies -

    Path: The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

    Domain: The domain that the cookie is available to. To make the cookie available on all subdomains of (including itself) then you'd set it to ''. Although some browsers will accept cookies without the initial ., ยป RFC 2109 requires it to be included. Setting the domain to '' or '' will make the cookie only available in the www subdomain.

    Secure: Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).
  6. yemeth

    yemeth New Member

    My mystake !
    That works well now. I initially thought that setcookie() must be put between the <head> and </head> balises...
    Actually I decided to use cookies because I failed to use the start_session(). Now I guess it is the same problem and that start_session must be written before any HTML code. So I am gonna try back to use the php sessions.
    Thanks a lot !
  7. midataz189

    midataz189 New Member

    I get same probleme one day !! Had u a solution for this ?
  8. essellar

    essellar Community Advocate Community Support

    The answer to the original problem has already been given in this thread (in both my answer and cybrax's). Please read them again.

    If there is something you don't understand, can you make your question more specific?

Share This Page