How to Detect If a User Has Logged Out, in PHP

Showing the user that he has logged out

Instead of using php unset(); function to destroy a session, you can actually save boolean "false" in the $_SESSION["user"];

For example, in you logout page:

Instead of destroying, set session to false

Ex:

``
//When logging out
$_SESSION["user"] = false;

//now to check if user logged out

if($_SESSION["user"] === false) {

echo "you logged out";

}elseif(!isset($_SESSION["user"])) {//when the session finally expires
echo " you haven't logged in";

}else{
echo "you are logged in";
}

//to get rid of it after page reload, you need to unset the SESSION

unset($_SESSION["user"]);

How to check if a user is logged in

Here is how to check if a user is logged in and then redirect them to the page they first visited.

First check to see if a user is logged in:

<?php

session_start();
if(!(isset($_SESSION['username'])))
{
header("Location: index.php");
}

?>

Then include that file in all of your web pages you will be using. Also, create a session for the URL. This will go at the top of your page:

<?php include "includes/login-check.php"; ?>
<?php $_SESSION['url'] = $_SERVER['REQUEST_URI']; ?>
<?php ob_start(); ?>

Then right in the body of the HTML add this:

<input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />

Then within your login file check for the URL session:

    //check to see what page user first visited
if(isset($_SESSION['url'])) {
$url = $_SESSION['url'];
} else {
$url = "../index.php";
}
//redirect user to page they initially visited
header("Location: $url");

That should fully answer your question.

Detecting if a user has logged out (got no session) using php via JS, and if so - stopping the execution of the remainder of the JS function

Here's a callbacky version:

function CheckForSession(onLoggedIn, onLoginExpired) {
var str="chksession=true";

jQuery.ajax({
type: "POST",
url: "chk_session.php",
data: str,
cache: false,
success: function(res){
if(res == "0") {
onLoginExpired();
} else {
onLoggedIn();
}
}
});
}

jQuery('body').on('click','.cart_icon_div1.active.selected', function(){

CheckForSession(function() {
// Do any important session-required stuff here
},
function() {
alert('Your session has been expired!');
});
});

PHP-Script to check if user is logged in not working as expected

Your going to want to update

if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] !== true){

with

if(!isset($_SESSION["loggedin"]) || !$_SESSION["loggedin"]) {

That verifies that the $_SESSION["loggedin"] is not set OR that its set and NOT TRUE then it will do your redirection

How to display if you signed out in PHP?

The idea of being signed in (typically associated with the retention of state between the client and server) is addressed most commonly through the use of cookies (or more aptly sessions). This has nothing to do with sending POST requests to the server. Just checking that the stateful information is still valid.

Assuming you do something similar to this to sign the user in...

<?php
session_start();
if ($user->signIn()) { // successful sign in attempt
$_SESSION['signedIn'] = true;
$_SESSION['userId'] = $user->id;
} else {
// failed to sign in
}

Assuming you do this to sign the user out...

<?php
session_start();

// Destroy the session cookie on the client
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}

// Destroy the session data on the server
session_destroy();

Then determining whether or not the user is signed in from index.php should be as simple as this...

<?php
session_start();

if (!empty($_SESSION['signedIn'])) { // They are signed in
/* Do stuff here for signed in user */
} else { // They are not
/* Do other stuff here for signed out user */
}


Related Topics



Leave a reply



Submit