How to use PHP sessions to display current online users

Source: Internet
Author: User
Summary: php session refers to a mechanism in which a user is active during the period from entering the website to closing the website. It provides a public variable storage mechanism that is used by all webpages. So what is the usefulness of SESSION? Shopping cart is used for online shopping. you can add the items you bought to the shopping cart at any time and check out the items at the cashier. Summary throughout the process

Php session refers to a mechanism in which a user is active during the period from entering the website to closing the website. It provides a public variable storage mechanism used by all webpages. So what is the usefulness of SESSION? Shopping cart is used for online shopping. you can add the items you bought to the shopping cart at any time and check out the items at the cashier. During the whole process, the shopping cart has always played the role of temporarily storing the selected item and used it to track the user's activity on the website. this is the role of SESSION.

The invention of the SESSION fills in the limitations of the HTTP protocol. the HTTP protocol is considered as a stateless protocol. after the server completes the response, the server loses contact with the browser. The invention of session allows a user to save his information when switching between multiple pages.

PHP3 does not directly provide the session function. we can only implement it in other ways, such as using PHPLIB. If PHP4 is compared with PHP3, its biggest improvement is to provide SESSION.



Basic Session knowledge

To use the session, you must use PHP4.1 or later, and set register_globle = Off in php. ini to register_globle = On. In addition, session. cookie_path =/is not easy to change.

The session in PHP uses the client Cookie by default. When the client's Cookie is disabled, it is automatically passed through Query_String.



Php processes a total of 11 session functions. we will introduce several functions in detail.

1. session_start

Function: start a session or return an existing session.

Function prototype: boolean session_start (void );

Return value: Boolean

Function description: This function has no parameters and returns true. It is best to place this function first, and there cannot be any output before it, otherwise an alarm will be triggered, such as: Warning: cannot send session cache limiter-headers already sent (output started at/usr/local/apache/htdocs/cga/member/1.php: 2) in/usr/local/apache/htdocs/cga/member/1.php on line 3

2. session_register

Function: registers a new variable as a session variable.

Function prototype: boolean session_register (string name );

Return value: Boolean value.

Function description: This function adds a variable to the current SESSION in the global variable. the parameter name is the name of the variable to be added. if it succeeds, the logical value true is returned. You can use $ _ SESSION [name] or $ HTTP_SESSION_VARS [name] to set values or assign values.

3. session_is_registered

Function: checks whether a variable is registered as a session variable.

Function prototype: boobean session_is_registered (string name );

Return value: Boolean

Function description: This function checks whether a specified variable has been registered in the current session. the parameter name is the name of the variable to be checked. If the call succeeds, the logical value true is returned.

4. session_unregister

Function: Delete a registered variable.

Function prototype: boolean session_session_unregister (string name );

Return value: Boolean

Function description: This function deletes the variables in the global variables in the current session. The parameter name is the name of the variable to be deleted. if it is successful, true is returned.

5. Session_destroy

Function: ends the current session and clears all resources in the session.

Function prototype: boolean session destroy (void );

Return value: Boolean value.

Function description: This function ends the current session. this function has no parameters and returns true.



The functions described above will be used in the following sections, but there are also some functions related to the session:

6. session_encode

Function: sesssion information encoding

Function prototype: string session_encode (void );

Return value: string

Function description: the returned string contains the names and values of each variable in the global variable, in the form of a | s: 12: "it is a test"; c | s: 4: "lala"; a is the variable name s: 12 represents the value of variable a "it is a test the length is 12 variables are separated by semicolons.

7. session_decode

Function: decodes sesssion information.

Function prototype: boolean session_decode (string data)

Return value: Boolean

Function description: This function decodes session information. if the session information is successful, the logical value true is returned.

8. session_name

Function: Access the current session name.

Function prototype: boolean session_name (string [name]);

Return value: string

Function description: This function can obtain or reset the name of the current session. If the parameter name is not set, the current session name is obtained. if the parameter is added, the session name is set to the parameter name.

9. session_id

Function: Access the ID of the current session.

Function prototype: boolean session_id (string [id]);

Return value: string

Function description: This function can obtain or reset the ID number of the currently stored session. If no parameter id is specified, only the id of the current session is obtained. if a parameter is added, the id of the session is set to the new id.

10. session_unset

Function: delete all registered variables.

Function prototype: void session_unset (void)

Return value: Boolean

Function description: Unlike Session_destroy, this function does not end a session. Just like using the session_unregister function to cancel all session variables one by one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.