search
HomeBackend DevelopmentPHP Tutorial如何利用PHP会话显示出当前在线的用户_PHP

如何利用PHP会话显示出当前在线的用户_PHP

Jun 01, 2016 pm 12:34 PM
functionuseFunctionvariableonlinehowshowuserreturn

综述

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逐一注销掉所有的会话变量。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft