如何利用PHP会话显示出当前在线的用户_PHP
综述
PHP会话即SESSION是指用户从进入网站到关闭网站这段时间内活动的一种机制,它提供了所有网页都共同使用的公共变量存贮机制。那么SESSION到底有什么用处呢?网上购物时大家都用过购物车,你可以随时把你选购的商品加入到购物车中,最后再去收银台结帐。在整个过程中购物车一直扮演着临时存贮被选商品的角色,用它追踪用户在网站上的活动情况,这就是SESSION的作用。
SESSION的发明填补了HTTP协议的局限,HTTP协议被认为是无状态协议,当它在服务端完成响应之后,服务器就失去了与该浏览器的联系。会话的发明使得一个用户在多个页面间切换时能够保存他的信息。
在PHP3版本未直接提供session功能,我们只能用其它办法来实现,比如用PHPLIB。如果说PHP4与PHP3相比,它最大的进步就是提供了SESSION。
Session基础知识
要使用session需要PHP4.1以上的版本,而且需要把php.ini中的register_globle=Off设成register_globle=On。另外,session.cookie_path = / 这行不易更改。
PHP中的session默认情况下是使用客户端的Cookie。当客户端的Cookie被禁用时,会自动通过Query_String来传递。
Php处理会话的函数一共有11个,我们详细介绍一下将要用到几个函数。
1、 session_start
函数功能:开始一个会话或者返回已经存在的会话。
函数原型:boolean session_start(void);
返回值:布尔值
功能说明:这个函数没有参数,且返回值均为true。最好将这个函数置于最先,而且在它之前不能有任何输出,否则会报警,如: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
函数功能:登记一个新的变量为会话变量
函数原型:boolean session_register(string name);
返回值:布尔值。
功能说明:这个函数是在全局变量中增加一个变量到当前的SESSION中,参数name就是想要加入的变量名,成功则返回逻辑值true。可以用$_SESSION[name]或$HTTP_SESSION_VARS[name]的形式来取值或赋值。
3、 session_is_registered
函数功能:检查变量是否被登记为会话变量。
函数原型:boobean session_is_registered(string name);
返回值:布尔值
功能说明:这个函数可检查当前的session之中是否已有指定的变量注册,参数name就是要检查的变量名。成功则返回逻辑值true。
4、 session_unregister
函数功能:删除已注册的变量。
函数原型:boolean session_session_unregister(string name);
返回值:布尔值
功能说明:这个函数在当前的session之中删除全局变量中的变量。参数name就是欲删除的变量名,成功则返回true.
5、 Session_destroy
函数功能:结束当前的会话,并清空会话中的所有资源。
函数原型:boolean session destroy(void);
返回值:布尔值。
功能说明:这个函数结束当前的session,此函数没有参数,且返回值均为true
上面介绍函数下文将会用到,但还有一些有关session的函数也介绍一下:
6、 session_encode
函数功能:sesssion信息编码
函数原型:string session_encode(void);
返回值:字符串
功能说明:返回的字符串中包含全局变量中各变量的名称与值,形式如:a|s:12:"it is a test";c|s:4:"lala"; a是变量名 s:12代表变量a的值"it is a test的长度是12 变量间用分号”;”分隔。
7、 session_decode
函数功能:sesssion信息解码
函数原型:boolean session_decode (string data)
返回值:布尔值
功能说明:这个函数可将session信息解码,成功则返回逻辑值true
8、 session_name
函数功能:存取当前会话名称
函数原型:boolean session_name(string [name]);
返回值:字符串
功能说明:这个函数可取得或重新设置当前session的名称。若无参数name则表示获取当前session名称,加上参数则表示将session名称设为参数name
9、 session_id
函数功能:存取当前会话标识号
函数原型:boolean session_id(string [id]);
返回值:字符串
功能说明:这个函数可取得或重新设置当前存放session的标识号。若无参数id则表示只获取当前session的标识号,加上参数则表示将session的标识号设成新指定的id
10、 session_unset
函数功能:删除所有已注册的变量。
函数原型:void session_unset (void)
返回值:布尔值
功能说明:这个函数和Session_destroy不同,它不结束会话。就如同用函数session_unregister逐一注销掉所有的会话变量。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Recently, Samsung Display and Microsoft signed an important cooperation agreement. According to the agreement, Samsung Display will develop and supply hundreds of thousands of OLEDoS panels for mixed reality (MR) head-mounted devices to Microsoft. Microsoft is developing an MR device for multimedia content such as games and movies. This device is expected to It will be launched after the OLEDoS specifications are finalized, mainly serving the commercial field, and is expected to be delivered as early as 2026. OLEDoS (OLED on Silicon) technology OLEDoS is a new display technology that deposits OLED on a silicon substrate. Compared with traditional glass substrates, it is thinner and has higher pixels. OLEDoS display and ordinary display

What is GateToken(GT) currency? GT (GateToken) is the native asset on the GateChain chain and the official platform currency of Gate.io. The value of GT coins is closely related to the development of Gate.io and GateChain ecology. What is GateChain? GateChain was born in 2018 and is a new generation of high-performance public chain launched by Gate.io. GateChain focuses on protecting the security of users' on-chain assets and providing convenient decentralized transaction services. GateChain's goal is to build an enterprise-level secure and efficient decentralized digital asset storage, distribution and transaction ecosystem. Gatechain has original

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
