简体   繁体   English

正确的 PHP 密码保护

[英]Proper PHP Password Protection

I'm looking for the proper process of doing password protection on one single PHP page.我正在寻找在单个 PHP 页面上进行密码保护的正确过程。 Prefferable with all of the code existing within that same page/file.最好使用存在于同一页面/文件中的所有代码。

I have seen a few example here on Stack Overflow but they always have the instruction of pasting all of the code to the top of the page.我在 Stack Overflow 上看到了一些例子,但他们总是有将所有代码粘贴到页面顶部的指令。 And what does is adds a password input box to the top of the page, while loading absolutely everything you might have wanted to protect below that anyway because there is nothing present in the code or the instructions to clearly indicate how to make it so information is actually hidden before the password is entered.什么是在页面顶部添加一个密码输入框,同时绝对加载您可能想要保护的所有内容,因为代码或说明中没有任何内容可以清楚地表明如何使信息如此实际上隐藏在输入密码之前。

Here is an example of what commonly occurs on stack overflow when a question is answered:以下是回答问题时堆栈溢出时通常发生的情况的示例:

The user is told to paste this at the top of their php document:用户被告知将其粘贴到他们的 php 文档的顶部:

if (isset($_COOKIE['PrivatePageLogin'])) {
   if ($_COOKIE['PrivatePageLogin'] == md5($password.$nonsense)) {
?>

    <!-- LOGGED IN CONTENT HERE -->

<?php
      exit;
   } else {
      echo "Bad Cookie.";
      exit;
   }
}

if (isset($_GET['p']) && $_GET['p'] == "login") {
   if ($_POST['user'] != $username) {
      echo "Sorry, that username does not match.";
      exit;
   } else if ($_POST['keypass'] != $password) {
      echo "Sorry, that password does not match.";
      exit;
   } else if ($_POST['user'] == $username && $_POST['keypass'] == $password) {
      setcookie('PrivatePageLogin', md5($_POST['keypass'].$nonsense));
      header("Location: $_SERVER[PHP_SELF]");
   } else {
      echo "Sorry, you could not be logged in at this time.";
   }
}
?>

They are then told to paste this directly below:然后他们被告知将其直接粘贴在下面:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
<label><input type="text" name="user" id="user" /> Name</label><br />
<label><input type="password" name="keypass" id="keypass" /> Password</label><br />
<input type="submit" id="submit" value="Login" />
</form>

In this scenario nothing will be protected whatsoever.在这种情况下,任何东西都不会受到保护。 The page will load all content immediately but there will be a username and password field on the top of the page.该页面将立即加载所有内容,但页面顶部将有一个用户名和密码字段。 It will technically function while protecting nothing.从技术上讲,它将在不保护任何东西的情况下发挥作用。

In order to have this work properly, do you put the entire page contents within the form tags?为了使这项工作正常进行,您是否将整个页面内容放在表单标签中? What do you do?你做什么工作?

This may seem an odd question to ask but for the entire existence of this website people have been answering questions like that.这似乎是一个奇怪的问题,但对于这个网站的整个存在,人们一直在回答这样的问题。 The code always works but the instructions for it's basic use are often incomplete or incorrect.该代码始终有效,但其基本使用说明通常不完整或不正确。

And to preemptively answer a few questions: htaccess is a waste of time and effort, you spend more time trying to figure out what it's giving an internal server error than you do just protecting a file or folder.并且先发制人地回答几个问题:htaccess 是浪费时间和精力,与仅仅保护文件或文件夹相比,您花更多的时间试图找出它导致内部服务器错误的原因。

The information doesn't need to be protected by skynet level AI so php is fine.信息不需要被天网级别的AI保护,所以php没问题。

This question cannot be answered in a reasonable short answer, a complete login system has some complexity (database, hashing, security, ...).这个问题不能用一个合理的简短回答来回答,一个完整的登录系统具有一定的复杂性(数据库、散列、安全性……)。 Nevertheless i would like to give some starters:不过,我想给一些开场白:

  • To store passwords never use MD5, instead PHP offers the password_hash() and the password_verify() functions.永远不要使用 MD5 来存储密码,PHP 提供了password_hash()password_verify()函数。
  • Passwords should not be stored in cookies, it is common practise to remember the login state in a session.密码不应存储在 cookie 中,通常的做法是记住会话中的登录状态。 A random token will maintain the session.一个随机令牌将维持会话。
  • Each page with restricted access has to check this state, and redirect to the login page if necessary.每个访问受限的页面都必须检查此状态,并在必要时重定向到登录页面。
  • A safe site requires HTTP/SSL, otherwise you can not protect from a ManInTheMiddle attack.一个安全的站点需要 HTTP/SSL,否则您无法抵御 ManInTheMiddle 攻击。

There are plenty of tutorials out there (good ones and unsecure ones), do some research and look out for the points mentioned above.有很多教程(好的和不安全的),做一些研究并注意上面提到的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM