dongxing2710
2014-04-19 02:21
浏览 43

使用Ajax调用Php函数请求更改会话变量

I have a communication problem between home.php page and user.php page.

on Homepage there is link for Log out

<span class="log_out"> <a id="logOut">Log Out</a></span>

When a user click this page ajax call will be started

Here is my ajax call

<script>
    $( document ).ready(function() {
        $( "#logOut" ).click(function() {
        $.ajax({
         url: 'class/user.php',
         data: "logout=1",
         success: function(data) {
            $('body').append(data);
         }
         });
        });
});

in user.php I have this

<?php
    if(isset($_GET['logout'])){
        echo "alert";
        $_SESSION['user'] = 0;
    }
?>

When I click logout, alert is being appended in body, but session variable was not changed at all.

I dont know what's going on here.

图片转代码服务由CSDN问答提供 功能建议

我在home.php页面和user.php页面之间存在通信问题。 主页上有

的链接 <前> &lt; span class =“log_out”&gt; &lt; a id =“logOut”&gt;退出&lt; / a&gt;&lt; / span&gt;

当用户点击此页面时,将启动ajax调用< / p>

这是我的ajax调用

 &lt; script&gt; 
 $(document).ready(function(){
 $(  “#logOut”)。click(function(){
 $ .ajax({
 url:'class / user.php',
 data:“logout = 1”,
 success:function(data){  
 $('body')。append(data); 
} 
}); 
}); 
}); 
   
 
 

< user.php中的/ p>

我有这个

 &lt;?php 
 if(isset($ _ GET ['logout']))  {
 echo“alert”; 
 $ _SESSION ['user'] = 0; 
} 
?&gt; 
   
 
 

点击退出时, 警报正在附加在正文中,但会话变量根本没有改变。

我不知道这里发生了什么。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpb_4431 2014-04-19 02:34
    已采纳

    if(isset($_GET['logout'])){
        if(!isset($_SESSION)) 
        { 
            session_start(); 
        } 
        $_SESSION['user'] = 0;
        Print_r ($_SESSION);
    

    }

    I found solution thanks for helping guys, I just need to add session validation on my if else clause, although I have this validation on top of the page, when I added it inside the function, problem fixed

    点赞 评论
  • dongxing1412 2014-04-19 02:28

    You need to add session_start(); to the top of your user.php file and also debug with the echo after a session is set, otherwise you'll get the warning you're getting at the moment.

    点赞 评论

相关推荐 更多相似问题