doudao1282 2014-06-04 09:07
浏览 51
已采纳

需要一个Ajax调用来销毁会话

I have a script where when a user get verified he/she is brought to Home.php. At the moment Home.php doesn't do much. But in the bottom left hand corner I have a log out button. And as you know when the user clicks on this button he expects his session to be destroyed and for him to be redirected to a log in page. Unfortunately you can't make a click listener in php. I have browsed for an hour looking for a solution but I have not been able to find the right key word or something. This is my code

EDIT: You only really have to read some code from Home.php the est is only if someoe wants to run the code if they are not sure of their answer

Index.php(Login Page)

<?php

session_start();

mysql_connect("localhost","root","") or die ("cannot");
mysql_select_db("virtualdiary") or die ("db");

if (isset ($_POST["Username"])&& isset($_POST["Password"]))
{


$Username = $_POST["Username"];
$Password = $_POST["Password"];

$_SESSION["username"] = $Username;

$DB_Check = " SELECT * from users Where username = '".$Username."' and password = '".$Password."' " ;
$result = mysql_query($DB_Check);

if(mysql_fetch_assoc($result) === false){
    $error = "invalid username or password";
    }else{
    header( 'Location: Home.php' ) ;

    }
     }
?>

 <html>

<head>
<link type="text/css" rel="stylesheet" href="Index.css"/>
<title>Login</title>

</head>

<body>
<div id="main">
<div class="messages">
 <?php 
 if(isset($error))
  echo $error; ?>
</div>
<form action="Index.php" method="post">
 <h5>Diary Name:</h5>
     <input name="Username" type="text"/>

 <h5>Password:</h5>
     <input name="Password" type="password"/>
     </br>
     </br>
     </br>

     <input name="login" type="submit"/>

 </form>

 <p>Click <a href="Register.php">HERE </a>to register.</p>

 </div>
</body>
</html>

Home.php

  <?php 
session_start();
echo "Username = " . $_SESSION["username"] . " !";

mysql_connect("localhost","root","") or die ("cannot");
mysql_select_db("virtualdiary") or die ("db");

if (isset($_POST["entry"])){



$entry = $_POST["entry"];

$submission = "INSERT INTO `virtualdiary`.`entries` (`entry`) VALUES ('". $entry . "')";

mysql_query($submission);
}
 ?>

 <html>

 <head>
 <link type="text/css" rel="stylesheet" href="Home.css"/>
 <title>Home</title>
 </head>
 <body>
 <h1>Entry:  </h1>
 <form method="post" action="Home.php">
    <textarea name="entry" rows="24" cols="87">
    <?php 
    if (isset($_POST["entry"])){
    echo $entry;
   } 
?>
</textarea>
     </br>
 </br>
    <input name="submit" type="submit"/>     
   </form>

     <button id="LogOut"><a href="Index.php">Log Out</a></button>
     </body>

   </html>

From what I have found from searching around I will need a Home.js file with an ajax call. I don't know the first thing about Ajax so I will probably need code to paste or a very blunt tutorial. Thanks

  • 写回答

1条回答 默认 最新

  • drnpwmq4536 2014-06-04 09:10
    关注

    You could change the logout href to /Logout.php, and in Logout.php have

    <?php
    session_start();
    session_destroy();
    header('Location: /Index.php');
    ?>
    

    That will simply destroy the users current session, then redirect the user back to the Index.php page.

    The AJAX way would be (using jQuery, I can't remember the vanilla JS syntax for ajax calls)

    $.ajax({
        type: 'GET',
        url: '/Logout.php',
        success: function(msg) {
            if (msg == 'loggedOut') {
                window.location.href = 'Index.php';
            }
        }
    });
    

    And then you'd need to change Logout.php, instead of the header line, make it echo/die/print loggedOut (or a json string which would probably be better, but this is just an example).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?