php开发之session的高级应用
1,Session 临时文件
在服务器中,如果将用户所有的Session都保存到临时目录中,会降低服务器的安全性和效率。打开服务器存储的站点会非常的慢。
使用php函数session_save_path()函数存储Session临时文件,可以缓解因临时文件的存储导致的服务器效率降低和站点打开缓慢的问题。
示例代码如下:
<?php $path ="./tmp/"; //设置session存储的路径 session_save_path($path); session_start(); $_SESSION['userName']=true; ?>
注意
session_save_path() 必须在session_start() 之前执行。
2,Session 缓存
Session 缓存是将网页中的内容临时存储到IE客户端的Temporary INternet Files文件夹,并且可以设置缓存的时间。
Session的缓存使用的是 session_cache_limiter()函数,其语法如下:
string session_cache_limiter([string cache_limiter]);
其中参数cache_limiter 为public 或private 。同事session不是在服务器端,而是在客户端。在服务器中没有显示。
缓存时间的设置,使用的是函数 session_cache_expire()语法如下:
int session_cache_expire([int new_cahche_expire]);
参数new_cahche_expire 是session缓存的时间数字,单位分钟。
注意:
这两个session函数必须在session_start()函数之前执行
session 缓存页面的示例代码如下:
<?php session_cache_limiter("private"); $cache_limit =session_cache_limiter(); //开启客户端缓存 echo "缓存限制为:".$cache_limit."\n"; session_cache_expire(30); $cache_expire = session_cache_expire(); //设定客户端缓存时间 echo "客户端缓存时间为:".$cache_expire."分钟\n"; session_start(); ?>
运行结果如下:
3,Session数据库存储
在php中Session 的数据库存储主要是通过 session_set_save_handler()函数来实现的。 具体语法如下:
bool session_set_save_handler(string open,string close,string read,string write,string destroy,string gc);
下面分别将这6个参数(函数)分装起来,在学习完面向对象编程后,大家会有一个更加清晰的认识。
(1) 封装session_open()函数,代码如下:
function _session_open($save_path,$session_name){global $handle;$handle =mysql_connect('localhost','root','root')or die('数据库连接失败!');mysql_select_db('db_database11',$handle)or die('数据库不存在');return(true);}
(2)封装session_close()函数,代码如下:
function _session_close(){global $handle;mysql_close($handle);return(true);}
(3) 封装 session_read()函数,在函数中设定当前时间的UNIX时间戳,根据$key查找Session名片及内容。代码如下:
function _session_read($key){golbal $handle; //全局变量$handle 连接数据库$time =time(); //设定当前时间$sql ="select session_data from tb_session where session_key = '$key' and session_time>'$time'";$result =mysql_query($ssql,$handle);$row =mysql_fetch_array($result);if($row){return($row['session_data']);}else{return(false);}}
(4) 封装session_write()函数,函数设定Session的失效时间,查找到Session的名称及内容,如果查询结果为空。则将页面中Session根据session_id,session_name,失效时间,插入数据库中。如果查询结果不为空,则根据 $key修改数据库中Session存储信息。代码如下:
function _session_write($key,$data){global $handle;$time = 60*60;$lapse_time =time()+$time; //得到UNIX时间戳$sql = "select session_data from tb_session where session_key ='$key' and session_time>$lapse_time";$result =mysql_query($sql,$handle);if(mysql_num_rows($result)==0){ //没有结果$sql ="insert into tb_session values('$key','$data',$lapse_time)";$result =mysql_query($sql,$handle);}else{$sql ="update tb_session set session_key='$key',session_data ='$data',session_time =$lapse_time where session_key ='$key'";$result =mysql_query($sql,$handle);}return($result);}
(5) 封装session_destroy(),根据$key删除数据库中的Sessin.代码如下:
function _session_destroy(){global $handle;$sql ="delete from tb_session where session_key ='$key'";$result =mysql_query($sql,$handle);}
(6)封装session_gc(),根据Session的失效时间删除过期的Session,示例代码如下:
functin _session_gc($expiry_time){global $handle;$sql ="delete from tb_session where session_expiry_time<$expiry_time";$result =mysql_query($sql,$handle);return($result);}
具体的代码运行就不做了,等到学完面向对象的编程的时候给大家演示下哦。

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

Alipay PHP...

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
