php 如何仅为登录用户保护页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6810221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:20:13  来源:igfitidea点击:

How do I protect a page only for logged users?

phplogin-script

提问by Erik

I created a login form that works great. But I realized the page my user is directed to can still be accessed by anybody. How do I protect the page being accessed only viewable by those logged in?

我创建了一个很好用的登录表单。但是我意识到我的用户指向的页面仍然可以被任何人访问。如何保护正在访问的页面只能由登录者查看?

Do I need to place a script on the success page itself?

我需要在成功页面本身上放置脚本吗?

Here is my check_login.php:

这是我的 check_login.php:

<?php
$host="localhost"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="xxx"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

$user_info = mysql_fetch_assoc($result);

 if( isset($user_info['url']) ) {

 session_register("myusername");
 session_register("mypassword");
     header('Location: ' . $user_info['url']); //Redirects to the supplied url from the DB
 } else {
  header("location:error.htm");
 }
 ?>

采纳答案by Sebi

Every of your page should start with

你的每个页面都应该以

session_start();

and you should not be using session_register( "variablename" )as of PHP version 4.2, use

并且您不应该session_register( "variablename" )从 PHP 4.2 版开始使用,请使用

$_SESSION["variable"] = value;

so example page with is-logged-it checking would be:

所以带有 is-logged-it 检查的示例页面将是:

<?php
session_start();
if($_SESSION["loggedIn"] != true) {
    echo("Access denied!");
    exit();
}
echo("Enter my lord!");
?>

and logging-in script:

和登录脚本:

<?php
    /*
        ... db stuff ...
    */

if( isset($user_info['url']) ) {
    $_SESSION["loggedIn"] = true;
    $_SESSION["username"] = $myusername;
    header('Location: ' . $user_info['url']); //Redirects to the supplied url from the DB
} else {
    header("Location: error.htm");
}
?>

回答by plague

On the page that requires the user to be logged in check to see if they have a valid session. If not send them to the login page.

在需要用户登录的页面上,检查他们是否具有有效的会话。如果没有将它们发送到登录页面。

if (!$_SESSION['myusername'])  
{  
    header('location: /login.php');  
    exit;  
}

回答by Dor

In each page/content with restricted access, you should authenticate the client/user. If people were crazy then you'd have to make the user fill in his details (username/password) in every page, but thanks to "HTTP cookies" - we don't have to do that.

在访问受限的每个页面/内容中,您应该对客户端/用户进行身份验证。如果人们疯了,那么您必须让用户在每个页面中填写他的详细信息(用户名/密码),但多亏了“HTTP cookie”——我们不必这样做。

回答by Kerrek SB

You should basically use session management to track whether a user is in an authenticated session or not. If not, you (re)direct them to the index page; if yes, you grant them access to whichever resource they requested.

您基本上应该使用会话管理来跟踪用户是否在经过身份验证的会话中。如果没有,您(重新)将它们定向到索引页面;如果是,您授予他们访问他们请求的任何资源的权限。

To use sessions, put your session setup functions at the top of every PHP scriptinside your application (setup functions include session handler, cookie domain and cookie name), and say session_start(). Then, check if a login flag has been defined in the current session like $_SESSION["user_is_logged_in"]. In the authentication page, you would of course define $_SESSION["user_is_logged_in"] = true;at some stage.

要使用会话,请将会话设置函数放在应用程序中每个 PHP 脚本的顶部(设置函数包括会话处理程序、cookie 域和 cookie 名称),然后说session_start(). 然后,检查当前会话中是否定义了登录标志,例如$_SESSION["user_is_logged_in"]. 在身份验证页面中,您当然会$_SESSION["user_is_logged_in"] = true;在某个阶段进行定义。