简体   繁体   中英

Redirect to login page if user is not logged in

I created my website, but I want to require users to log in before they can access certain pages. For example, my home page is index.php , having url (www.sample.com/index.php). There is a login button in this page will redirect to login.php . In login.php page I have an edit button, and onclick event takes to edit.php .

Now, if I paste the URL www.sample.com/edit.php in the addressbar, it goes directly to edit.php , but I want it to take to index.php if the user is not logged in, similar to how Google requires authentication.

Set a session after login successful and in edit.php check for it, if session is not set, redirect it to homepage.

login.php :

//put this at the first line
session_start();
//if  authentication successful 
$_SESSION['login'] = true;

edit.php :

if(!$_SESSION['login']){
   header("location:index.php");
   die;
}

然后请检查edit.php上的会话...如果在edit.php上,会话不在那里,那么它将被重定向到index.php ..我正在谈论将在用户成功登录时启动的会话

You can use session variable to do this, you ust be set session on login.

So on edit page starting you can write following code to check wheter user is logged in or not..

 session_start();
if(!issset($_SESSION['login_session'])){
    header("Location:login.php");
    exit(); 
}

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