note 105120 added to function.setcookie
| From: | mkmohsinali at gmail dot com | Date: | Thu, 28 Jul 2011 18:01:36 +0000 |
| Subject: | note 105120 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181963@lists.php.net to get a copy of this message | ||
#cookies.php
/*This code will demonstrate use of cookies with PHP
It is very easy to understand and is better for beginner to
understand and get idea about power of cookies when used
with PHP.Here we give user a form to choose colors he/she
likes for website and when he/she visits site again within one
hour his/her settings are saved and read from cookie
and he/she doesn't have to set the page color and page
text color again.You can change time from 3600
seconds to whatever you deem appropriate in your case.
if you don't understand anything please email me*/
<?php
#checking if form has been submitted
if (isset($_POST['submitted'])){
#if yes (form is submitted) assign values from POST array to variables
$newbgColor=$_POST['bgColor'];
$newtxtColor=$_POST['txtColor'];
#set cookies
setcookie("bgColor",$newbgColor,time()+3600);
setcookie("txtColor",$newtxtColor,time()+3600);
}
#in case user has come for first time and cookies are not set then
if ((!isset($_COOKIE['bgColor']) ) && (!isset($_COOKIE['txtColor']))){
$bgColor = "Black";
$txtColor="White";
}
#if cookies are set then use them
else{
$bgColor = $_COOKIE['bgColor'];
$txtColor = $_COOKIE['txtColor'];
}
?>
<!-- HTML Page-->
<html>
<body bgcolor="<?php echo $bgColor ?>" text="<?php echo $txtColor
?>">
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method
="POST">
<p>Body Color:</p>
<select name=bgColor>
<option value ="Red">Red</option>
<option value ="Green" selected>Green</option>
<option value ="Blue">Blue</option>
<option value ="Yellow">Yellow</option>
<option value ="Black">Black</option>
<option value ="Brown">Brown</option>
<option value ="White">White</option>
</select>
<p>Text Color:</p>
<select name=txtColor>
<tion value ="Red">Red</option>
<option value ="Green" selected>Green</option>
<option value ="Blue">Blue</option>
<option value ="Yellow">Yellow</option>
<option value ="Black">Black</option>
<option value ="Brown">Brown</option>
<option value ="White">White</option>
</select>
<input type ="hidden" name="submitted" value="true"></br>
<input type="submit" value="remind">
</form>
</body>
</html>
----
Server IP: 69.147.83.197
Probable Submitter: 182.177.44.228
----
Manual Page -- http://www.php.net/manual/en/function.setcookie.php
Edit -- https://master.php.net/note/edit/105120
Del: integrated -- https://master.php.net/note/delete/105120/integrated
Del: useless -- https://master.php.net/note/delete/105120/useless
Del: bad code -- https://master.php.net/note/delete/105120/bad+code
Del: spam -- https://master.php.net/note/delete/105120/spam
Del: non-english -- https://master.php.net/note/delete/105120/non-english
Del: in docs -- https://master.php.net/note/delete/105120/in+docs
Del: other reasons-- https://master.php.net/note/delete/105120
Reject -- https://master.php.net/note/reject/105120
Search -- https://master.php.net/manage/user-notes.php