[英]Undefined variable: _SESSION when sending variables via post through JavaScript trigger
In my index.php file I call session_start()
and set a couple of session variables. 在我的index.php文件中,我调用
session_start()
并设置几个会话变量。 In a second PHP file I would like to access these session variables. 在第二个PHP文件中,我想访问这些会话变量。
Thing is, this PHP file is purely a backend script and is POSTed to when a JavaScript function is triggered. 事实上,这个PHP文件纯粹是一个后端脚本,并在触发JavaScript函数时被POST。 When the POST call attempts to cause the script in the second PHP file to be executed the error log is reporting that:
当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文件并仍然拥有我的会话变量。
There's nothing special whatsoever about POST requests and sessions. 关于POST请求和会话没有任何特别之处。
You just need to call session_start
at the top of every
file
request you want to use sessions in, that's it. 您只需要在要使用会话的每个
文件
请求的顶部调用session_start
,就是这样。
Try again with that in mind, it ought to work. 再考虑一下,它应该工作。
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();}
}
我遇到了这个问题,在尝试了很多东西之后,我只在第二个脚本中包含了session_start()并且它有效。
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)
在文档的开头,这将启动SESSION ENGINE并使您能够设置会话变量
(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']
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. 它会工作正常。
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();
1)这是在声明
session_start();
之前尝试访问$ _SESSION变量的结果session_start();
In my own case, I had already started a session in a header.php file. 在我自己的情况下,我已经在header.php文件中启动了一个会话。 But I was accessing the $_SESSION variable before the include statement.
但我在include语句之前访问了$ _SESSION变量。 Example;
例;
<?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.
虽然这是在一个较晚的时间。
session_start() is the answer. session_start()就是答案。 And here's one more way of going about it:
这是另一种方法:
<?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的顶部。
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 . PHP区分大小写 ,在所有php文件中使用$ _SESSION而不是$ _session。当然它可以工作。 Example :
示例:
<?php
session_save_path('tmp/'); (is not optional - it is mandatory)
session_start();
echo $_SESSION["s1"]."<br/>";
echo $_SESSION["s2"]."<br/>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.