Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,940 questions

51,877 answers

573 users

How to create and print a cookie with setcookie in PHP

2 Answers

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

*/
 
$cookie_name = "user_name";
$cookie_value = "Erin Disco";
setcookie($cookie_name, $cookie_value, time() + 3600); // expire in 1 hour
?>
<html>
<body>
<?php
// page need to be reload
if(!isset($_COOKIE[$cookie_name])) 
    echo "Cookie named '" . $cookie_name . "' is not set";
else {
    echo "Cookie '" . $cookie_name . "' is set! <br />";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html> 
<?php
      
/*
run:
  
Cookie 'user_name' is set 
Value is: Erin Disco 
  
*/

?>

 



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

*/
 
$cookie_name = "user_name";
$cookie_value = "Erin Baron";
setcookie($cookie_name, $cookie_value, time() + 60*60*24*30); //  expire in 30 days
?>


<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) 
    echo "Cookie named '" . $cookie_name . "' is not set";
else {
    echo "Cookie '" . $cookie_name . "' is set! <br />";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html> 

<?php
      
/*
run:
  
Cookie 'user_name' is set! 
Value is: Erin Baron 
  
*/

 



answered Dec 20, 2015 by avibootz

Related questions

3 answers 259 views
3 answers 263 views
1 answer 533 views
1 answer 167 views
1 answer 222 views
1 answer 197 views
...