[英]How do I make sure that no other page apart from login page can be accessed without logging in?
In my PHP application, the pages beyond login page must be visible only when user is logged in. This is how I log in: 在我的PHP应用程序中,仅当用户登录时,登录页面之外的页面才必须可见。这是我的登录方式:
if($status=="ok"){
session_start();
echo '<script>window.location.assign("/Annapoorna/Welcome_Page.php");</script>';
}else{
echo '<script>alert("username / password is invalid");window.history.go(-1);</script>';
}
Status gets Ok if username and passwords match. 如果用户名和密码匹配,状态为“确定”。
Also, how can I ensure that a user who is not logged in cannot access pages apart from the login page, even by putting direct URL of other page? 另外,如何确保未登录的用户即使通过放置其他页面的直接URL也无法访问登录页面以外的页面? How do I redirect such a request to login page?
如何将这样的请求重定向到登录页面?
Create one script that check the session. 创建一个检查会话的脚本。 and that file should be included in all the pages except login file.
并且该文件应包含在除登录文件之外的所有页面中。 Consider the following example.
考虑以下示例。
File Name : session_check.php 文件名:session_check.php
<?php
session_start();
if(!isset($_SESSION["status"]) && $_SESSION["status"]=='')
{
header('Location: index.php'); // your login page
exit;
}
?>
So this will check in all the pages that your 'status' session is empty or not. 因此,这将签入所有您的“状态”会话为空的页面。 If your session is empty that means that use is not logged in. and if status is OK, that means user is logged in.
如果您的会话为空,则表示未登录use。如果状态为OK,则表示用户已登录。
Now, in your login page, you have to add condition as per below. 现在,在您的登录页面中,您必须按照以下条件添加条件。
<?php
session_start();
if(isset($_SESSION["status"]) && $_SESSION["status"]=='OK')
{
header("Location: home.php");
exit;
}
?>
Now, this will check that if the status session is not empty, then will redirect to home.php file (after the login file). 现在,这将检查状态会话是否为空,然后将重定向到home.php文件(在登录文件之后)。 If session is empty, then it will stay login page.
如果会话为空,则它将保留登录页面。
<?php
session_start();
// do this in header of pages where you don't need access of all users
if(isset($_SESSION['status']) && $_SESSION['status'] != 'OK'){
// go and login user
header("Location: login.php");
die();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.