无论我在两个文件中使用session_start(),还是只在第一个文件中使用$_lang,我都必须使用php文件,并希望使用会话将$_lang发送到其他页面。如果我使用cookie,我也会遇到同样的问题
js文件:
$(document).ready(function(){
cat();
function cat(){
$.ajax({
url: "action.php",
method: "POST",
data: {menu:1},
success: function(data){
$("#get_menu").html(data);
}
})
}
});
第一个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"];
...
发布于 2017-03-06 08:49:29
不要使用$_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");
?>
发布于 2017-03-06 10:46:18
将session_start()
添加到第二个文件。因为您正在重定向浏览器,所以您有一个新的请求,如果没有session_start()
,$_SESSION
对象将不可用。
<?php
session_start(); <-- Add this
include ('db.php');
$_lang=$_SESSION['lang'];
...
https://stackoverflow.com/questions/42615920
复制相似问题