Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 7th Jul 2013, 07:58 PM   #1
Active Warrior
 
skillboyz's Avatar
 
Join Date: 2013
Location: AU
Posts: 57
Thanks: 0
Thanked 1 Time in 1 Post
Default
New to php and help regarding cookies
Share on: 
fb share twitter share gplus share more share

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?
skillboyz is offline  
Unread 7th Jul 2013, 09:09 PM   #2
Active Warrior
War Room Member
 
Paul Moss's Avatar
 
Join Date: 2013
Location: Florida
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default
Re: New to php and help regarding cookies
Share on: 
fb share twitter share gplus share more share

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';
}
Paul Moss is offline  
Unread 7th Jul 2013, 09:45 PM   #3
Advanced Warrior
War Room Member
 
Brandon Tanner's Avatar
 
Join Date: 2006
Location: USA
Posts: 1,907
Thanks: 236
Thanked 1,377 Times in 676 Posts
Default
Re: New to php and help regarding cookies
Share on: 
fb share twitter share gplus share more share

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:
// Set cookie for 30 days (2592000 seconds)
setcookie('my_cookie', 'some value', time() + 2592000); 


Brandon Tanner is offline  
Unread 8th Jul 2013, 12:37 PM   #4
Active Warrior
 
Join Date: 2009
Posts: 31
Thanks: 1
Thanked 4 Times in 4 Posts
Default
Re: New to php and help regarding cookies
Share on: 
fb share twitter share gplus share more share

PHP Code:
setcookie("cookie-name", -value, strtotime( '+30 days' ), "/", "", "", TRUE); 
Cookie expire time is 30 days in this case.
Rennell Garrett is offline  
Unread 12th Jul 2013, 01:24 AM   #5
Mobitech
 
octalsoftware's Avatar
 
Join Date: 2012
Posts: 51
Thanks: 6
Thanked 2 Times in 2 Posts
Default
Re: New to php and help regarding cookies
Share on: 
fb share twitter share gplus share more share

<?php
$expire=time();
setcookie("user", "Alex Porter", $expire);
?>


<?php
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br>";
else
echo "Welcome guest!<br>";
?>

octalsoftware is offline  
Closed Thread


Bookmarks

Tags
cookies, php


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 12:20 PM.