php基础知识(三)-会话使用
php基础知识(3)-会话使用
php基础知识-会话使用
示例代码下载页http://xieye.iteye.com/blog/1336095(在附件)
会话是指:服务器对同一个用户在一段时间内的各种连接的识别。
会话原理,有多种,但实际上主要是cookie。
cookie原理:web协议规定,服务器返回请求时可以将一小部分数据存在浏览器客户端,浏览器在下次往同域名服务器请求信息时,会
同时自动把该段信息再发送给服务器,而这一切是对用户透明的,用户不需要理解这些东西可以照常上网。
cookie的一个特点是,服务器存客户端时可以带有时间参数,浏览器会根据这个参数决定cookie是否过期来处理,如果过期了,这个cookie就
不会到服务端。
cookie的另一个特点是多个名称和值的组合。
php实现会话的机制:
php会话开启有两种途径:
一种设置php.ini里的大约1014行
session.auto_start = 0;
改为1的话,会话会自动开启。
第二种方法是使用session_start()函数。
在现实应用中,一般都取消配置文件中的自动开启,而手动使用函数开启会话。
开启了会话之后,
假如客户端没有传来一个称为PHPSESSID的cookie的话,那么服务端会在返回时自动发送该名称的cookie给用户,值是随机的一串乱码
并在服务器的会话目录中建一个文件,名字是名为PHPSESSID的cookie的值,内容为空。
假如客户端传来一个名为PHPSESSID的cookie,则服务器会按照该cookie的值到会话目录中找到对应文件,并把值取出放进会话全局数组中,
程序可以随意读取。
可以简单理解为php会话是以(名为phpsessid的cookie的值)这个文件名对应的文件里的内容。
对于php程序开发的网站,在不做特殊修改php配置的情况下,
用户把浏览器关闭,再打开,一般来说会生成新的会话。原因是php发送的cookie默认浏览器关闭则cookie过期
程序还可以随意的修改会话里的内容。
会话应用,登录,注册等。
例子1:展示,不使用数据库,使用会话来记住客户在网站中留下的痕迹。
http://localhost/command/peixun/session/6.php
可以点击文章查看,点击任意文章(例子就两篇)查看后,再点击返回,
可以发现系统能够记录用户查看过的文章,使用的是会话。
说明:如果浏览器关闭,就不行了,如果想长期保存,则需要用户登录机制,且保存在数据库。
存cookie方案不推荐,因为cookie容量最多2K,实在是太小了。
代码可下载
6.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
7.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
8.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|

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

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
