php session会话使用方法详解
本文章来给各位php 初学者介绍一篇关于php中全局变量session会话的基本使用方法,有需要了解的朋友可进入参考参考。
Session是什么
Session是指有始有终的一系列动作或信息,比如去看电影从进入影院到走出影院的整个过程可以称为一个Session。Cookie将数据存储在客户端计算机上,而Session将不定量的变量存储在服务器端。
每一个用户链接网站服务器后便会产生一个Session,所以首先要告诉服务器使用Session功能来存储数据。启用Session功能的方式有两种:
1、使用session_start()函数启动会话,其结构形式如下:
session_start()
函数不能带参数,其返回值为布尔型。
2、在php.ini中设置参数 session.auto_start,将此参数设为1,即当有连接到服务器后Session功能将自动启用。
启动Session后,在使用Session变量之前首先要注册变量,之后才能使用,反之不使用某个变量时就要取消注册。Session_register(变量名)可以注册一个Session变量;Session_unregister(变量名)可以取消注册一个Session变量,例如:
代码如下 | 复制代码 |
session_start(); /* 启动session变量 */ |
调用session变量
php session使用方法:用函数$_SESSION[]可以创建一个函数变量,其格式如下:
代码如下 | 复制代码 |
$_SESSION['$string']=$str; |
参数$string是要定义的变量名,$str是变量值。在调用时使用$_SESSION[$string]的格式。
实例:
代码如下 | 复制代码 |
session_start(); |
session的使用
本站前面介绍了session的一些概念以及基本操作,这里通过实例介绍php session使用的方法。
实例代码:
代码如下 | 复制代码 |
session_start(); /* 启动session */ |
session.php代码:
代码如下 | 复制代码 |
session_start(); echo "用户名是:" .$_COOKIE['name'] ." "; echo "密码是:" .$_SESSION['password'] ." "; echo "访问时间是:" .date('Y m d H:i:s', $_SESSION['time']) ." "; ?> |
获取session_id的值
每一个session都有会被指定一个特定的id,用session_id来标识。我们可以通过session_id()函数查看当前session_id的值,其结构形式如下:
session_id()
实例:
代码如下 | 复制代码 |
session_start(); /* 启动session */ |
cookie与session的区别
session和cookie都是重要的会话管理应用,在许多的php开发项目都会用到。它们的作用是能够给不同的用户创建不同的值,从而让不同的用户返回的结果都不相同。
那么session cookie区别在哪里呢?
cookie是利用客户端来创建,当用户用浏览器打开网站时就会给用户创建一个cookie,这个过程是由用户的电脑执行的,网站的服务器只是发送一些指令。因此如果用户禁止了cookie功能,则无法利用cookie来实现相关功能。
session则是利用服务端来创建,整个过程都由服务器来执行,用户无权干预。
在运用时,如果你希望节约服务器的资源,可以选用cookie来进行会话管理;如果你希望所有的浏览者都能使用会话管理,则应该选用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

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



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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

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.

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.
