简体   繁体   中英

How to on load page destroy user session and force user log out?

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>";
        }
    }

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

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();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM