PHP Cookie PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
PHP Cookie
PHP Cookie
PHP cookie is a small piece of information which is stored at client browser. It is used to recognize the user.
Cookie is created at server side and saved to client browser. Each time when client sends request to the server, cookie is embedded with request. Such way, cookie can be received at the server side.
In short, cookie can be created, sent and received at server end.
PHP setcookie() function
PHP setcookie() function is used to set cookie with HTTP response. Once cookie is set, you can access it by $_COOKIE superglobal variable.
Syntax
- bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path
- [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
Example
- setcookie("CookieName", "CookieValue");/* defining name and value only*/
- setcookie("CookieName", "CookieValue", time()+1*60*60);//using expiry in 1 hour(1*60*60 seconds or 3600 seconds)
- setcookie("CookieName", "CookieValue", time()+1*60*60, "/mypath/", "mydomain.com", 1);
PHP $_COOKIE
PHP $_COOKIE superglobal variable is used to get cookie.
Example
- $value=$_COOKIE["CookieName"];//returns cookie value
PHP Cookie Example
File: cookie1.php
- <?php
- setcookie("user", "Sonoo");
- ?>
- <html>
- <body>
- <?php
- if(!isset($_COOKIE["user"])) {
- echo "Sorry, cookie is not found!";
- } else {
- echo "<br/>Cookie Value: " . $_COOKIE["user"];
- }
- ?>
- </body>
- </html>
Output:
Firstly cookie is not set. But, if you refresh the page, you will see cookie is set now.
Output:
PHP Delete Cookie
If you set the expiration date in past, cookie will be deleted.
File: cookie1.php
- <?php
- setcookie ("CookieName", "", time() - 3600);// set the expiration date to one hour ago
- ?>