web 2.0

How to use PHP & Cookies ($_COOKIE,$HTTP_COOKIE_VARS)

How to use PHP & Cookies  ($_COOKIE,$HTTP_COOKIE_VARS) Learn PHP how to using Cookies and read a php variable , Create Cookies , Read Cookies , Delete/Clear Cookies

ShotDev Focus:
- Create Cookies
- Read Cookies
- Delete/Clear Cookies

Example

php_cookie1.php (Create Cookies)


<?
ob_start();
$strName = "Mr.Weerachai Nukitra";
setcookie("strName",$strName,time()+3600); // Create Cookies Expire 1 Hour
$strSiteName = "ThaiCreate.Com";
setcookie("strSiteName",$strSiteName,time()+3600); // Create Cookies Expire 1 Hour
?>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
Cookies Created.<br><br>
<a href="php_cookie2.php">Check Cookies</a>
</body>
</html>
<?
ob_end_flush();
?>

php_cookie2.php (Read Cookies)


<?
ob_start();
?>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
Cookies Check.<br>
$strName = <?=$_COOKIE["strName"];?><br>
$strSiteName = <?=$_COOKIE["strSiteName"];?><br>
<br>
<a href="php_cookie3.php">Delete Cookies</a>
</body>
</html>
<?
ob_end_flush();
?>

php_cookie3.php (Clear Cookies)


<?
ob_start();
setcookie("strName"); // Delete Cookies
setcookie("strSiteName"); // Delete Cookies
?>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
Created Check.<br>
$strName = <?=$_COOKIE["strName"];?><br>
$strSiteName = <?=$_COOKIE["strSiteName"];?><br>
<br>
<a href="php_cookie1.php">Create Cookies</a><br>
<a href="php_cookie2.php">Check Cookies</a><br>
</body>
</html>
<?
ob_end_flush();
?>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_cookie1.php

Screenshot

PHP Cookie

PHP Cookie

PHP Cookie

.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.