简体   繁体   中英

Restrict access to login page after users have logged in php

i have a login page on my mobile website as index.html so the user has to either login , which will take them to the main site, or register, which will let them register, and then login and gain access to the main site.

bit like a mobile app login page.

how can i block this page being accessed again by the user once they are logged in? as they can simply press the back button on their phones to go back to this page.

ideally i want if they try and access this index.php page once they are logged in to be redirected back to the home.php page.

started work on site here - http://m.cutecupcak.es

Use a session or cookie.

You would set the session upon the login and check your index.php page if the session is set or not.

Basic useage of a session

<?php
session_start();

// Set the session
$_SESSION["loggedin"] = "yes";

// Check if the session exists or doesn't, in this case, it does.
isset($_SESSION['loggedin']){
echo "You're logged in";
}else{
echo "You're not logged in";
}
?>

To build upon Jacob's answer.

Set a cookie or session upon logon and upset it upon logout.

In the index page. Check that the user is already logged in, and redirect the to the members page if they are.

Also it is a good idea to use the header function and disable caching of the page so that it is checked against the server every time.

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