什么是 PHP session_start() [英] What is PHP session_start()

查看:37
本文介绍了什么是 PHP session_start()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是否基于 cookie 启动当前会话?从 PHP 网站上得到的.PHP 如何控制会话?如果我在用户打开我的登录页面时开始会话,我什至使用该会话做什么?我可以使用当前会话获取有关登录用户的信息吗?

Does it start a current session based on cookies? Got that from the PHP website. How does PHP control the session? If I start a session when a user opens up my login page, what do I even use that session for? Can I use the current session to get info about the logged in user?

推荐答案

PHP 会话系统允许您将数据安全地存储在 $_SESSION 全局数组中.一个典型的例子是当用户输入密码时将用户的标识符存储在会话中:

The PHP session system lets you store securely data in the $_SESSION global array. A typical example is to store the user's identifier in the session when they type in their password:

if ($user = try_login($login, $password)) 
  $_SESSION['user'] = $user;

然后,您可以在所有其他页面上访问该信息:

Then, you can access that information on all other pages:

if (isset($_SESSION['user']))
  // logged in !
  echo user_name($_SESSION['user']);

数据存储在服务器上,因此不存在篡改风险(另一方面,请注意您的磁盘使用情况).

The data is stored on the server, so there is no risk of tampering (on the other hand, mind your disk usage).

启动会话让当前请求使用 $_SESSION.如果这是用户的第一次访问,该数组将为空,并且会为您发送一个新的会话 cookie.

Starting the session lets the current request use $_SESSION. If this is the user's first visit, the array will be empty and a new session cookie will be sent for you.

关闭会话只会阻止当前请求使用 $_SESSION,但数据会保留用于下一个请求.

Closing the session merely prevents the current request from using $_SESSION, but the data stays around for the next requests.

销毁会话会永远丢弃所有数据.会话在上次访问后的一段时间内(通常约为 30 分钟)被销毁.

Destroying the session throws away all the data, forever. The sessions are destroyed a certain duration after the last visit (usually around 30 minutes).

这篇关于什么是 PHP session_start()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆