Page 1 of 1

Cookies and headers not working

Posted: Fri May 31, 2002 5:55 am
by rats
just installed php 4.2.1 on WIN 2000 and IIS5 and I cant get cookies to work?

Have tried with Apache and php version 4.0.6 auto install and manual configuration.


Code: Select all

<?php $accesses++; setcookie("accesses", $accesses);
?>
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.

Code: Select all

<?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");	
    }
}
?>

Posted: Fri May 31, 2002 10:38 am
by fatal
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.

http://php.net/header
Maybe that can help you in your code writing.

Posted: Fri May 31, 2002 11:25 am
by sam
Try setting the cookie before sending the Location header. This might be a problem in the way that browsers handle the header processing.

Cheers Sam

Posted: Fri May 31, 2002 12:44 pm
by mikeq
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

Mike

php

Posted: Fri May 31, 2002 6:20 pm
by rats
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.

Posted: Sun Jun 02, 2002 9:33 am
by Stoyanski
What make you think that cookies does non work properly :?:
Read the first message in this forum and the questions will gone :twisted:

Posted: Sun Jun 02, 2002 9:57 am
by ozmodiar
In PHP 4.2.1, global variables have been turned off. Check out this page , it tells you how to access globals in PHP 4.2.1.

http://www.php.net/manual/en/language.v ... efined.php

Hope this helps

Re: php

Posted: Sun Jun 02, 2002 5:23 pm
by mikeq
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.

Posted: Mon Jun 03, 2002 6:11 am
by rats
Thanks but after another install cookies started working.

Posted: Mon Jun 03, 2002 1:11 pm
by Stoyanski
Cool 8O