"Follow" of the variable: cookie and session

zhaozj2021-02-16  156

"Follow" of the variable: cookie and session

"Follow" of the variable: cookie and session

Summary

There are many ways to implement variables "follow", which is more useful for cookie and session. Here we use the very popular PHP to explain their usage. (2002-09-02 13:24:37)

By Wing, Source:

Blue wind

In many cases, we need to track the activities of the viewer throughout the website, automatic or semi-automatic identification of them (that is, the function of usually said to the website), this time we often use a set of variables " Follow "visitors. There are many ways to implement variables "follow", which is more useful for cookie and session. Here we use the very popular PHP to explain their usage. First, cookie's use cookie is the information stored in the browser client, which means that the variables saved in the machine's machine, generally sent to the client as the HTTP header is sent to the client. Before the cookie takes effect, the customer will send the cookie to the server each time you issue a page request, as long as we perform corresponding processing for it, the variable "follow" can be implemented. 1. Set a cookie variable to set a cookie variable, the function used by PHP is:

Int setCookie (String Name, String Value, Int Expire, String Path, String Domain, INT Secure); where Name is a cookie variable name identifier, you can reference the cookie variable like using a normal variable name in PHP. Value is the initial value of the cookie variable, which represents the effective time of the cookie variable; Path is the correlation path of the cookie variable; Domain represents the website of the cookie variable; Secure needs to be valid when the HTTPS is secure. For example, we have to set a variable username, its value is a string "bluewind", we can write code:

SetCookie ("UserName", "Bluewind"); // These two parameters are setcookie necessary. We also want to set the variable to set the effective time to limit the operation timeout, such as 10 minutes:

SetCookie ("UserName", "Bluewind", 600000); // The unit of valid time is millisecond. Note: Like the SETCOOKIE and Header function, you need to put it before any statements that can be output to the client. 2. Destroy a variable destroy cookie variable as long as it is set to empty (""), if you want to destroy the above variable, just write again:

SetCookie ("UserName", ""); This is often used as a safe exit. 3. The valid range of cookies and the effective range of the Survival Cookie (that is, this range of page can get this cookie variable) The default is the directory and its subdirectory, of course you can modify it with the setCookie Path and Domain parameters. If you don't set the cookie's Expire (see 1. Set an example in a cookie variable), then when you leave the website, Cookie is also automatically destroyed. Http://www.netscape.com/newsref/std/cookie_spec.html is the full introduction information provided by cookie original NetScape. Second, session use session variables, which is the session-level variable, which is the public variables that visitors exist throughout the website. When the client does not support it, it is possible not to support cookie (such as Lynx under Linux ... huh, it is a horrible point), in order to ensure that the data is correct, it needs to use session variables. SESSION is different in various web languages, PHP has also been supported after 4.0. First, let's take a look at a simple example: Test.php ----------- Test1.php ------ 1. Initial SESSION If the PHP settings automatic session is not turned on, you need to use the session_start () function to initialize a session, this function is as follows:

: boolean session_start (void); its role is to initialize a new session, if the customer is already in the session, even the original session. This function does not have a parameter, and the return value is TRUE. 2, register a variable in the session You want to save the variables using the following functions:

Boolean session_register (String Name); This function adds a variable to the current session in the global variable. The parameter name is the number of variables that want to join. Success returns a TRUE value. Then you can use the variable name directly to assign it, this value will be saved. 3, using the value of the session variable as shown in the above example, as long as you repeat two steps in the new page (except for assignment), you can use the session variable directly. 4, Session destruction If you just want to log out of a variable rather than destroying the entire variable, then you need to use the function:

Boolean session_unregister (String name); Usage is very simple, parameter name is the variable name to delete. Success returns a TRUE value. However, if you want to "destroy" session variables, such as security exit, use functions:

Boolean session_destroy (void); this function ends the current session. This function does not have a parameter, and the return value is TRUE. 5, other useful session function a, check if the variable registers boolean session_is_registered (String Name); this function checks if there is a specified variable registration in the current session. The parameter name is the number of variables that want to check. Success returns a TRUE value. b, return to the registered variable NULL

Void session_unset (void); This function can be set empty all Session variables that are registered. Note that it is not Unregister, which is different from destroy. The following example is a good description of this function.

Output: A: 1 - Reg: B: - Reg: 1 C: - Reg: 1! B |! C | C, customize your own session processing method

Void session_set_save_handler (String Open, String Close, String Read, String Write, String Destroy, String GC) This function defines the saving function of the user-level Session (open, off, write, etc.). For example, this function is useful when we want to save the session in a local database. By default, each session stores in a separate file in the system temporary directory (for example, in the UNIX system). This is suitable for or is not suitable, and it is based on your needs. For example: If your support of the PHP web server is distributed on a different machine, you can't easily share the session between them (of course, you can also save the sessions in NFS sharing). Another potential problem is that thousands or millions of session files on your machine make your file system scattered. Note: This function appears after the 4.0B4 version. Before using this function, first configure the php.ini file, session.save_hadler = user, otherwise, session_set_save_handler () will not take effect. Also, according to my test, if you want to use this session block across page, you have to join your own function and session_set_save_handler in each script file for session, so the best way is to make a separate Files, in each script to be used in the SESSION script to include incoming. The following example provides the most basic session save method, similar to the default Files method. This is also easy to do if you want to use the database.

Example: session_set_save_handler () example Posted on August 01, 2004 10:03 AM

转载请注明原文地址:https://www.9cbs.com/read-8288.html

New Post(0)