PHP会话,为什么多次需要session_start()? [英] PHP session, why is session_start() required multiple times?

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

问题描述

我正在编写一个Web应用程序,它将一个页面中的POST数据保存到会话中,然后重定向到另一页面以利用创建的会话信息.这是在我阅读完有关处理数据和显示数据的正确方法之后,将它们分成两个不同的脚本,以免遇到冗余的$ _POST数据问题.也就是说,不要让$ _POST在每次页面刷新时都将相同的数据发送到服务器.

I am writing a web application that saves POSTed data to a session in one page, then redirects to another page to utilize the created session information. This was after I read that the proper way to process data and display data is to separate them into two different scripts so as not to run into a redundant $_POST data issue. That is, not to keep $_POSTing the same data to the server every page refresh.

我有一个查看页面index.php和一个数据处理页面setDate.php.当查看index.php时,用户可以选择通过输入表单设置$_POST['month']$_POST['year']变量,并将它们提交给setDate分别分配$_SESSION['desMonth']$_SESSION['desYear'].

I have a view page, index.php, and a data processing page, setDate.php. When viewing index.php, the user can choose to set $_POST['month'] and $_POST['year'] variables via an input form, and submit them to setDate to assign $_SESSION['desMonth'] and $_SESSION['desYear'] respectively.

直到我在setDate.php上添加第二个(IMO冗余)session_start();声明后,代码才开始按照我想要的方式工作.没有它,就好像index.php完全忽略了setDate.php$_SESSION[*]修改.

It wasn't until I added a second (IMO redundant) session_start(); declaration on setDate.php that the code started to work the way I wanted to. Without it, it was as if index.php was ignoring setDate.php's $_SESSION[*] modifications completely.

如果已经在使用$_SESSION[*]数据的初始index.php上启动了会话(并接收到PHPSESSID cookie),为什么必须定义此冗余的session_start();?

Why do I have to define this redundant session_start(); if I already started the session (and received the PHPSESSID cookie) on the initial index.php where the $_SESSION[*] data is being used?

以下是一些有效的代码段:

Here are some working code snippets:

setDate.php

<?php
require_once 'jan.php';
session_start();

//get the requested month and years to view (iterative).
if(isset($_POST['nextMonth']) && filter_var($_POST['nextMonth'], FILTER_SANITIZE_NUMBER_INT)) { //this filter only allows +- and 0-9
    $_SESSION['desMonth'] += sanitizeInput($_POST['nextMonth']);
    if($_SESSION['desMonth'] > 12) {
        $_SESSION['desMonth'] = $_SESSION['desMonth']-12;
        $_SESSION['desYear'] += 1;
    }
    else if($_SESSION['desMonth'] < 1) {
        $_SESSION['desMonth'] = 12;
        $_SESSION['desYear'] -= 1;
    }
}

//get the explicit month and years to view.
if(isset($_POST['month']) && filter_var($_POST['month'], FILTER_SANITIZE_NUMBER_INT)) {
    $_SESSION['desMonth'] = sanitizeInput($_POST['month']);
    echo "set month";
}
if(isset($_POST['year']) && filter_var($_POST['year'], FILTER_SANITIZE_NUMBER_INT)) {
    $_SESSION['desYear'] = sanitizeInput($_POST['year']);
    echo "set year";
}

echo $_SESSION['desMonth'];
echo $_SESSION['desYear'];
header("Location: /");
die();

?>

被截断的index.php

<?php 
session_start();
require_once 'cellUpdater.php';

$timeForDateUse = mktime(1,1,10,$_SESSION['desMonth'],1,$_SESSION['desYear']); //this line is used for various formatting below.
...

没有setDate.php中的session_start();声明,将不会保留$_SESSION[*]数据.为什么?

Without the session_start(); declaration in setDate.php the $_SESSION[*] data will not be preserved. Why?

回答问题,编辑虚构的互联网点

Question answered, editing for imaginary internet points

推荐答案

来自php.net:

session_start()创建一个会话或恢复当前会话,具体取决于 通过GET或POST请求传递或通过传递的会话标识符 Cookie.

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

当调用session_start()或会话自动启动时,PHP将 调用打开和读取会话保存处理程序.

When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

换句话说,session_start()不仅可以在尚不存在会话时创建会话,还可以使脚本访问当前会话.它授予对$_SESSION变量的读取访问权限.

In other words, session_start() does not only create a session when a session does not exists yet, but it also makes it possible for a script to access the current session. It gives read and write access to the $_SESSION variable.

没有session_start,脚本无法从会话中写入或读取,会话仍然存在,但脚本无法读取或修改.如果只想授予对会话的读取访问权限,则可以调用session_write_close();来关闭写入访问权限.当您希望多个文件同时打开同一会话时,这会很方便.脚本具有写访问权时,它将阻止当前会话文件,并阻止所有其他希望对同一会话进行写访问的脚本.

Without session_start, the script cannot write or read from the session, the session is still there but it cannot be read or modified by the script. If you only want to give read access to a session you can call session_write_close(); to close the write access. This can be handy when you want multiple files to open the same session at the same time. When a script has write access it blocks the current session file, blocking all other scripts that want write access to the same session.

如果您很懒,并且总是希望会话处于活动状态,则可以编写

If you are lazy and always want a session to be active, you can write

php_flag session.auto_start 1

.htaccess文件中以启用php中会话的自动启动.

in a .htaccess file to enable the auto start of a session in php.

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

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