转载自:http://www.tutorialspoint.com/php/php_sessions.htm

An alternative way to make data accessible across the various pages of an entire website is to use a PHP Session.

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.

The location of the temporary file is determined by a setting in the php.ini file calledsession.save_path. Bore using any session variable make sure you have setup this path.

When a session is started following things happen:

  • PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers such as 3c7foj34c3jj973hjkop2fc937e3443.

  • A cookie called PHPSESSID is automatically sent to the user's computer to store unique session identification string.

  • A file is automatically created on the server in the designated temporary directory and bears the name of the unique identifier prefixed by sess_ ie sess_3c7foj34c3jj973hjkop2fc937e3443.

When a PHP script wants to retrieve the value from a session variable, PHP automatically gets the unique session identifier string from the PHPSESSID cookie and then looks in its temporary directory for the file bearing that name and a validation can be done by comparing both values.

A session ends when the user loses the browser or after leaving the site, the server will terminate the session after a predetermined period of time, commonly 30 minutes duration.

Starting a PHP Session

A PHP session is easily started by making a call to the session_start() function.This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.

Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.

The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.

Make use of isset() function to check if session variable is already set or not.

Put this code in a test.php file and load this file many times to see the result:

<?phpsession_start();if(isset($_SESSION['counter'])){$_SESSION['counter']+=1;}else{$_SESSION['counter']=1;}$msg="You have visited this page ".$_SESSION['counter'];$msg.="in this session.";?><html><head><title>Setting up a PHP session</title></head><body><?php  echo($msg);?></body></html>

Destroying a PHP Session

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

Here is the example to unset a single variable:

<?phpunset($_SESSION['counter']);?>

Here is the call which will destroy all the session variables:

<?phpsession_destroy();?>

Turning on Auto Session

You don't need to call start_session() function to start a session when a user visits your site if you can set session.auto_start variable to 1 in php.ini file.

Sessions without cookies

There may be a case when a user does not allow to store cookies on their machine. So there is another method to send session ID to the browser.

Alternatively, you can use the constant SID which is defined if the session started. If the client did not send an appropriate session cookie, it has the form session_name=session_id. Otherwise, it expands to an empty string. Thus, you can embed it unconditionally into URLs.

The following example demonstrates how to register a variable, and how to link correctly to another page using SID.

<?phpsession_start();if(isset($_SESSION['counter'])){$_SESSION['counter']=1;}else{$_SESSION['counter']++;}?>$msg = "You have visited this page ".  $_SESSION['counter'];$msg .= "in this session.";echo ( $msg );<p>To continue  click following link<br/><a  href="nextpage.php?<?php echo htmlspecialchars(SID);>">
</p>

The htmlspecialchars() may be used when printing the SID in order to prevent XSS related attacks.

PHP - Sessions相关推荐

  1. Exception loading sessions from persistent storage

    严重: Exception loading sessions from persistent storage java.io.EOFException 删除Tomcat里面的work/Catalina ...

  2. Rsession: R sessions wrapping for Java

    2019独角兽企业重金招聘Python工程师标准>>> Rsession: R sessions wrapping for Java https://github.com/yannr ...

  3. Express.js 中的 Sessions 如何工作?(译)

    世界上任何一个拥有用户数据的 web 应用都必须处理 sessions.作为一名开发者,我们必须要知道它们是什么以及如何处理它们. 在这篇文章中,我想要分享的是: session 是什么? sessi ...

  4. Oracle sessions,processes 和 transactions 参数 关系 说明

    一.官网说明 1.1 processes 11gR2 的文档: Property Description Parameter type Integer Default value 100 Modifi ...

  5. Load Balance Tomcat with Nginx and Store Sessions in Redis--reference

    An awkward title, but that's exactly what we're going to do. For some time, I was looking for a way ...

  6. PROCESSES, SESSIONS和CONNECTIONS的区别

    From:AskTom You Asked Hi Tom What's the difference between connections, sessions and processes? I re ...

  7. 保存对象报错with two open Sessions

    purorderService.save(newpur); 出现如下 org.springframework.orm.hibernate3.HibernateSystemException: ille ...

  8. Tomcat : IOException while loading persisted sessions: java.io.EOFException

    严重: IOException while loading persisted sessions: java.io.EOFException 严重: Exception loading session ...

  9. ORA-00018: maximum number of sessions exceeded 超出最大会话数

    ORA-00018: maximum number of sessions exceeded ORA-00018: 超出最大会话数 Cause:       All session state obj ...

  10. SESSIONS.ser 的问题

    网络上的解决方案: (1) Tomcat\work\Catalina\localhost\工程名\SESSIONS.ser session未超时的情况下服务器关闭大的时候被序列化为工程名\SESSIO ...

最新文章

  1. 理论+实操: linux中firewalld防火墙基础讲解(转载)
  2. 面试题: mysql 数据库已看 sql安全性 索引 引擎 sql优化
  3. 【小白学习C++ 教程】四、C++逻辑运算符、While循环和For 循环
  4. 前端学习(2863):简单秒杀系统学习之优化cookie
  5. 不安装cudnn可不可以_关于CUDA和cuDNN的安装
  6. Python虚拟环境你会用了吗?手把手教你
  7. cocos2d-x 3.0 画图节点——Node
  8. python去除视频中的logo_Python音视频开发:消除抖音短视频Logo和去电视台标的实现详解...
  9. 如何在Python中从stdin读取
  10. 使用W3C标准的DOM处理XML文件[C++和Java版]
  11. php sspi应用,SPI串行通信模块应用(一)
  12. 关于哔哩哔哩视频,音频合并(使用ffmpeg批量处理)
  13. 2020 用html jQuery实现广告轮播图自动切换 滚动页面 鼠标悬浮下标且左右切换图片
  14. 证件照换底色·网页图片设计·第一次项目技术总结
  15. 华三防火墙web端口_设置H3C SecPath F100 系列防火墙的web访问
  16. 电商平台订单号生成策略
  17. win10 SystemParametersInfo 设置屏保 不好使_Acer 暗影骑士 N50-N92电脑如何重装win10?win10专业版系统重装教程...
  18. QT: 基于QT和嵌入式的视频监控系统
  19. Noteability的一些使用小技巧
  20. 智联卓聘IM演进过程

热门文章

  1. 测试网络SNMP连接的几个方法(我平时调试SNMP程序时用到的几个解决方案)
  2. vim编辑多文件,多窗口,切换窗口,切换文件命令
  3. mysql记录锁、间隙锁、临键锁
  4. 国际短信平台接口调用的方法步骤,简单5步快速教程
  5. JS笔记_P72数组的四个主要方法
  6. Android简单计时器详解(Timer)
  7. 手摸手带你学移动端WEB开发
  8. MacOS 苹果系统下 OBS黑屏(屏幕录制黑屏)解决办法
  9. 【JAVA】:java中getTime函数
  10. [Elasticsearch]4.可伸缩性解密:集群、节点和分片