PHP setcookie() does not set cookie !

yemeth

New Member
Messages
11
Reaction score
0
Points
0
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:
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 ?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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.
 

yemeth

New Member
Messages
11
Reaction score
0
Points
0
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:
<!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 ?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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:
<?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).
 

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
PHP:
$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"]).
 

yemeth

New Member
Messages
11
Reaction score
0
Points
0
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 !
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
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?
 
Top