PHP4.0 has a long waiting feature, which is supported by PHP sessions. In contrast, PHP3.0 users have to apply third-party libraries or do not have to perform this function. Lack of session support is the biggest disadvantage of PHP. PHP 4.0 has a feature that is always waiting for. this is the support of PHP sessions. In contrast, PHP 3.0 users have to apply third-party libraries or do not have to do this. The lack of session support is one of the biggest shortcomings of PHP and the most widely recognized place. However, as session support has become part of the latter since the beginning of PHP 4.0 in the early beta version, this obstacle also disappears.
With session support, you can maintain user-specific variables during user visits to the network site without having: set multiple cookies, hide form fields from applications, or store information in a database that you may frequently connect.
Starting a session on a page tells the PHP engine that you want to start a session (if not) or continue the current session:
Session_start ();
When a session is started, an ID string (for example, 940f8b05a40d3169c030c9c7745aead9) is sent to the user through a cookie. a temporary file that matches this is created on the server. in the preceding example, the name is like this: sess_940f8b05a40d3169c030c9c7745aead9. This file contains the registered session variables and their assigned values.
User visit counters are the most common examples of application sessions:
Start your PHP module to ensure that the PHP code is the first line of the file: no vacancy, no HTML output, and so on. This is because when the session function sends a file header, the system reports an error if you have sent the vacant or HTML code before the session_start () function.
// If a session does not yet exist for this user, start one
Session_start ();
Next, register a variable named count.
Session_register ('count ');
Registering a variable tells PHP that as long as the session exists, a variable named count also exists. Currently, this variable has not been assigned a value. However, if you add 1 to it, the value is assigned to 1:
$ Count;
Considering the code above, you have actually started a session (if not previously) A user is assigned a session id, a variable named count is registered, and $ count is added to 1 to display the user's first visit page:
To display the number of times a user visits a page under the current session, you only need to print the value of $ count:
Echo'
You 've been here $ count times.
';
The code for all visit counters is as follows:
Session_start ();
Session_register ('count ');
$ Count;
Echo'
You 've been here $ count times.
';
?>
If you reload the preceding script, you can see that the count value has increased. Interesting?
You can also register arrays in sessions. Suppose you have an array named $ faves:
$ Faves = array ('Chocolate', 'coffee ', 'beer', 'Linux ');
You can register the array like other single variables:
Session_register ('fafes ');
There is no difference between the index array and other single variables, such as $ faves. If your user wants to show his or her hobbies on a Web site page, you can register his or her hobbies as a session variable named $ faves, then you can print these values on other pages: