user3142443
user3142443

Reputation: 61

cookie value not shown

This is my code:

    <?php 
    $friendid = 10;
    $friendname = "enco";

    $max=count($_COOKIE['rooms']); 
    $i = $max + 1;

    setcookie("rooms[$i]['type']", "1on1", time() + 3600, "/", ".mywebsite.com");
    setcookie("rooms[$i]['name']", $friendname, time() + 3600, "/", ".mywebsite.com");
?>

The code below is in another page:

<?php
    $max=count($_COOKIE['rooms']); 

$k = 0;
for($k = 0; $k<$max; $k++) {
    echo "Cookie 1 show: " . $_COOKIE['rooms'][$k]['type'] . "<br /><br />";
    echo "Cookie 2 show: " . $_COOKIE['rooms'][$k]['name'] . "<br /><br />"; 
    }
    ?>

But it does not work. When I try to echo the cookies like I did in the example above, nothing appears.

My question is: Are these structures correct:

setcookie("rooms[$i]['type']", "1on1", time() + 3600, "/", ".mywebsite.com");
setcookie("rooms[$i]['name']", $friendname, time() + 3600, "/", ".mywebsite.com");

in order to show these in another page (not in the same page where the cookies are written):

echo "Cookie 1 show: " . $_COOKIE['rooms'][$i]['type'] . "<br /><br />";
echo "Cookie 2 show: " . $_COOKIE['rooms'][$i]['name'] . "<br /><br />";

Thanks

Upvotes: 0

Views: 39

Answers (1)

Marc B
Marc B

Reputation: 360922

PHP's superglobals _GET, _POST, _REQUEST, _COOKIE are all created at script startup, and then NEVER modified by PHP for the duration of the script's execution.

The cookie you create with setcookie() will therefore NOT be available in _COOKIE until the NEXT time you run this code.

Upvotes: 2

Related Questions