-1

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. I have same problem if i use cookie

js file:

$(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
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"];
        ...
0

3 Answers 3

1

Don't use $_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");

?>
Sign up to request clarification or add additional context in comments.

2 Comments

If you just have a minimal file (session_start() adn then print_r($_SESSION)), do you get the same error?
@scovetta I added the print_r($SESSION)) in second file and i got Array ( [lang] => us ) the array is available but i don't know why this part is not working $_lang=$_SESSION['lang'];
1

Add session_start() to the second file. Since you're redirecting the browser, you have a new request, and without session_start(), the $_SESSION object won't be available.

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

 include ('db.php');

 $_lang=$_SESSION['lang'];

...

Comments

0

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.