Thursday, December 29, 2022

Cookies in PHP

To create cookies setcookie( ) method is used:


Example:
setcookie("name","usman" ,time( ) + (86400 * 30), "/");
Syntax:
setcookie(name, value, validity time, path, domain);

To retrieve value from cookies:

Example:
echo $_COOKIE['name'];
Syntax:
$_COOKIE['cookie name'];


To discard cookies:

set cookies expiry time 1 hour back
setcookie("name","usman" ,time( ) - (3600), "/");


Download Code


index.php


<form action="#" method="POST" class="myform">
<input type="text" name="user" placeholder="Enter Your Name..." />
<input type="submit" />
</form>

<div class="myinfo">
<?php 
if(isset($_COOKIE['uid'])){
echo "<h1>Your Name is : ".$_COOKIE['uid']."</h1>";
}
?>

</div>


new.php


<?php
if(isset($_POST['user'])){
$user = $_POST['user'];
setcookie("uid",$user,time() + (86400 * 30), "/");
header("location:index.php");
}

Share this

1 Comment to "Cookies in PHP"

sagiramacfarlane said...

A Complete Guide to Merit Casino Sites and Bonus Offers
Find out how 인터넷 카지노 사이트 to use the Merit casino site, the choegocasino.com twitter top casinos 마리나 베이 샌즈 카지노 in Canada, 메리트카지노 쿠폰 plus their bonuses and offers. Find out how to use 코인카지노 the free