+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP setcookie() does not set cookie !

  1. #1
    yemeth is offline x10Hosting Member yemeth is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    PHP setcookie() does not set cookie !

    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 :

    PHP Code:
    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. #2
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: PHP setcookie() does not set cookie !

    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.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  3. #3
    yemeth is offline x10Hosting Member yemeth is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    Re: PHP setcookie() does not set cookie !

    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 :
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    	<?php setcookie("userSession", "true", time()+3600) or die('unable to create cookie');  ?>
    </head>
    <body>
    </body>
    </html>
    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. #4
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: PHP setcookie() does not set cookie !

    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 Code:
    <?php setcookie("userSession""true"time()+3600) or die('unable to create cookie');  ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
       <title>Testing PHP setcookie()</title>
    </head>
    <body>
       <h1>Testing PHP setcookie()</h1>
    </body>
    </html>
    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).
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  5. #5
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Re: PHP setcookie() does not set cookie !

    PHP Code:
    $expire time()+3600// 1 hour expiry
    $value "cookie value - string or interger";
    setcookie("cookie_name"$value$expire,"/""your_domain.com"0); 
    Give the above code a try, and replace your_domain.com 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 - http://php.net/manual/en/function.setcookie.php

    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


    Domain: The domain that the cookie is available to. To make the cookie available on all subdomains of example.com (including example.com itself) then you'd set it to '.example.com'. Although some browsers will accept cookies without the initial ., » RFC 2109 requires it to be included. Setting the domain to 'www.example.com' or '.www.example.com' 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"]).
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  6. #6
    yemeth is offline x10Hosting Member yemeth is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    Re: PHP setcookie() does not set cookie !

    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. #7
    midataz189 is offline x10Hosting Member midataz189 is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    11

    Re: PHP setcookie() does not set cookie !

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

  8. #8
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: PHP setcookie() does not set cookie !

    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?
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. Who ate the last cookie?
    By cybrax in forum Off Topic
    Replies: 4
    Last Post: 03-14-2011, 12:47 AM
  2. JFusion + SMF Cookie Help!
    By h4nbury in forum Free Hosting
    Replies: 0
    Last Post: 07-21-2009, 06:08 AM
  3. PHP setcookie problem...
    By Scoochi2 in forum Programming Help
    Replies: 8
    Last Post: 09-03-2008, 02:23 PM
  4. Cookie problems
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 05-07-2008, 02:20 PM
  5. Cookie Troubles
    By chr0n1x in forum Free Hosting
    Replies: 3
    Last Post: 10-21-2005, 02:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers