PHP Cookie
Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. you can save cookie to the user browser using php setcookie() function.In This Lesson we will learn how to create ,retrieve ,update and delete cookie..
Step 1 :php setcookie: Create Cookie
<?php $int='3600';//Set Cookie time Duration 1 Hour; setcookie("UserName","ANJAN KUMAR",time()+$int); //username is your cookie's name //value is cookie's value //$int is time of cookie expiration ?>Step 2: php get cookie(Retrieve Cookie from browser)
Here we have used if(isset($_COOKIE["UserName"])) to identify the cookie are exist or not.
if cookie exist it will return the cookie Username else Return 'No Cookie Found'.
<?php if(isset($_COOKIE['UserName'])){ echo 'Your UserName Is: '.$_COOKIE["UserName"].' <br> '; //Getting All Cookie as Array print_r($_COOKIE); }else{ echo "No Cookie Found"; } ?gt;Step 3: Update Cookie:
Use below codes to update an existing cookie .you can modify the below codes as your requirement
<?php $int='3600';//Set Cookie time Duration 1 Hour; $cookie=setcookie("UserName","ANJAN DHALI",time()+$int); //name is your cookie's name //value is cookie's value //$int is time of cookie expires ?>Step 4: Unset Cookie(Delete Cookie from Browser):
<?php $cookie=setcookie("UserName","ANJAN KUMAR",time()-1); //UserName is cookie Name ?>
Some Examples of PHP Cookie::
=>Creating and Retrieve Multiple cookie:
<?php // set the cookies setcookie("cookie[three]", "cookiethree"); setcookie("cookie[two]", "cookietwo"); setcookie("cookie[one]", "cookieone"); // after the page reloads, print them out if (isset($_COOKIE['cookie'])) { foreach ($_COOKIE['cookie'] as $name => $value) { $name = htmlspecialchars($name); $value = htmlspecialchars($value); echo "$name : $value <br/> \n"; } }else{ echo "No Cookie Found"; } ?>=>PHP COOKIE CRUD(Create,Read,Update,Delete Using HTML Form):
Create index.php file and save the below codes.
<form method="POST"> <select name="color"> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> <option value="Black">Black</option> <option value="Brown">Brown</option> </select><br/> <input type="submit" name="submit" value="Set Cookie"/>|<input type="submit" name="delete" value="Delete Cookie"/> |<input type="submit" name="update" value="Update Cookie"/><br/> <input type="submit" name="get" value="Get Cookie"/> </form> <a href="index.php">Refresh to View Changes</a><br/>now add the below php codes after your html codes..
<?php if(isset($_POST['submit'])){ $color=$_POST['color']; // color is cookie's name setcookie("color",$color); /*new color is blue*/ } //Getting Cookie if(isset($_POST['get'])){ $color= $_COOKIE["color"]; } if(isset($_POST['update'])){ $color=$_POST['color']; setcookie("color",$color); /*new color is blue*/ } //Delete Cookie if(isset($_POST['delete'])){ setcookie("color","",time()-1); } //echo The Cookie Result if(isset($_COOKIE['color'])){ $color= $_COOKIE["color"]; echo $color; }else{ echo "No Cookie Found"; } ?>Now Run Your index.php file from localhost to watch result
Best Examples for PHP Cookie:
Change Website Color using php cookie function..
Create two files named:
index.php and table.php
Note: index.php page for save and retrieve color name from browser. table file to retrieve color from browser and show as a table.
Index.php Codes:
Create two files named:
index.php and table.php
Note: index.php page for save and retrieve color name from browser. table file to retrieve color from browser and show as a table.
Index.php Codes:
<?php #checking if form has been submitted if (isset($_POST['submit'])){ #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']; } ?gt;now copy below html codes and place after php codes
<html> <body bgcolor="<?php echo $bgColor;?>" text="<?php echo $txtColor;?>"> <form 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></br> <input type="submit" name="submit" value="remind"> </form><br/> <a href="index.php">Refresh to Watch Result</a> </body> </html>That's It Now run index.php from localhost and watch result..
PHP Cookie |
Table.php Codes:
now we will use the browser cookie to change table background and text color:
<?php $txtColor=$_COOKIE['txtColor']; $bgcolor=$_COOKIE['bgColor']; ?> <style> table{ background:<?php echo $bgcolor; ?>; color:<?php echo $txtColor;?>; } </style> <table border="1" cellspacing="0" width="40%"> <tr> <th>Name</th> <th>Age</th> <th>University</th> </tr> <tr> <td>ANJAN KUMAR</td> <td>21 Years</td> <td>Dhaka University</td> </tr> <tr> <td>ANJAN KUMAR</td> <td>21 Years</td> <td>Dhaka University</td> </tr> <tr> <td>ANJAN KUMAR</td> <td>21 Years</td> <td>Dhaka University</td> </tr> <tr> <td>ANJAN KUMAR</td> <td>21 Years</td> <td>Dhaka University</td> </tr> </table>Result:
PHP Cookie |
Thank you for Reading our Post. Keep visiting for More Codes..Please like,share and comment's..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.