Session in PHP is a way of temporarily storing and making data accessible across all the trang web pages. It will create a temporary tệp tin that stores various session variables and their values. This will be destroyed when you close the website. This file is then available to all the pages of the website to access information about the user.
Bạn đang xem: Getting started with php sessions
You need to lớn set the path for this file using the session.save_path setting from the php.ini file. If you don’t mix this path, the session might malfunction.
What Is the Use of Session in PHP?
A session in PHP allows the webserver khổng lồ get the information about when you started a website, what you were doing, when you closed the website & other related information. It is required because, unlike the PC or mobile, the webserver does not have any information about you. That’s where sessions come into the picture.
These sessions have session variables that store all the necessary information into a temporary file. By default, it will destroy this tệp tin when you close the website. Thus, to put it simply, a session in PHP helps in storing information about users and makes the data available to all the pages of a trang web or application until you close it.
What Happens When You Start a Session in PHP?
The following things occur when a session is started:It creates a random 32 digit hexadecimal value as an identifier for that particular session. The identifier value will look something like 4af5ac6val45rf2d5vre58sd648ce5f7.It sends a cookie named PHPSESSID to the user’s system. As the name gives out, the PHPSESSID cookie will store the unique session id of the session.A temporary file gets created on the server & is stored in the specified directory. It names the tệp tin on the hexadecimal id value prefixed with sess_. Thus, the above id example will be held in a tệp tin called sess_4af5ac6val45rf2d5vre58sd648ce5f7.
PHP will access the PHPSESSID cookie and get the chất lượng id string to get session variables’ values. It will then look into its directory for the file named with that string.
When you close the browser or the website, it terminates the session after a certain period of a predetermined time.
How lớn Start a PHP Session?
You can start a session in PHP by using the session_start() function. This function will, by default, first check for an existing session. If a session already exists, it will bởi vì nothing, but it will create one if there’s no pre-existing session available.
To set session variables, you can use the global array variable called $_SESSION<>. The vps can then access these global variables until it terminates the session. Now that you know what a session is in PHP và how khổng lồ start one, it’s time to look at an example & see how it works.
Note: It is always recommended to lớn put the session_start() function as the first line in your code, even before any HTML tags.
Example: How lớn Start a Session in PHP?
In the example below, you will start a session that will count how many times you have visited a website page. For this, you will create a session variable named counter.