Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand PHP conversation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to understand PHP conversation". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Guide reading

From the point of view of the title, the scope of this article has been delineated, trying to clarify how Session (called Session for conversation) is implemented and used in PHP language, with emphasis on the use after basic understanding.

The idea of the article is to first figure out what session is, what is the use of Session, what tricks Session uses, and how it is used in PHP.

2. Session (Session) description

Before starting, first recommend a basic theory book "HTTP authoritative Guide", which is the basic knowledge of programmers. The author has an electronic version, and you can leave a message if you need it.

A. The origin of Session

Almost everyone goes online, and hundreds of millions of data are transmitted to each other in the network. The reason why the data can be transmitted safely is based on the HTTP protocol, which is very familiar, right? In fact, what the HTTP protocol does is to provide a series of ways to complete your network access, both sides build a visit, in principle, a session is established. To give an example: Xiaoming typed https://www.baidu.com/(HTTPS into the browser as an encrypted version of HTTP, compared with adding SSL encryption layer), this is Xiaoming launched a request to Baidu, saying: "I want to see your interface". Baidu's servers received a message that included what Xiao Ming wanted to do, as well as Xiaoming's address (otherwise Baidu would not know who to give the content to) The server checks the information OK, puts on record the request of Xiaoming, sends out what the nickname wants, and a complete request ends. This is a conversation, and the core of the conversation is Xiao Ming's information filing (in fact, it also involves the connection of TCP/IP, which has nothing to do with this article and is ignored).

In fact, rather than building a Session, it is better to summarize a visit as a Session.

B. What can Session do?

From the above, we can see that every visit is a session, and the server has to record information, which is expensive. At the same time, it is impossible for the same person to build and save 10 times and 20 times in a row. One is to increase the overhead, and the other is to be stupid. In other words, why is it that a person (exactly the same computer and browser) can reuse a Session at a particular time? Because Session has a default expiration time, the server will be cleaned up after it expires (if not, if you think about so many people in the world, it's too bad to keep one at a time).

Ok, since the same person, multiple visits are a Session (don't doubt that the server cannot tell the same person, see the book recommended above), and the content of each visit is for the record, that is to say, all the behavior servers in your Session cycle are aware of, then the next important role comes, the server analyzes your access requests. Those who can learn about the behavior preference of this particular user can push some data that users like to care about by doing some analysis, and that's where advertising orientation comes from.

Of course, there may be other users, performance, and so on, individuals do not particularly understand the mechanism, that's all here.

3. Use of Session in PHP

From the above wordiness, we can find that the concept of Session actually occurs on the server side. PHP provides a series of configurations, functions, etc., to achieve a good Session function. Session supports a method to save some data during concurrent access in PHP. This allows you to build more customized programs to improve the attractiveness of your web site. A visitor to your web site will be assigned a unique id, the so-called session id. This id can be stored in a cookie on the client side or passed through URL. Session support allows you to save the requested data in the super-global array $_ SESSION. When a visitor visits your site, PHP will automatically check (if session.auto_start is set to 1) or at your request (explicitly through session_start () or implicitly through session_register ()) whether the current session id is created by a previously sent request. If this is the case, the previously saved environment will be rebuilt.

Basic usage of session in php

By assigning a unique Session ID to each individual user, the function of storing data for different users can be realized. Sessions are often used to save and share information between multiple page requests. Generally speaking, Session ID is sent to the browser through cookie, and the data in the session is also retrieved through the session ID on the server side. If the session ID information is not included in the request, PHP creates a new Session and assigns a new ID to the newly created Session.

The workflow of Session is simple. When you start a Session, PHP tries to find the Session ID from the request (usually through Session cookie), and if the request does not contain Session ID information, PHP creates a new Session. After Session starts, PHP sets the data in Session to the $_ SESSION variable. When PHP stops, it automatically reads the contents of $_ SESSION, serializes it, and sends it to the session preservation manager for saving. By default, PHP uses the built-in file Session save manager (files) to save Session. You can also modify the Session save manager you want to use through the configuration item session.save_handler (configure the project in php.ini). For the file Session save manager, Session session data is saved to the location specified by the configuration item session.save_path (configuration project in php.ini). You can start a session manually by calling the function session_start. If the configuration item session.auto_start is set to 1, Session starts automatically when the request starts. After the execution of the PHP script, session shuts down automatically. At the same time, you can manually close the session by calling the function session_wirte_close ().

B. session information in php is configured in php.ini

This part is put here because, without indicating the previous problem, who knows what the configuration in php.ini is. The session.save_handler and session.save_path mentioned above are the configuration items in php.ini, and this one is not detailed, because the php manual is so detailed. The default mode for this article is files.

C. Session mechanism in php

Session_start () is the beginning of the session mechanism, and session will determine whether there is a $_ cookie [session _ name ()]; session_name () returns the COOKIE key value that holds the session_id, generates a session_id if it does not exist, and then passes the generated session_id to the client as the value of COOKIE. This is equivalent to performing the following COOKIE operation. Instead, if there is a session_id = $_ Cookie [session _ name]; then go to the folder specified by session.save_path to find the file named 'SESS_'.session_id (). Read the contents of the file to deserialize and put it in $_ SESSION.

At the end of the session, the Session write operation is performed or the session_write_close () operation is performed manually.

There are usually three ways to destroy Session in the code.

1. Setcookie (session_name (), session_id (), time ()-80000000,); / / execute before logging out

2. Usset ($_ SESSION); / / this deletes all $_ SESSION data. After refreshing, COOKIE is sent, but there is no data.

3. Session_destroy (); / / Delete $_ SESSION Delete the session file and session_id

At the end of the appendix, quote a piece of code on the network.

/ / call function open ($save_path, $session_name) {global $sess_save_path; $sess_save_path=$save_path; return (true) when SESSION initializes;} / / call function close () {return (true);} function read ($id) {global $sess_save_path; $sess_file= "$sess_save_path/sess_$id" when closed. Return (string) @ file_get_contents ($sess_file);} / / write operation function write ($id,$sess_data) {global$sess_save_path; $sess_file= "$sess_save_path/sess_$id"; if ($fp= @ fopen ($sess_file, "w")) {$return=fwrite ($fp,$sess_data); fclose ($fp); return$return before the end of script execution } else {return (false);}} function destroy ($id) {global$sess_save_path; $sess_file= "$sess_save_path/sess_$id"; return (@ unlink ($sess_file));} function gc ($maxlifetime) {global$sess_save_path; foreach ("$sess_save_path/sess_*") as$filename) {if (filemtime ($filename) + $maxlifetime

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report