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
Alternative for $_Server['Http_Referer'] PHP Variable in Msie
PHP Twitter Replace Link and Hashtag with Real Link
Attach Image to Facebook Event (PHP Sdk, Rest or Graph API)
HTML PHP Display Username After Success Login
Indented List to Multidimensional Array
Modify HTML Attribute with PHP
JavaScript String Length Differs from PHP Mb_Strlen
Insert Multiple Data into Database in Yii 2
Codeigniter Activerecord: Join Backticking
PHP JSON_Decode Fails Without Quotes on Key
Facebook Oauthexception: "User Hasn't Authorized the Application to Perform This Action"
Php's Simplexml: How to Use Colons in Names
Set Quantity Minimum, Maximum and Step at Product Level in Woocommerce
How to Get/Set Session_Id() or Should It Be Generated Automatically