3

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?

Mark
  • 151
  • 1
  • 6

2 Answers2

1

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

Misunderstood
  • 5,534
  • 1
  • 18
  • 25
  • Doesn't work, but might be because there is header('Location: http://www.example.com/index?c=d') In /home/user/public_html/index.html – Mark Apr 15 '15 at 02:54
  • You have to redirect to (include) a page with no redirect. – Misunderstood Apr 15 '15 at 03:04
  • The cookie should work with the redirect header. do you have a link I can try that has the ` header("Location: http://www.example.com");` – Misunderstood Apr 15 '15 at 03:05
  • header('Location: example.com/index?c=d') there is no .html on that location, it would redirect to an index directory or a file name index with no extension. – Misunderstood Apr 15 '15 at 03:09
  • I created my page with set cookies and a redirect, and the Browser has the cookies. Is the redirect redirect to a different domain or sub domain? – Misunderstood Apr 15 '15 at 03:17
  • Unfortunately I am testing on a local/private network. Also, sorry I mistyped the URL; it is "http://www.example.com/index.html?c=d" but the file to set the cookie is in http://www.example.com/setcookie.php. – Mark Apr 15 '15 at 03:42
  • I think you overlooking something, I used your code exactly except I changed example.com to intel.com and used 'test' as the cookie key and value.
    When you look at the HTTP Request Headers do you see `Set-Cookie:` .
    – Misunderstood Apr 15 '15 at 04:41
  • "When you look at the HTTP Request Headers do you see `Set-Cookie:`" - Should be "Response Headers". – MrWhite Nov 08 '15 at 21:49
0

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...

acheo
  • 3,106
  • 2
  • 32
  • 57