2017-03-06 87 views
-1

我必须php文件,并希望通过使用会话将$ _lang发送到其他页面,无论是在两个文件中使用session_start()还是仅在第一个文件中使用$ _lang都不能发送。我有同样的问题,如果我使用的cookie未定义的变量:_SESSION在php中?

js文件:

​​

第一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"); 


?> 

这里是第二个文件的代码。

<?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"]; 
     ... 

回答

0

不要使用_GLOBALS $ ...

这种尝试..

<?php 
session_start(); 

//get current url 
$goback=$_SERVER['HTTP_REFERER']; 
$_SESSION['lang']=$_GET['lang']; 
echo $_SESSION['lang']; 

//go to current url 
header("location:$goback"); 

?> 
+0

我试过了,还是一样的错误。 –

+0

如果你只有一个最小文件(session_start()和print_r($ _ SESSION)),你会得到同样的错误吗? – Scovetta

+0

@scovetta我添加了第二个文件的print_r($ SESSION))和我有 阵列([郎咸平] =>我们) 阵列可用,但我不知道为什么这部分不工作 $ _lang = $ _ SESSION [ '郎']; –

0

添加session_start()到第二个文件。由于您正在重定向浏览器,因此您有新的请求,并且没有session_start(),则$_SESSION对象将不可用。

<?php 
session_start(); <-- Add this 

include ('db.php'); 

$_lang=$_SESSION['lang']; 

... 
+0

它的工作!谢谢 –