PHP session变量的作用是:存储用户的session信息,或者改变用户的session设置。Session变量储存了一个单一用户的信息,它可以被所有的页面使用。 PHP Session变量 当你在自己的计算机上运行一个应用程序时,你打开它,对他做一些改变,然后关闭它,这个过程
PHP session变量的作用是:存储用户的session信息,或者改变用户的session设置。Session变量储存了一个单一用户的信息,它可以被所有的页面使用。
PHP Session变量
当你在自己的计算机上运行一个应用程序时,你打开它,对他做一些改变,然后关闭它,这个过程和session很相似。计算机知道你是谁,知道你什么时候启动了应用程序,什么时候关闭了应用程序。但在互联网上,会出现这样一个问题:因为HTTP地址不可能永久的保留下来,所以服务器很难辨认你是谁,你在干什么。
PHP Session允许你在服务器上储存用户信息(如:用户名[username]、购物清单[shopping]等等),从而解决了这个问题。然而,session信息也是临时存在的,当你离开这个网站时,他会被自动删除。如果你想永久保留这些信息,你可以尝试着把它储存在数据库中。
Session通过为每个访问者创建一个独立的ID(UID)并储存基于UID的变量来运行。UID既存储在cookie中,又在URL中展现出来。
启动PHP Session
在你将用户信息存入PHP Session之前,你必须先启动Session。
上述代码将在服务器上注册一个用户的session,允许你储存用户信息,并为用户session指定一个UID。
储存一个Session变量
储存和获取session变量的最佳方法是使用PHP $_SESSION变量:
session_start();
// store session data
$_SESSION['views']=1;
?>
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
结果:
Pageviews=1
在上述案例中,我们建立了一个简易的页面计数器。Isset()函数检查“views”变量是否已经被设置。如果“views”变量已经被设置了,我们会增加我们的计数。如果“views”变量不存在,我们会先创建一个“views”变量,并把“1”赋给它。
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;echo "Views=". $_SESSION['views'];
?>
删除Session
如果你希望删除一些session数据,你可以使用unset()函数或session_destroy()函数。
Unset()函数的作用是释放指定的session变量:
?>
你也可以使用session_destroy()函数将session全部删除:
session_destroy();
?>
注意:session_destroy()会重新设置你的session,你会丢失所有已保存的session数据。

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

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!
