Cookies and headers 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
rats
Forum Newbie
Posts: 21
Joined: Fri May 31, 2002 5:55 am

Cookies and headers not working

Post 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");	
    }
}
?>
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post 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.
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
rats
Forum Newbie
Posts: 21
Joined: Fri May 31, 2002 5:55 am

php

Post 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.
Stoyanski
Forum Newbie
Posts: 15
Joined: Sun Jun 02, 2002 9:33 am
Location: BG
Contact:

Post 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:
ozmodiar
Forum Commoner
Posts: 35
Joined: Sat Jun 01, 2002 6:29 am
Location: Dublin, Ireland

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Re: php

Post 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.
rats
Forum Newbie
Posts: 21
Joined: Fri May 31, 2002 5:55 am

Post by rats »

Thanks but after another install cookies started working.
Stoyanski
Forum Newbie
Posts: 15
Joined: Sun Jun 02, 2002 9:33 am
Location: BG
Contact:

Post by Stoyanski »

Cool 8O
Post Reply