Session And Cookies in PHP

Cookie store on browser and Session is store on Server. You can instruct the browser to latter use. PHP provide the function to set the Cookie or instruct browser to setting the Cookies values. setcookie() method allows you to define how long the Cookie should be saved for and which path and domain it should be valid for it. below is example of cookie to save the values.

When you wants to retrieve the Cookies values just echo the variable name as below. $_COOKIE is global variable in PHP.

Suppose you wants to save the time when Cookies goes expire pass the third parameter which is time values see the below example. here we added 1 hour that is Unix timestamp.

Now implement the session that store information from the multiple pages. Unlike, the Cookie store information on user computer.

Session is hold the information about the signal user and use allover in one application how it will be define.
A Session is started with the session_start() function. You must be define at top the application and remove the white space when you are defines.

For example consider above page is index.php now when you access this on another page it will available. Now create the another page called as second index.php. $_SESSION is also global variable.

Now another method which is session_destroy(); and session_unset(); used to destroy the Session variables. session_unset(); used to destroy the signal Session variable and session_destroy(); is used to destroy entire Session variable in the applications.