简体   繁体   中英

How to require a user to be logged in

I'm fairly new to PHP an was wondering how I have to make the user login before they can go to any other page on the website.
For example on www.cyka.us/p/index.php it will redirect them to www.cyka.us/p/login.php

Have you ever used sessions in php? You can use sessions as a marker if a certain user is logged in or not. It can be implemented like this:

 $isLogIn = $session('login'); if($isLogIn){ //go to other webpages }

Search sessions for further information :)

You simply test for it:

if (!user_is_logged_in()) {
    http_response_code(403);
    include("p/login.php");
    exit;
}

… the specifics of the user_is_logged_in function will depend on how you've implemented your login system.

You can do this with sessions concept in php. For every page you need check whether the user is logged in or not and redirect to (login)desired page.

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