I have a problem with the session variable in the remote server

advertisements

I am a starter in PHP language. I was trying to develop a small application. For that (lets say, page 1) I was posting some data to a PHP file from the page 1 through an Ajax call.Then I want to access the same value from that PHP file in another HTML file through another Ajax call (let's call page 2).

So I used the session variable for this. I was able to access the values easily in my local-host. Then I uploaded the files in my hosting server. But the application is not working. From the page 1 the data is sending and I am getting the Ajax response accordingly. but am not getting the data in the page2.

Page 1 code is as follows

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">

$( document ).ready(function() {
 $.ajax({
                type: 'GET',
                url: 'main.php',
                data: {type : 'test' },
                dataType:'html',
                success: function (data) {
                            location.assign("mains.html");  

                }
         });
    return true;
});

</script>

page 2 is as follows

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">

    $( document ).ready(function() {

        $.ajax({
                type: 'GET',
               url: 'main.php',
                data: { test: 'value' },
                dataType:'json',
                success: function (data) {
                                alert(data);

                }
         });

    });

</script>

the server file is as following,(of course its a PHP file)

<?php
session_start();

if(isset($_GET['type'])){

$_SESSION['type'] = $type = $_GET['type'];

echo $_SESSION['type'];

}

if(isset($_GET['test'])){

echo $_SESSION['type'];

}

echo $_SESSION['type'];
?>


Change the extension of your HTML file to .php, then start the file with

<?php
session_start();
?>

In other words: session_start() needs to be called in every PHP file you use.