【问题标题】:Need an Ajax call to destroy session需要 Ajax 调用来销毁会话
【发布时间】:2014-07-24 20:36:32
【问题描述】:

我有一个脚本,当用户通过验证时,他/她会被带到Home.php。目前Home.php 做的不多。但是在左下角我有一个注销按钮。如您所知,当用户单击此按钮时,他希望他的会话被破坏并被重定向到登录页面。不幸的是,您无法在php 中创建点击监听器。我已经浏览了一个小时寻找解决方案,但我无法找到正确的关键词或其他东西。 这是我的代码

编辑:您只需要从Home.php 读取一些代码,只有当某人不确定自己的答案时才想运行代码

Index.php(登录页面)

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

根据我在四处搜索中发现的信息,我需要一个带有 ajax 调用的 Home.js 文件。我不知道关于 Ajax 的第一件事,所以我可能需要粘贴代码或非常生硬的教程。 谢谢

【问题讨论】:

标签: javascript php ajax session event-listener


【解决方案1】:

您可以将注销 href 更改为 /Logout.php,并且在 Logout.php 中有

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

这将简单地破坏用户当前会话,然后将用户重定向回 Index.php 页面。

AJAX 方式是(使用 jQuery,我不记得 ajax 调用的原始 JS 语法)

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

然后您需要更改 Logout.php,而不是标题行,使其成为 echo/die/print loggedOut(或可能更好的 json 字符串,但这只是一个示例)。

【讨论】:

  • 感谢我整个上午都在寻找类似的东西
  • 没问题,别忘了,一个答案可以帮助您投票,如果它解决了您的问题,请接受它。
猜你喜欢
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多