php 未定义的变量:_SESSION 当通过 JavaScript 触发器通过 post 发送变量时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7237028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined variable: _SESSION when sending variables via post through JavaScript trigger
提问by keybored
In my index.php file I call session_start()
and set a couple of session variables. In a second PHP file I would like to access these session variables.
在我的 index.php 文件中,我调用session_start()
并设置了几个会话变量。在第二个 PHP 文件中,我想访问这些会话变量。
Thing is, this PHP file is purely a backend script and is POSTed to when a JavaScript function is triggered. When the POST call attempts to cause the script in the second PHP file to be executed the error log is reporting that:
事实是,这个 PHP 文件纯粹是一个后端脚本,并在 JavaScript 函数被触发时被 POST 到。当 POST 调用试图导致执行第二个 PHP 文件中的脚本时,错误日志报告:
_SESSION
is an undefined variable.
_SESSION
是一个未定义的变量。
I've tried calling start_session()
and session_regenerate_id()
at the top of the second PHP file but the issue has persisted.
我试过在第二个 PHP 文件的顶部调用start_session()
和session_regenerate_id()
但问题仍然存在。
I'm assuming what is going on is that because it's in a POST this PHP file is in its own session as I am still able to do this $_COOKIE[ini_get('session.name')]
.
我假设正在发生的事情是因为它在 POST 中,这个 PHP 文件在它自己的会话中,因为我仍然能够做到这一点$_COOKIE[ini_get('session.name')]
。
The information that I am trying to pass to the second PHP file isn't anything that needs to be secured but it would be nice to know in the future how to do this: call a PHP file via a POST and still have my session variables.
我试图传递给第二个 PHP 文件的信息不是任何需要保护的信息,但很高兴知道将来如何执行此操作:通过 POST 调用 PHP 文件并仍然保留我的会话变量.
回答by deceze
There's nothing special whatsoever about POST requests and sessions.
You just need to call session_start
at the top of everyfilerequest you want to use sessions in, that's it.
POST 请求和会话没有什么特别之处。
您只需要session_start
在要使用会话的每个文件请求的顶部调用,就是这样。
Try again with that in mind, it ought to work.
记住这一点再试一次,它应该可以工作。
回答by Gorkem Yontem
session_start();
is the answer of most of topic like this. i have been struggling with this issue and i solve my problem with this code
是大多数主题的答案。我一直在努力解决这个问题,我用这段代码解决了我的问题
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
if(session_id() == '') {session_start();}
} else {
if (session_status() == PHP_SESSION_NONE) {session_start();}
}
回答by robertparkerx
you must include
你必须包括
<?php session_start();?>
at the beginning of your document, this will start the SESSION ENGINE and enable you to set session variables (ie. $_SESSION['var1'], $_SESSION['var2'], etc)
在文档的开头,这将启动会话引擎并使您能够设置会话变量 (ie. $_SESSION['var1'], $_SESSION['var2'], etc)
If you're wanting to get values from a $_POST you could relate the two together by :
如果您想从 $_POST 获取值,您可以通过以下方式将两者联系起来:
$_SESSION['var1'] = $_POST['answer1']
回答by Ashahmali
I came faced with this problem, and after trying a lot of things, I just included session_start() on the second script and it worked.
我遇到了这个问题,在尝试了很多事情之后,我只是在第二个脚本中包含了 session_start() 并且它起作用了。
回答by Dung Dinh
Add the following code to the file browse.php:
将以下代码添加到文件 browse.php 中:
error_reporting( error_reporting() & ~E_NOTICE );
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
if(session_id() == '') {session_start();}
} else {
if (session_status() == PHP_SESSION_NONE) {session_start();}
}
It will work fine.
它会正常工作。
回答by Farhad
I had this problem and tried different ways and finally found a solution :
我遇到了这个问题并尝试了不同的方法,最后找到了解决方案:
PHP is case sensitive, Use $_SESSION instead of $_session in all php files.certainly It works . Example :
PHP 区分大小写,在所有 php 文件中使用 $_SESSION 而不是 $_session。当然可以。例子 :
<?php
session_save_path('tmp/'); (is not optional - it is mandatory)
session_start();
echo $_SESSION["s1"]."<br/>";
echo $_SESSION["s2"]."<br/>";
?>
回答by user28864
I also encountered the same problem recently. I could not access the contents of the $_SESSION variable.
我最近也遇到了同样的问题。我无法访问 $_SESSION 变量的内容。
1) This was as a result of trying to access the $_SESSION variable before the declaration of session_start();
In my own case, I had already started a session in a header.php file. But I was accessing the $_SESSION variable before the include statement. Example;
1) 这是在声明之前尝试访问 $_SESSION 变量的结果session_start();
在我自己的情况下,我已经在 header.php 文件中启动了一个会话。但是我在 include 语句之前访问了 $_SESSION 变量。例子;
<?php
$username = $_SESSION['username'];
//do some logical operation!!!
?>
<?php include("header.php");?>
instead of doing something like this
而不是做这样的事情
<?php include("header.php");?>
<?php
$username = $_SESSION['username'];
//do some logical operation!!!
?>
2) Another thing that may cause this problem, maybe a failure to start a session at the top of all the files that may require access to the $_SESSION variable using
2) 可能导致此问题的另一件事,可能是在所有可能需要使用 $_SESSION 变量访问的文件的顶部启动会话失败
session_start();
Hope this helps anybody that stumbles on the same problem. Although this is coming at a late hour.
希望这可以帮助任何遇到相同问题的人。虽然这是来晚了。
回答by learner_19
session_start() is the answer. And here's one more way of going about it:
session_start() 就是答案。这是另一种方法:
<?php
if (session_id() == "")
session_start(); ?>
This will ensure that a new session is started only if you do not have a current session on already. And ofcourse, as others have said, make sure to place this at the top of the html.
这将确保仅当您没有当前会话时才启动新会话。当然,正如其他人所说,确保将其放在 html 的顶部。