当服务器创建了一个session(session_start()),服务器将会在服务器的指定文件夹下创建一个session文件,其名称为sessionID,并当做cookie的值发送给浏览器。浏览器每次访问该服务器时便会带着这个cookie,服务器便会识别改sessionID,找到相应的session文件。该文件中存放了若干键值对。该session文件所存放的文件夹可以在配置文件php,ini中修改。
cookie 每次请求页面的时候进行验证,如果用户信息存储在数据库中,每次都要执行一次数据库查询,给数据库造成多余的负担。cookie可以被修改的,所以安全系数太低。
session是存储在服务器端面的会话,相对安全,并且不像Cookie那样有存储长度限制。由于Session是以文本文件形式存储在服务器端的,所以不怕客户端修改Session内容。实际上在服务器端的Session文件,PHP自动修改session文件的权限,只保留了系统读和写权限,而且不能通过ftp修改,所以安全得多。
对于 Cookie 来说,假设我们要验证用户是否登陆,就必须在 Cookie 中保存用户名和密码(可能是 md5 加密后字符串),并在每次请求页面的时候进行验证。
如果用户名和密码存储在数据库,每次都要执行一次数据库查询,给数据库造成多余的负担。因为我们并不能 只做一次验证。为什么呢?
因为客户端 Cookie 中的信息是有可能被修改的。假如你存储 $admin 变量来表示用户是否登陆,$admin 为 true 的时候表示登陆,为 false 的时候表示未登录,在第一次通过验证后将 $admin 等于 true 存储在 Cookie,下次就不用验证了,这样对么?错了,假如有人伪造一个值为 true 的 $admin 变量那不是就立即取的了管理权限么?非常的不安全。
而 Session 就不同了,Session 是存储在服务器端的,远程用户没办法修改 Session 文件的内容,因此我们可以单纯存储一个 $admin 变量来判断是否登陆,首次验证通过后设置 $admin 值为 true,以后判断该值是否为 true,假如不是,转入登陆界面,这样就可以减少很多数据库操作了。
而且可以减少每次为了验证 Cookie 而传递密码的不安全性了(Session 验证只需要传递一次,假如你没有使用 SSL 安全协议的话)。即使密码进行了 md5 加密,也是很容易被截获的。
session 会话会为每一个开启了 session 会话的访问者建立一个唯一的会话 ID ,用于识别用户。该会话 ID 可能存储于用户电脑的 cookie 内,也可能通过 URL 来传递。而对应的具体 session 值会存储于服务器端,这也是与 cookie 的主要区别,并且安全性相对较高。
创建 session
要创建 session 或返回已经存在的会话 ,就必须先使用 session_start() 函数开启一个 session 会话,系统会分配一个会话 ID:
<?php session_start();//此函数没有参数,且返回值均为true。最好将这个函数置于最先,而且在它之前不能有任何输出,否则会报警 ?>
注册session变量
使用 session_register() 函数注册一个 session 变量,成功返回 TRUE ,否则返回 FALSE 。
语法:bool session_register( mixed name [, mixed ...] )
使用 session_register() 函数可以在目前会话下注册一个或多个全局 session 变量。 参数name就是想要加入的变量名,成功则返回逻辑值true。可以用$_SESSION[name]或$HTTP_SESSION_VARS[name]的形式来取值或赋值。
例子:
<?php session_start(); $username = "nostop"; session_register("username"); ?>
在该例子中,我们向 session 注册了一个名为 username 的变量,其值为 nostop 。
读取 session
PHP 内置的 $_SESSION 变量可以很方便的访问设置的 session 变量。
例子:
<?php session_start(); echo "登记的用户名为:".$_SESSION["username"]; //输出 登记的用户名为:nostop ?>
销毁 session
session_unregister() 注销单个 session 变量
unset($_SESSION['age']); 用于注销以$_SESSION['age']注册的session变量
session_unset() 删除所有已注册的变量
session_destroy() 注销所有的session变量,并注销整个 session 会话
例子:
<?php session_start(); session_unregister("username"); //注销 session 某个变量 session_unset(); //注销 session 会话 ?>
检查变量是否被登记为会话变量
session_is_registered
语法:boobean session_is_registered(string name);
这个函数可检查当前的session之中是否已有指定的变量注册,参数name就是要检查的变量名。成功则返回逻辑值true。
例子:
<?php session_start(); if(!session_is_registered("gender")){ //判断当前会话变量是否注册 session_register("gender"); //注册变量 } $gender="女"; echo $_SESSION['gender']; //女 ?>
存取当前会话名称
session_name
语法:boolean session_name(string [name]);
这个函数可取得或重新设置当前session的名称。若无参数name则表示获取当前session名称,加上参数则表示将session名称设为参数name。
例子:
<?php $sessionName = session_name(); //取得当前 Session 名,默认为 PHPSESSID $sessionID = $_GET[$sessionName]; //取得 Session ID session_id($sessionID); //使用 session_id() 设置获得的 Session ID ?>
存取当前会话标识号
session_id
语法:boolean session_id(string [id]);
这个函数可取得或重新设置当前存放session的标识号。若无参数id则表示只获取当前session的标识号,加上参数则表示将session的标识号设成新指定的id。
设置 Session 的生存期
setcookie:向客户端发送一个 HTTP cookie。
<?php session_start // 保存一天 $lifeTime = 24 * 3600; setcookie(session_name(), session_id(), time() + $lifeTime, "/"); ?>
session_set_cookie_params:设置 Session 的生存期的,该函数必须在 session_start() 函数调用之前调用。
如果客户端使用 IE 6.0 , session_set_cookie_params(); 函数设置 Cookie 会有些问题,所以我们还是手动调用 setcookie 函数来创建 cookie。
// 保存一天
<?php $lifeTime = 24 * 3600; session_set_cookie_params($lifeTime); session_start(); $_session["admin"] = true; ?>
设置 Session 文件的保存路径
session_save_path() :必须在 session_start() 函数调用之前调用。
<?php // 设置一个存放目录 $savePath = "./session_save_dir/"; // 保存一天 $lifeTime = 24 * 3600; session_save_path($savePath); session_set_cookie_params($lifeTime); session_start(); $_session["admin"] = true; ?>
<?php session_start(); //启动Session $username='nostop'; session_register('username'); //注册一个名为username变量 echo '登记的用户:'.$_SESSION['username']; //登记的用户:nostop 读取Session变量 $_SESSION['age']=23; //声明一个名为age的变量,并赋值 echo '年龄:'.$_SESSION['age']; //年龄:23 session_unregister('username'); //注销Session变量 echo $_SESSION['username']; //空 echo $_SESSION['age'];//23 unset($_SESSION['age']); //注销Session变量 echo '登记的用户:'.$_SESSION['username']; //空 echo '年龄:'.$_SESSION['age']; //空 ?>

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.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function