我有一个问题与远程服务器的会话变量


I have an issue with the session variable in remote server

我是PHP语言的初学者。我试图开发一个小应用程序。为此(比方说,第1页),我通过Ajax调用将一些数据从第1页发送到一个PHP文件。然后我想通过另一个Ajax调用(让我们调用page 2)访问另一个HTML文件中PHP文件中的相同值。

所以我使用了session变量。我能够轻松地在本地主机中访问这些值。然后我把文件上传到我的主机服务器上。但是这个应用程序不起作用。从页面1发送数据,并相应地获得Ajax响应。但是我没有得到页面中的数据。

第1页代码如下

<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>

第二页如下

    <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>

服务器文件如下,(当然是一个PHP文件)

<?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'];
?>

将HTML文件的扩展名更改为.php,然后以

开始该文件
<?php
session_start();
?>

换句话说:session_start()需要在你使用的每个PHP文件中调用。