I have not worked with cookies much but I have run into a problem. I have a script that was creating a cookie OK until I added the header function in after it. It seems that the header function will run OK but the cookie never gets set. However, If I comment out the header() line the cookies are set again. has anyone come across this problem before or have any suggestions?

Any help would be appreciated.

Damien.

    Strange problem coz it never happens to me.
    Anyway, could you post your code here so that it can be reviewed. 🙂

      I won't paste all the code because it is too long. However, it is a login script that sets cookies when the user/pass has been approved. Then I want the user sent to a new page with the header function. Is it possible that the browest is redirected to a new page before the cookies are set? I am unsure.

      setcookie("cookie_user", md5($user), time()+$cto, "/", "", 0);
      setcookie("cookie_session", "$session", time()+$cto, "/", "", 0);
      header("Location: check_user.php");

      Damien.

        The cookies will be set before redirection happens, that's obvious.
        But what confuse me is when you stripped off the header, the cookie get set. How is that possible?
        Anything strange within your check_user.php?

        Nb: You dont have to have all parameters written in setcookie() function. The last two can be omited without any problems.

          As the script runs that cookies should be set before the redirection. I thought that because both the setcookie() and header() functions need to be sent before any output that maybe they were interfering with each other.

          check_user.php just contains the basic script that checks that the user is still valid. i.e. grabs the cookie vars via but does not modify in any way.
          I just created a temp.php file that prints a simple message to the browser which I have header() redirecting to and found that the cookie is still not being created. Therefore, the problem must not be with check_user.php.

          Thanks for your help so far scoppc but I still can't figure this out.

            The following code shows if the setcookie function failed or was a success.

            $user_c = setcookie("cookie_user", md5($user), time()+$cto, "/", "", 0); 
            $session_c = setcookie("cookie_session", "$session", time()+$cto, "/", "", 0);
            
            var_dump($user_c);
            var_dump($session_c);
            
            var_dump($_COOKIE);
            
             

              Thanks to all. The previous thread has showed what I thought might be the problem of header() and setcookie being mutually exclusive when running PHP as a CGI on IIS. I am running IIS and PHP as a CGI. My pc dual boots with FreeBSD running PHP as a module on apache so I'll give that a go.

              Thanks.

              Damien.

                Write a Reply...