What is Session in PHP?
A PHP session is used to store the information from the web pages. Normally web pages don’t have any memories to store these information. But using we can save the necessary information.
Now a day’s all application will maintain session. It mainly used to solve this problem by storing user information to be used across multiple pages.
For example
Session variable will save the user information once and maintain all the remaining pages of the same application.
How to start using Session in PHP?
Function session_start() is used to start the PHP session. For setting session variable PHP will use a keyword called $_session which is a global variable.
In every web page we need to check the session variable so that we need to get the session variable from the global variable which we set.
How to get the session variable in PHP?
We can get the session variable by using the global session variable.
After setting the session it is also possible to change the session. This is very useful because we can change the session at any time.
How to change the session in PHP?
Changing session variable is very simple and we easily overwrite the new session values.
By using the feature called destroy, user can delete a PHP session at any point of time.
For deleting the session we need to two things.
1) First, we need to remove all the global session variables.
2) Second (after step 1) we need to destroy the session
For removing the session variable we can use the function called session_unset()
. This function is used to clear all the session variables.
For destroying the session variables we can use the function called session_destroy()
. This function is used to destroy all the sessions.
Now coming to second part i.e. cookie in PHP
What is Cookie in PHP?
Cookie in PHP is used to identifying the user(s). Using cookie we can store the data. It is a small part of data which will store in user web browser. So whenever user browse next time browser send back the cookie data information to server for getting the previous activities. In PHP cookie can be created, retrieved, modified or deleted.
Cookie Creation in PHP
In PHP using setcookie() function we can create a cookie.
Syntax:
setcookie(name, value, expire, path, domain, security);
Options Details:
Name -> Name of the cookie
Value -> Value of the cookie (content) what we are going to save.
Expire ->Expiry time (How long this cookie will be active)
Path ->Set the path for which directories are valid. Using forward slash it permit for all directives.
Domain ->Specify the domain name so that this cookie will only valid for that particular domains.
Security -> Set 1 for Secure Transmission (HTTPS) or Set 0 for regular transmission (HTTP)
Retrieving Cookie in PHP
In PHP we can retrieve the cookie by using $_cookie
or using $HTTP_COOKIE_VARS
Modifying Cookie in PHP
For modifying a cookie you should set a new cookie with the same name.
Deleting a cookie
To delete a cookie we should call a setcookie() function and set past expiration date.