php 如果用户未登录,则重定向到登录页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28779043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to login page if user is not logged in
提问by user3386779
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.phppage I have an edit button, and onclick
event takes to edit.php.
我创建了我的网站,但我想要求用户在访问某些页面之前登录。例如,我的主页是index.php,具有 url (www.sample.com/index.php)。此页面中有一个登录按钮将重定向到login.php。在login.php页面中,我有一个编辑按钮,onclick
事件需要到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.phpif the user is not logged in, similar to how Google requires authentication.
现在,如果我将 URL www.sample.com/edit.php 粘贴到地址栏中,它会直接转到edit.php,但如果用户未登录,我希望它转到index.php,类似于 Google需要身份验证。
回答by Alireza Fallah
Set a session after login successful and in edit.php check for it, if session is not set, redirect it to homepage.
登录成功后设置会话并在edit.php中检查它,如果未设置会话,则将其重定向到主页。
login.php :
登录.php :
//put this at the first line
session_start();
//if authentication successful
$_SESSION['login'] = true;
edit.php :
编辑.php:
if(!$_SESSION['login']){
header("location:index.php");
die;
}
回答by TECHNOMAN
Then please check the session on edit.php...If on edit.php, session is not there, then it wll redirected to index.php..I m talking about the session which will be started when user will login successfully
然后请检查edit.php上的会话...如果在edit.php上,会话不存在,那么它将重定向到index.php..我说的是当用户成功登录时将启动的会话
回答by rut2
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();
}