Cookie in PHP

Google Advertisements

Inheritance in PHP Next Tutorial

Cookie in PHP

Cookie is generally used to identify a user. Cookies are text files stored on the client computer and they are kept of use tracking purpose. PHP transparently supports HTTP cookies. Using PHP, you can both create and retrieve cookie values.

Note: PHP Cookie must be used before <html> tag.

cookie in php

There are three steps involved in identifying returning users;

  • Server script sends a set of cookies to the browser. For example name, age, or identification number etc.
  • Browser stores this information on local machine for future use.
  • When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.

Create Cookies in PHP

A cookie is created in php using setcookie() function. Here only the name parameter is required. All other parameters are optional.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

Create/Retrieve a Cookie in PHP

The following example creates a cookie named "user" with the value "Hitesh Kumar". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).

We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:

Create Cookie

<?php
$cookie_name = "user";
$cookie_value = "Hitesh Kumar";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(!isset($_COOKIE[$cookie_name]))
{
 echo "Cookie named '" . $cookie_name . "' is not set!";
} else 
{
 echo "Cookie '" . $cookie_name . "' is set!
"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <body> <html>

Modify a Cookie Value

To modify a cookie, just set (again) the cookie using the setcookie() function

Modify Cookie

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(!isset($_COOKIE[$cookie_name]))
{
  echo "Cookie named '" . $cookie_name . "' is not set!";
} 
else 
{
 echo "Cookie '" . $cookie_name . "' is set!
"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <body> <html>

Delete a Cookie in PHP

Delete Cookie

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>

<!DOCTYPE html>
<html>
<body>
<?php

echo "Cookie 'user' is deleted.";

?>
<body>
<html>

Check Cookies are Enabled or not

To Check Cookies are Enabled or not, First Create a test cookie with the setcookie() function, then count the $_COOKIE array variable;

Check Cookie Enabled ?

<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(count($_COOKIE) > 0)
{
 echo "Cookies are enabled.";
} else 
{
 echo "Cookies are disabled.";
}

?>
<body>
<html>

Inheritance in PHP Next Tutorial

Google Advertisements

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs