Mark
Mark

Reputation: 151

PHP - Set cookie and redirect

I am trying to set a cookie and redirect. Using Debian GNU/Linux 6.0 (64 bit) with PHP 5.3.3-7+squeeze19 with Suhosin-Patch (cli) (built: Feb 17 2014 10:10:23) and Apache/2.2.16 (Debian).

For some reason this works:

<?php
    $cookie_name = $_GET['a'];
    $cookie_value = $_GET['b'];
    setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
?>

But this does not:

<?php
    $cookie_name = $_GET['a'];
    $cookie_value = $_GET['b'];
    setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
    header("Location: http://www.example.com");
    exit;
?>

Even after several page loads. I've tried adding error reporting to the top of my code, but I don't see any errors when I load the page nor in the Apache log (/var/log/apache2/error.log):

    error_reporting(E_ALL);ini_set('display_errors','1');

For some reason whenever I redirect, even using javascript as below, a cookie will not add.

<?php
    $cookie_name = $_GET['a'];
    $cookie_value = $_GET['b'];
    setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=http://www.example.com">
        <script type="text/javascript">
            window.location.href = "http://www.example.com"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        If you are not redirected, follow <a href='http://www.example.com'>this link</a>!
    </body>
</html>

Why does the first example work but not the others?

Upvotes: 3

Views: 7786

Answers (2)

acheo
acheo

Reputation: 3126

I was also getting this weirdness but with js redirect. Testing with chrome browser on xp.

The way I solved it was to do the cookie setting with injected js using document.cookie =

                ?>
                <script type="text/javascript">
                    function setCookie(cname, cvalue, exdays) {
                        var d = new Date();
                        d.setTime(d.getTime() + (exdays*24*60*60*1000));
                        var expires = "expires="+d.toUTCString();
                        document.cookie = cname + "=" + cvalue + "; " + expires;
                    }
                    setCookie("foo","<?php echo $bar; ?>",30);
                    window.location = "<?php echo $destination_page; ?>.php";
                </script>
                <?php

then the problem went away.

It felt like the redirect was causing the php setcookie to fail for some reason...

Upvotes: 0

Misunderstood
Misunderstood

Reputation: 5661

Use include rather than redirect

This also saves the Browser a round trip HTTP request

<?php
    $cookie_name = $_GET['a'];
    $cookie_value = $_GET['b'];
    setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
    include('/home/user/public_html/index.html');
    exit;
?>

While I prefer include over a redirect header, your cookie should work. I have tested and it works just like it should.

In my test I redirected to another domain. The cookie is set in the domain where the PHP script resides.

setcookie('test', 'test', time() + (86400 * 30), "/");
header("Location: http://www.intel.com");

Works just like it is supposed to:

enter image description here

Upvotes: 1

Related Questions