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!
Having trouble with the header() aswell. This code from Pro PHP4 book makes the browser time out. Yes cookies work in my browser and ive tried IE and Opera.
<?php
// check for cookies with the algorithm:
// 1 – Send a cookie
// 2 – Reload the page
// 3 – Check to see if the cookie exists
// 4 – Perform desired action based on the results
if (!$cookie) {
// Send a redirect header to the page proving that
// we’ve attempted to set the cookie.
header("Location: $PHP_SELF?cookie=1");
// Set a test cookie.
setcookie("test", "1");
} else {
// Test to see if the cookie hasn’t been set
if (!$test) {
// The cookie doesn’t exist
echo ("Please enable cookies in your browser.");
} else {
// The cookie exists, send them to a page with cookie support.
header("Location: http://yourserver.com/next.php");
}
}
?>
The problem with headers is that it has to be sent before anyother code.
Php Manual wrote:Note: The HTTP status header line will always be the first sent to the client, regardless of the actual header() call beeing the first or not. The status may be overridden by calling header() with a new status line at any time unless the HTTP headers have already been sent.
Top of my head, if you don't include a time part to the cookie it will only exist for the current browser session, i.e. close the browser lose the cookie.
and you setcookie("test","1"); will never get executed because the header part before it calls the same page with ?cookie=1
Thanks for the replies but I am not really interested in the wisdom of the code. This code is just an example from the book professsional php4 by wrox press. I want to know why the cookies are not working on my install of php.
I am trying to make a members database using cookies which is difficult without being able to set cookies. Please dont tell me how to do it without cookies.
rats wrote:Thanks for the replies but I am not really interested in the wisdom of the code. This code is just an example from the book professsional php4 by wrox press. I want to know why the cookies are not working on my install of php.
I am trying to make a members database using cookies which is difficult without being able to set cookies. Please dont tell me how to do it without cookies.
Hi rats,
Maybe you never really read any of the posts but,
The setcookie function is not getting called at all, ever, because the header function was being called first. Now without fixing that your cookie setting will never work, and also not including an expiry time the cookie will expire when the browser is closed.