使用原会话_PHP
一个被大家长期所期待的PHP 4.0特性是会话支持。相比之下,PHP 3.0用户则不得不使用第三方库或根本不能实现此项功能。而缺乏会话支持则正是PHP最受人批评的地方。不过,从早期测试版的发布开始,会话支持就成为PHP 4.0的一部分,因此这个问题也得到了消除。
你可以通过会话以在用户访问网络站点期间维持用户特定变量,而无须设置多个cookie、使用隐蔽表单域,或在你可能经常要连结的一个数据库内存储信息等。
在一个页面上启动会话即告之PHP引擎要么是要开始一个会话(如果之前没有启动)或是继续当前会话:
session_start();
启动一个会话将通过cookie向用户发送一个认证字符串(如940f8b05a40d5119c030c9c7745aead9);而在服务器端则会创建一个同名的相映临时文件,如sess_940f8b05a40d5119c030c9c7745aead9。此文件包含了注册会话变量及其值。
显示会话动作最常见的例子是访问计数器:
启动你的PHP模块,并完全确保PHP代码是文件的第一行:无空白、无HTML输出等任何代码。原因是会话函数发出一个文件头时,如在调用session_start()函数之前发送了空白或者HTML输出,系统将会出错。
//如果会话对用户并不存在,启动一新会话
session_start();
接下来,注册count变量。
session_register('count');
注册变量即告之PHP:只要在会话存在时,一个名叫count的变量也就存在。目前此变量还没有赋值。不过,如果你对其加1,则值即可被赋为1:
$count++;
把以上各点一并考虑,你已经完成以下工作:启动了一个会话(如果先前没有)、为某个用户分配了会话标识(如果不存在会话标识)、注册了名为count的变量并把$count加1以表示此次为用户首次访问页面:
要显示用户在当前会话中已经访问页面次数,只需要输出$count的值:
echo "
You've been here $count times.
";整个访问计数器代码如下:
session_start();
session_register('count');
$count++;
echo "
You've been here $count times.
";?>
如果重新装载以上脚本,可以发现计数值增加。这的确令人激动。
同时可以在会话中注册数组。假设你已经有一个名为$faves的数组:
$faves = array ('chocolate','coffee','beer','linux');
可以象其他单个变量一样注册该数组:
session_register('faves');
应用数组同应用其他单独变量相同,如$faves。如果用户希望在Web站点的一个页面上现场展示其爱好,则可以在名为$faves会话变量中注册用户爱好,并在另一页面输出这些值:
session_start();
echo "My user likes:
- ";
- $v"; }
echo "
while (list(,$v) = each ($faves)) {
echo "
?>
这样即可得到一个显示用户爱好的美观简洁的列表。
会话变量值不能被查询字符串所覆盖,也就是说,不能通过键入http:///www.yourdomain.com/yourscript.php?count=56 来为注册会话变量$count赋予新值。这一点对安全而言是至关重要:你只能在服务器端通过脚本修改或者删除(未注册的)会话变量。
如果希望完全删除某个会话变量,应从系统中取消注册该变量:
session_unregister('count');
要完全删除某会话,如按下Logout按钮,则下列脚本比较简洁:
session_destroy();
使用会话来存储变量值减轻了数据库连接负载,并避免了噩梦般复杂代码的编写,以及用大量隐私声明以解释为何在访问过程中向用户发送多达50份cookie。而现在仅仅需要一个cookie、一个变量就可以了——如同一颗水珠折射出整个世界——没有比这更简单的了!

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



With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

As a platform integrating social networking and e-commerce, Xiaohongshu has attracted more and more users to join. Some users hope to register multiple accounts to better experience interacting with Xiaohongshu. So, how to register multiple accounts on Xiaohongshu? 1. How to register multiple accounts on Xiaohongshu? 1. Use different mobile phone numbers to register. Currently, Xiaohongshu mainly uses mobile phone numbers to register accounts. Users sometimes try to purchase multiple mobile phone number cards and use them to register multiple Xiaohongshu accounts. However, this approach has some limitations, because purchasing multiple mobile phone number cards is cumbersome and costly. 2. Use email to register. In addition to your mobile phone number, your email can also be used to register a Xiaohongshu account. Users can prepare multiple email addresses and then use these email addresses to register accounts. but

Xiaohongshu, a social platform integrating life, entertainment, shopping and sharing, has become an indispensable part of the daily life of many young people. So, how to register a Xiaohongshu account? 1. How to register a Xiaohongshu account? 1. Open the Xiaohongshu official website or download the Xiaohongshu APP. Click the "Register" button below and you can choose different registration methods. Currently, Xiaohongshu supports registration with mobile phone numbers, email addresses, and third-party accounts (such as WeChat, QQ, Weibo, etc.). 3. Fill in the relevant information. According to the selected registration method, fill in the corresponding mobile phone number, email address or third-party account information. 4. Set a password. Set a strong password to keep your account secure. 5. Complete the verification. Follow the prompts to complete mobile phone verification or email verification. 6. Perfect the individual

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

As one of the most popular lifestyle sharing platforms in the world, Xiaohongshu has attracted a large number of users. So, how to register a Xiaohongshu account? This article will introduce you to the Xiaohongshu account registration process in detail, and answer the question of how to recover Xiaohongshu account abnormalities. 1. How to register a Xiaohongshu account? 1. Download the Xiaohongshu APP: Search and download the Xiaohongshu APP in the mobile app store, and open it after the installation is complete. 2. Register an account: After opening the Xiaohongshu APP, click the "Me" button in the lower right corner of the homepage, and then select "Register". 3. Fill in the registration information: Fill in the mobile phone number, set password, verification code and other registration information according to the prompts. 4. Complete personal information: After successful registration, follow the prompts to complete personal information, such as name, gender, birthday, etc. 5. Settings

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

qooapp is a software that can download many games, so how to register an account? Users need to click the "Register" button if they don't have a pass yet, and then choose a registration method. This account registration method introduction is enough to tell you how to operate it. The following is a detailed introduction, so take a look. How to register a qooapp account? Answer: Click to register, and then choose a registration method. Specific methods: 1. After entering the login interface, click below. Don’t have a pass yet? Apply now. 2. Then choose the login method you need. 3. You can use it directly after that. Official website registration: 1. Open the website https://apps.ppaooq.com/ and click on the upper right corner to register. 2. Select registration

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you
