[英]Undefined variable: _SESSION in php?
I have to php files and want to send the $_lang to the other page by using session, whether i use session_start() in both file or just only in the first file the $_lang can not be sent.我必须使用 php 文件并希望通过使用会话将 $_lang 发送到另一个页面,无论我在两个文件中都使用 session_start() 还是仅在第一个文件中使用 $_lang 都无法发送。 I have same problem if i use cookie
如果我使用 cookie,我也有同样的问题
js file: js文件:
$(document).ready(function(){
cat();
function cat(){
$.ajax({
url: "action.php",
method: "POST",
data: {menu:1},
success: function(data){
$("#get_menu").html(data);
}
})
}
});
first php file:第一个php文件:
<?php
session_start();
//get current url
$goback=$_SERVER['HTTP_REFERER'];
$GLOBALS['_lang']=$_GET['lang'];
$_SESSION['lang']=$_lang;
echo $_SESSION['lang'];
//go to current url
header("location:$goback");
?>
here is the code for second file.这是第二个文件的代码。
<?php
include ('db.php');
$_lang=$_SESSION['lang'];
//$_lang= 'us';
if(isset($_POST["menu"])){
//function display_menu(){
$category_query="SELECT * FROM menu WHERE parent_id=0";
$run_query=mysqli_query($con,$category_query);
if(mysqli_num_rows($run_query)>0){
while($row=mysqli_fetch_array($run_query)){
$menu_id=$row["menu_id"];
$menu_name=$row[$_lang];
$menu_icon=$row["icon"];
...
Don't use $_GLOBALS ...不要使用 $_GLOBALS ...
Try with this ..试试这个..
<?php
session_start();
//get current url
$goback=$_SERVER['HTTP_REFERER'];
$_SESSION['lang']=$_GET['lang'];
echo $_SESSION['lang'];
//go to current url
header("location:$goback");
?>
Add session_start()
to the second file.将
session_start()
添加到第二个文件中。 Since you're redirecting the browser, you have a new request, and without session_start()
, the $_SESSION
object won't be available.由于您正在重定向浏览器,因此您有一个新请求,如果没有
session_start()
,则$_SESSION
对象将不可用。
<?php
session_start(); <-- Add this
include ('db.php');
$_lang=$_SESSION['lang'];
...
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。当然可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.