What is the Session and cookie in PHP and how to use it for beginners

 What is the Session in PHP and how to use it for beginners 





What is a Session ?

  • A session is a global variable stored on the server .
  • Each session is assigned a unique id which is used to retrieve stroed values.
  • A cookie holding the specific session id is stored on the user's computer whenever a session is created and sent back to the server with each request. The particular PHP session id is displayed to cookies if the client browser does not support cookies.
  • Compared to cookies, sessions have a larger storage capacity for data.
  • When the browser is shut off, the session values are automatically removed. You should store the value in the database if you wish to keep it there permanently.
  • Session variables are saved in the $_SESSIOn array variable, which must come before any HTML tags, similarly to the $_COOKIE array variable.
What is the Session and cookie in PHP and how to use it for beginners


Why and when to use sessions ?

  • You wish to store sensitive data more securely on the server, away from the reach of malevolent users, so things like the user id can be stored there.
  • The goal is to transfer values from one object to another.
  • You want the alternative to cookie on browsers that do not support cookies,
  • As opposed to passing them in the URL, you want to keep global variables in a way that is both more effective and secure.
  • When creating an application, like a shopping cart, it is necessary to temporarily store information with a storage capacity more than 4KB.

Creating Session

You must first use the PHP session start function to create a session before storing your values in the $_SESSION array variable.

Destroying a PHP Session 

The session destroy() function in PHP allows you to end a session. All session variables can be deleted with a single call to the function, which requires no arguments. Use the unset() function to remove a single session variable if you wish to delete it completely.

What is a PHP cookie 

A PHP cookie is a short data file that is stored in the client browser. It is employed to identify the user. Cookie creation occurs on the server, and client browsers save the data. whenever a client requests something from the server. A cookie is integrated into the request. Cookies can be received at the server side in this manner.

PHP's distinction between sessions and cookies

Cookie                                                        Session

Cookies are stored in browser as             Sessions are stored in server side
text file format

There is a maximum quantity of data      It is stored unlimited amount of data
stored.             

It is only allowing 4kb [4096bytes]          It is holding the multiple variable in session

It is not holding the multiple variable      It is holding th multiple variable in session

We can accessing the cookie values in     We cannot accessing the session values in easily . So it is more secure.

Setting the cookie time to expire              using sessiom_destroy(), we will destroyed  the sessions.

sstcookie() function must appear              session_start() function must be the very first thing in yuor document.before any HTML tags.            easily. Si it is less secure
BEFORE the <html>tag.

PHP setcookie() function

To set a cookie with an HTTP response, use the setcookie() method in PHP. Once cookie is set, the superglobal variable $_COOKIE can be used to access it.

Creating cookies

<php
setcookie_name, cookie_value,[expiry_time ],[cookie_path],
[dmain],[secure],[httpponly]);

?>

Here

  • The PHP function used to create cookies is called php setcookie.
  • The name of the cookie that the server will use to get its value out of the $_COOKIe array variable is cookie name. It is required.
  • "Cookie_value" is the value of the cookie and its mandatory
  • Expiry time is an optional field that can be used to specify a cookie's expiration time, such as an hour. Using the PHP time() functions, the time is set using a plus or minus value higher than zero.
  • The cookie path on the server can be set using the optional parameter cookie path. The cookie will be made accessible throughout the entire domain when the forward slash "/" is used.
  • domain is optional cookie domainfor example www.php.net. to make cookies visible on all sub domain must be prefixed with a dot like php.
The setcookie() function comes in two basic flavours:
  • Session cookies-- cookies that are temporary and expire when the browser is closed.
  • Persistent cookie-- Cookies that expire after a certain amount of time

Thanks for visiting our site any query or question plz comment bellow or contact us .


Post a Comment

Previous Post Next Post