I have a php file which simply has to set a cookie and then forward the user to another page. The entire file currently consists of:

<?php
setcookie("cclang", "$lang");
header("Location: index.php");
?>

The user is forwarded but no cookie is set. If I remove the header part, ie.

<?php
setcookie("cclang", "$lang");
?>

the cookie is set. What am i doing wrong?

Thanks,
Jon.

    I do exactly what you're doing all the time, and never have any trouble with it.

    Always put an exit; after your location, or use it in the form
    die(header('Location: blah-blah...'));

    PS. what version of php you using, as in the 'old days' the headers would go out in reverse order, and that might apply to the cookies, too.

      Thats what seems odd, i've always used it before, on the same server even, and it's always worked.

      I thought i must be making some sort of really stupid mistake, which i couldn't see.

      What are the alternatives?

        I'm not sure what version my host uses, but i've also tried:

        <?php
        header("Location: index.php");
        setcookie("cclang", "$lang");
        ?>

        which also doesn't work

          try using the
          session_write_close()
          just before the header()

            nope, doesn't seem to help. I've used a javascript as a temporary alternative - but, obviously it's not ideal.

              hm...the only time I had a similar problem was when the domain changed.
              (from secure.domain.com to www.domain.com or back)
              but setting the cookie_domain in that case helped.

              you probably want to check the header what exactly is happening.
              try this here if you have no local tool:
              http://www.delorie.com/web/headers.html

                Write a Reply...