javascript - Need an Ajax call to destroy session - Stack Overflow

I have a script where when a user get verified heshe is brought to Home.php. At the moment Home.php do

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

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

Share Improve this question edited Jun 4, 2014 at 9:24 TMH 6,2468 gold badges55 silver badges89 bronze badges asked Jun 4, 2014 at 9:07 GreevmanGreevman 661 gold badge4 silver badges11 bronze badges 5
  • 1 How css code can be related to ajax and sessions? – John Doe Commented Jun 4, 2014 at 9:11
  • Could you not just do <a href="Index.php?logout"> and then at the top of index.php use: if (isset($_GET['logout'])) { session_destroy(); } - No need for ajax. The user will then be shown the login form. – Paul Blundell Commented Jun 4, 2014 at 9:11
  • <button id="LogOut" onclick="jsfunction()">Log Out</button> Then make jsfunction() log the user out and redirect the user via Ajax. – kpp Commented Jun 4, 2014 at 9:26
  • 2 Danger: You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. – Quentin Commented Jun 4, 2014 at 9:26
  • tks @Quentin I only need it present so I didn't get bogged down with seurity – Greevman Commented Jun 4, 2014 at 9:33
Add a ment  | 

1 Answer 1

Reset to default 3

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745274014a4619924.html

相关推荐

  • javascript - Need an Ajax call to destroy session - Stack Overflow

    I have a script where when a user get verified heshe is brought to Home.php. At the moment Home.php do

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信