Cookie code not working!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andrew_hodgson
Forum Newbie
Posts: 9
Joined: Fri Mar 12, 2010 9:09 am

Cookie code not working!

Post 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
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Cookie code not working!

Post 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.
andrew_hodgson
Forum Newbie
Posts: 9
Joined: Fri Mar 12, 2010 9:09 am

Re: Cookie code not working!

Post 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
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Cookie code not working!

Post 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();
?>
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Cookie code not working!

Post 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
andrew_hodgson
Forum Newbie
Posts: 9
Joined: Fri Mar 12, 2010 9:09 am

Re: Cookie code not working!

Post 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
andrew_hodgson
Forum Newbie
Posts: 9
Joined: Fri Mar 12, 2010 9:09 am

Re: Cookie code not working!

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cookie code not working!

Post 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>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
andrew_hodgson
Forum Newbie
Posts: 9
Joined: Fri Mar 12, 2010 9:09 am

Re: Cookie code not working!

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cookie code not working!

Post 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');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply