$_COOKIE variable

Cookies is temporary stored simple files or simple data in the web site browser . The $_COOKIE variable stores information passed into the script through HTTP Cookies. Cookies stored date in the browser 1,024 bytes only. The cookie function must be put before any HTML code.

$_COOKIE variable syntax :

<?php
setcookie('name','value','expires','path','domain','secure');
?>

name

The name of your cookie. You will use this name to later retrieve your cookie.

Value

The value that is stored in your cookie. Common values are username (string) and last visit (date).

Expiration

The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

path

path specifies the server path of the cookie.

domain

domain specifies the domain name of the cookie

Secure

specifics whether a cookie should be transmitted or not over a secure connection

Storing a cookie Variable:

The following example cookie function have been used which function we have set three arguments that is Username , TutorialsScripts and time. In the Cookie username is set with value TutorialsScripts which is set to expire after one hour on user's computer.

Example:


<?php
setcookie("UserName", "TutorialsScripts", time()+3600);
?>
<html>
<body>
  ----------
  ----------
  ----------
</body>
</html>
?>

Retrieve a cookie Value:

Cookie information can retrieved using the $_COOKIE array variable.

Example:

The following will retrieve our username cookie value.

<?php
echo $_COOKIE["username"];
?>

Output:

TutorialsScripts




Content