Cookie in PHP

Google Advertisements

Prev Tutorial 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>

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

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

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

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs