javascript - How to on load page destroy user session and force user log out?
Get the solution ↓↓↓I have a part of the code where the user is allowed to use the services from the site as long as he pays the subscription after the subscription expires he has 10 days to pay, after 10 days from the day the subscription expires his account is suspended.
The problem is that only when the user logs out and tries to log in he see that his account has been suspended.
now I need to add on this existing code that during the load page destroy user session if the user has expired 10 days after not paying the subscription.
most of the code is already made I just need where to put and which code exactly to use to destroy user session on page load and to log out user:
The code:
if ($user->expire_date == null) {
echo "<script>window.location.href ='/payment.php';</script>";
}else{
$extendExpiredDate = date("Y-m-d", strtotime($user->expire_date ." +10 days") );
$todayDate = Date('Y-m-d');
if ($todayDate > $extendExpiredDate) {
echo "<script>window.location.href ='/payment.php';</script>";
}
}
Answer
Solution:
use the cookies!! delete with javascript all cookies of session and redirect you user to payment.php
php session its other function what work for you task
Answer
Solution:
other options for you task is this
https://www.php.net/manual/en/function.session-destroy.php
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
// time() return Now Date
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mysqli::real_connect(): (hy000/2002): connection refused
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.