| | #1 |
| Active Warrior Join Date: 2013 Location: AU
Posts: 57
Thanks: 0
Thanked 1 Time in 1 Post
|
I have a web application where i want to check whether cookie is there or not , if cookie is there i want to do something if its not there i would like to create new cookies. How can i check whether cookies exist or not? |
| |
| | #2 |
| Active Warrior War Room Member Join Date: 2013 Location: Florida
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
|
Lets say your cookie is called my_cookie: if(!isset($_COOKIE['my_cookie'])) { // Cookie is not set, set it setcookie('my_cookie', 'some value'); } else { // Cookie exists, so something echo 'Im doing it'; } |
| |
| | #3 |
| Advanced Warrior War Room Member Join Date: 2006 Location: USA
Posts: 1,907
Thanks: 236
Thanked 1,377 Times in 676 Posts
|
Don't forget to set an expiration time for the cookie, otherwise it will expire as soon as the user closes their browser. PHP Code: |
| | |
| |
| | #4 |
| Active Warrior Join Date: 2009
Posts: 31
Thanks: 1
Thanked 4 Times in 4 Posts
| PHP Code: |
| |
| | #5 |
| Mobitech Join Date: 2012
Posts: 51
Thanks: 6
Thanked 2 Times in 2 Posts
|
<?php $expire=time(); setcookie("user", "Alex Porter", $expire); ?> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br>"; else echo "Welcome guest!<br>"; ?> |
| | |
| |
|
| Bookmarks |
| Tags |
| cookies, php |
| |