Page 1 of 1

Cookie code not working!

Posted: Fri Mar 12, 2010 9:19 am
by andrew_hodgson
Can someone tell me why this won't work? I can't understand why it won't install a cookie on my browser! I've tried Safari, Firefox and Opera and no luck on any of them!

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cookie</title>
</head>
 
<body>
 
<?php 
 
    setcookie("test", 45, time()+(3600));
    echo "This page has loaded correctly! But is thy cookie there!";
    
?>
<br />
<a href="cookie_read.php">Check that cookie!</a>
</body>
</html>
 
The link is to a page which has been coded to show the value of the cookie but it doesn't display any value! When I check the cookies stored on my browser it isn't there either!

Any ideas as to why it's all going wrong!!!!!

Thanks guys!


Andy

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 9:24 am
by Grizzzzzzzzzz
i assume your code on the 2nd page is something like:

Code: Select all

 
<?php
 
    echo $_COOKIE["test"];
 
?>
 


Make sure you have cookies enabled in your browser, and on your server.

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 9:30 am
by andrew_hodgson
yeah test page looks a little like that,

Cookies are enabled on my browser, how do I check on my web server? I'm just running Apache as a local host just now as I'mm still learning it all.


Andy

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 9:40 am
by timWebUK
Sounds like your server isn't configured to display your PHP errors as your code needs to control it's output before you modify headers.

This will work:

Code: Select all

<?php
    ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cookie</title>
</head>
 
<body>
 
<?php
    setcookie("test", 45, time()+(3600));
    echo "This page has loaded correctly! But is thy cookie there!";  
?>
<br />
<a href="cookie_read.php">Check that cookie!</a>
</body>
</html>
<?php
    ob_flush();
?>

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 9:41 am
by Grizzzzzzzzzz
make another php file containing

Code: Select all

 
<?php
 
echo phpinfo();
 
?>
 
view it and it'll tell you your php configuration on your server

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 10:49 am
by andrew_hodgson
hmmmm, there are A LOT of settings in here, which one am I after?

Under Apache environment I've got this setting;
HTTP_COOKIE: SQLiteManager_currentLangue=2

This under PHP Variables:
_SERVER["HTTP_COOKIE"] SQLiteManager_currentLangue=2

Under HTTP Headers Information:
Cookie: SQLiteManager_currentLangue=2

No idea if this is what I need to be looking at.

If these are wrong can someone help me change them, not sure how to do that either!

Thanks!


Andy

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 10:52 am
by andrew_hodgson
oh and all of these under session;
Local Value Master Value
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 11:53 am
by AbraCadaver
Pay attention to what timWebUK has said:

Code: Select all

<?php
setcookie("test", 45, time()+(3600));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cookie</title>
</head>
 
<body>
 
<?php
echo "This page has loaded correctly! But is thy cookie there!";
?>
<br />
<a href="cookie_read.php">Check that cookie!</a>
</body>
</html>

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 12:37 pm
by andrew_hodgson
ah I see! Got it working now.

So I take it that the setcookie function must be declared PRE dtd?

Is this a relatively new thing? My tutorial videos have declared it in the body tag. There not exactly brand new but there not old either!

Cheers guys!


That's a massive help!

Re: Cookie code not working!

Posted: Fri Mar 12, 2010 12:54 pm
by AbraCadaver
andrew_hodgson wrote:ah I see! Got it working now.

So I take it that the setcookie function must be declared PRE dtd?

Is this a relatively new thing? My tutorial videos have declared it in the body tag. There not exactly brand new but there not old either!

Cheers guys!


That's a massive help!
Once you output anything, HTML, a blank space, a blank line, whatever, headers are sent. After that you can't send any headers. This applies to header(), session_start(), setcookie(), etc... It's not new. I don't know why a tutorial would do this, unless a) it sucks or b) it assumes that you start output buffering automatically in php.ini.

If you do this at the top of your page you would see it:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');