Forum Moderators: coopster

Message Too Old, No Replies

Redirect on logout

         

Jamier101

9:27 pm on Oct 11, 2010 (gmt 0)

10+ Year Member



Hi all,

I'm thinking about having my page tell the user that they've just logged off but I can't think how it would be done, I have the following already and my idea is below that.

Top of page

<?php

//error_reporting(E_ALL|E_STRICT);

require_once("Connections/connection.php"); // Connection to the server

session_start();

$tbl_name="users"; // Define the table name

//include("includes/security.php"); //Link to the security file

$id = $_SESSION['id'];

////////////////////////////////////////////////////////////////////////////////////
$query = "SELECT * FROM `users` WHERE `username` = '".$id."' ";
$result = mysql_query($query) or die("Query error:".mysql_error());
$rowAccount = mysql_fetch_array($result);
////////////////////////////////////////////////////////////////////////////////////

?>

Section found within the div tags

<div id="user_box">
<?php

if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo 'Logged in as '. $rowAccount['username'];
echo '<input type="button" name="Button" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}

?>
</div>

Idea

<div id="user_box">
<?php

if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo 'Logged in as '. $rowAccount['username'];
echo '<input type="button" name="Button" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}
elseif{
//redirected from the logoff script after the button being clicked
echo 'You are now logged out'
}

?>
</div>

Matthew1980

10:16 pm on Oct 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jamier101,

The link that you have there points to logout.php, which by the looks of it opens a new window, but JS isn't my forte, so for all I know it could be ordering a burger?!?

I digress, the issue is your needing a file called logout, in which you will need to have a process handler to see if the session/cookie exists, if it does (which it should) unset() it destroy() it and then in the mix you would need to update the database so that you could see as the user is logged out (if you have that functionality), then lastly, if all the conditions are met, and you successfully 'log out', you can have a redirect back to a holding page, or your main screen, the choice is yours - sorry if I have misunderstood your issue though :)

Oh and for the record, elseif()<-- needs a clause to evaluate, only the else is classed as a default action when using an elseif() chain:-

if($a == $b){
//a is equal to b
}
elseif($a != $b){
//a is not equal to b
}
else{
//a is not in the room!
}

You can use a switch in place of the elseif chain, but that's if a single variable or array key (or just data) has more than one expected value...

Cheers,
MRb

Jamier101

10:55 pm on Oct 11, 2010 (gmt 0)

10+ Year Member



Currently on clicking the logout button the user is directed through this script:

<?php
session_start();

session_destroy();

header("location:index.php");
exit;

?>

It then brings them back to the index page from wherever they logged out which is fine but I like the idea of showing that you've just logged out so that people are clear on the fact that the session as completely ended.

herro123

7:43 am on Oct 12, 2010 (gmt 0)

10+ Year Member



thanks for this

Matthew1980

8:33 am on Oct 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there herro123,

Welcome to WebmasterWorld,

Glad as you found this thread helpful.

Cheers,
MRb

Jamier101

9:16 am on Oct 12, 2010 (gmt 0)

10+ Year Member



Welcome herro123 :)

rocknbil

3:35 pm on Oct 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<div id="user_box">
<?php
if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo '<p>Logged in as '. $rowAccount['username'] . '</p>
<form method="get" action="logout.php">
<input type="submit" name="Button" value="Logout">
</form>
';
}
else {
if (isset($_GET['logout_variable'])) {
echo '<p>You have successfully logged out.</p>';
}
echo '
<form method="post" action="login-script.php">
<!-- presuming uname and pass here -->
<input type="submit" value="Log In">
</form>
';
}
?>
</div>


Note the small form changes, which allow Javascript independence. Do not rely on Javascript, the above will work without it. Just sent a variable "logout_variable" from your logout script to trigger it. Note the bolded else, this will display the login form for both initial hit and logged out states.