How to create and print array of cookies with setcookie in PHP

3 Answers

0 votes
/*
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 
                                                    [, string $path = "" 
                                                    [, string $domain = "" 
                                                    [, bool $secure = false 
                                                    [, bool $httponly = false ]]]]]] )

*/
 
// expire at the end of the session, when the browser closes
setcookie("cookie[one]", "cookie_one");
setcookie("cookie[two]", "cookie_two");
setcookie("cookie[three]", "cookie_three");
?>
<html>
<body>
<?php

if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) 
        echo "$name : $value <br />";
}
?>
</body>
</html> 
<?php
      
/*
run:
  
three : cookie_three 
two : cookie_two 
one : cookie_one 
  
*/

?>

 



answered Dec 20, 2015 by avibootz
0 votes
<?php
/*
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 
                                                    [, string $path = "" 
                                                    [, string $domain = "" 
                                                    [, bool $secure = false 
                                                    [, bool $httponly = false ]]]]]] )

*/
 
// expire at the end of the session, when the browser closes
setcookie("cookie[one]", "cookie_one");
setcookie("cookie[two]", "cookie_two");
setcookie("cookie[three]", "cookie_three");
?>
<html>
<body>
<?php

echo '<pre>' . print_r($_COOKIE, 1) . "</pre>";

?>
</body>
</html> 
<?php
      
/*
run:
  
Array
(
    [user_name] => Erin Baron
    [cookie] => Array
        (
            [three] => cookie_three
            [two] => cookie_two
            [one] => cookie_one
        )

)

  
*/

?>

 



answered Dec 20, 2015 by avibootz
0 votes
<?php
/*
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 
                                                    [, string $path = "" 
                                                    [, string $domain = "" 
                                                    [, bool $secure = false 
                                                    [, bool $httponly = false ]]]]]] )

*/
 
// expire at the end of the session, when the browser closes
setcookie("cookie[one]", "cookie_one");
setcookie("cookie[two]", "cookie_two");
setcookie("cookie[three]", "cookie_three");
?>
<html>
<body>
<?php

echo $_COOKIE['cookie']['one'] . "<br />";
echo $_COOKIE['cookie']['two'] . "<br />";
echo $_COOKIE['cookie']['three'] . "<br />";

?>
</body>
</html> 
<?php
      
/*
run:
  
cookie_one
cookie_two
cookie_three
  
*/

?>

 



answered Dec 20, 2015 by avibootz

Related questions

2 answers 290 views
1 answer 549 views
1 answer 189 views
1 answer 215 views
3 answers 282 views
1 answer 238 views
...