我有一个我想要密码保护的页面。我尝试过进行HTTP身份验证,但由于某种原因,它无法在我的托管上运行。还有其他快速(简单)方法吗?谢谢!
答案 0 :(得分:50)
这里不是最强大的密码保护,所以请不要用它来保护信用卡号码或非常重要的东西。
只需将以下所有代码放入名为(secure.php)的文件中,更改用户并从“admin”传递到您想要的任何内容。然后就在那些包含include(“secure.html”)的行下面,只需将其替换为您希望他们能够看到的文件名。
他们将在[YouDomain.com/secure.php]访问此页面,然后PHP脚本将在内部包含您想要密码保护的文件,以便他们不知道该文件的名称,以后不能直接绕过密码提示访问它。
如果您想添加更多级别的保护,我建议您将您的(secure.html)文件放在站点的根文件夹[/ public_html]之外,并将其放在与该目录相同的级别,它不在目录中。然后在PHP脚本中包含文件,只需使用(“../secure.html”)。那(.../)意味着返回一个目录来查找文件。这样做,有人可以访问(secure.html)页面上的内容的唯一方法是通过(secure.php)脚本。
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "admin"
&& $pass == "admin")
{
include("secure.html");
}
else
{
if(isset($_POST))
{?>
<form method="POST" action="secure.php">
User <input type="text" name="user"></input><br/>
Pass <input type="password" name="pass"></input><br/>
<input type="submit" name="submit" value="Go"></input>
</form>
<?}
}
?>
答案 1 :(得分:25)
这有点晚了,但我想回复以防其他人来到这个页面,发现最高的回复有点过了。我对系统进行了一些改进。请注意,它仍然不是非常安全,但它是一种改进。
首先准备密码盐文件:
<强> hash_generate.php:强>
<?php
$user = "Username"; // please replace with your user
$pass = "Password"; // please replace with your passwd
// two ; was missing
$useroptions = ['cost' => 8,];
$userhash = password_hash($user, PASSWORD_BCRYPT, $useroptions);
$pwoptions = ['cost' => 8,];
$passhash = password_hash($pass, PASSWORD_BCRYPT, $pwoptions);
echo $userhash;
echo "<br />";
echo $passhash;
?>
将输出$userhash
和$passhash
放在两个文本文件中:user.txt和pass.txt。其他人建议将这些文本文件放在public_html之上,这是一个好主意,但我只是使用.htaccess并将它们存储在名为&#34; stuff&#34;
<强>的.htaccess 强>
deny from all
现在没人能看到哈希。接下来是你的index.php:
<强>的index.php:强>
<?php
$user = ""; //prevent the "no index" error from $_POST
$pass = "";
if (isset($_POST['user'])) { // check for them and set them so
$user = $_POST['user'];
}
if (isset($_POST['pass'])) { // so that they don't return errors
$pass = $_POST['pass'];
}
$useroptions = ['cost' => 8,]; // all up to you
$pwoptions = ['cost' => 8,]; // all up to you
$userhash = password_hash($user, PASSWORD_BCRYPT, $useroptions); // hash entered user
$passhash = password_hash($pass, PASSWORD_BCRYPT, $pwoptions); // hash entered pw
$hasheduser = file_get_contents("stuff/user.txt"); // this is our stored user
$hashedpass = file_get_contents("stuff/pass.txt"); // and our stored password
if ((password_verify($user, $hasheduser)) && (password_verify($pass,$hashedpass))) {
// the password verify is how we actually login here
// the $userhash and $passhash are the hashed user-entered credentials
// password verify now compares our stored user and pw with entered user and pw
include "pass-protected.php";
} else {
// if it was invalid it'll just display the form, if there was never a $_POST
// then it'll also display the form. that's why I set $user to "" instead of a $_POST
// this is the right place for comments, not inside html
?>
<form method="POST" action="index.php">
User <input type="text" name="user"></input><br/>
Pass <input type="password" name="pass"></input><br/>
<input type="submit" name="submit" value="Go"></input>
</form>
<?php
}
答案 2 :(得分:13)
<?php
$username = "the_username_here";
$password = "the_password_here";
$nonsense = "supercalifragilisticexpialidocious";
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.";
}
}
?>
页面上的登录表单...
(在同一页面,在上面^发布的代码的正下方)
<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>
答案 3 :(得分:7)
创建两个文件:
<强>保护-this.php 强>
<?php
/* Your password */
$password = 'MYPASS';
if (empty($_COOKIE['password']) || $_COOKIE['password'] !== $password) {
// Password not set or incorrect. Send to login.php.
header('Location: login.php');
exit;
}
?>
<强>的login.php:强>
<?php
/* Your password */
$password = 'MYPASS';
/* Redirects here after login */
$redirect_after_login = 'index.php';
/* Will not ask password again for */
$remember_password = strtotime('+30 days'); // 30 days
if (isset($_POST['password']) && $_POST['password'] == $password) {
setcookie("password", $password, $remember_password);
header('Location: ' . $redirect_after_login);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Password protected</title>
</head>
<body>
<div style="text-align:center;margin-top:50px;">
You must enter the password to view this content.
<form method="POST">
<input type="text" name="password">
</form>
</div>
</body>
</html>
然后在要保护的文件的顶部需要 protect-this.php :
// Password protect this content
require_once('protect-this.php');
示例结果:
填写正确的密码后,用户将被带到index.php。密码存储30天。
PS:它没有专注于保证安全,而是专心致志。黑客可以蛮力这个。用它来保持普通用户的远离。不要用它来保护敏感信息。答案 4 :(得分:6)
我只是寻找一个$_GET
变量并重定向用户,如果它不正确。
<?php
$pass = $_GET['pass'];
if($pass != 'my-secret-password') {
header('Location: http://www.staggeringbeauty.com/');
}
?>
现在,如果此页面位于:http://example.com/secrets/files.php
您现在可以通过以下方式访问它:http://example.com/secrets/files.php?pass=my-secret-password
请记住,这不是最有效或最安全的方式,但尽管如此,这是一种简单快捷的方法。 (另外,我知道我的答案已经过时,但是看到这个问题的其他人可能会发现它很有价值)
答案 5 :(得分:4)
Some easy ways:
Use Apache's digest authorization.
Use lighttpd's digest authorization.
Use php's header digest authorization.
如果你想要你也可以这样做只有某些ip地址可以登录.. :)用lighttpd真的很容易
更新:我很快就会发布一些例子,所以不要拒绝投票,我只需要为这个答案做些准备。
如果您想使用会话,以下是最佳方式:
# admin.php
session_start();
if(!$_SESSION["AUTH"])
require_once "login.php";
# Do stuff, we are logged in..
# login.php
session_start();
if($_REQUEST["username"] == "user" && $_REQUEST["password"] == "pass")
$_SESSION["AUTH"] = true;
else $_SESSION["AUTH"] = false; # This logs you out if you visit this login script page without login details.
if($_SESSION["AUTH"])
require_once "admin.php";
此方法不包含上述示例,但您对此方法感兴趣。其他方法示例仍然存在,我没有足够的时间来获取apache或lighttpd设置和php头认证:http://php.net/manual/en/features.http-auth.php会这样做。
答案 6 :(得分:2)
这给我带来了很多帮助,节省了我很多时间,它易于使用,而且工作得很好,我甚至采取了改变它的风险,它仍然有效。
如果你不想在这样做上花费太多时间,那就相当不错了。)
答案 7 :(得分:2)
</html>
<head>
<title>Nick Benvenuti</title>
<link rel="icon" href="img/xicon.jpg" type="image/x-icon/">
<link rel="stylesheet" href="CSS/main.css">
<link rel="stylesheet" href="CSS/normalize.css">
<script src="JS/jquery-1.12.0.min.js" type="text/javascript"></script>
</head>
<body>
<div id="phplogger">
<script type="text/javascript">
function tester() {
window.location.href="admin.php";
}
function phpshower() {
document.getElementById("phplogger").classList.toggle('shower');
document.getElementById("phplogger").classList.remove('hider');
}
function phphider() {
document.getElementById("phplogger").classList.toggle('hider');
document.getElementById("phplogger").classList.remove('shower');
}
</script>
<?php
//if "login" variable is filled out, send email
if (isset($_REQUEST['login'])) {
//Login info
$passbox = $_REQUEST['login'];
$password = 'blahblahyoudontneedtoknowmypassword';
//Login
if($passbox == $password) {
//Login response
echo "<script text/javascript> phphider(); </script>";
}
}
?>
<div align="center" margin-top="50px">
<h1>Administrative Access Only</h1>
<h2>Log In:</h2>
<form method="post">
Password: <input name="login" type="text" /><br />
<input type="submit" value="Login" id="submit-button" />
</form>
</div>
</div>
<div align="center">
<p>Welcome to the developers and admins page!</p>
</div>
</body>
</html>
基本上我在这里做的是在一个php文件中创建一个页面,当你输入密码时,如果它正确,它将隐藏密码屏幕并带来保护前进的东西。然后是css,这是一个至关重要的部分,因为它使类隐藏并显示页面的不同部分。
/*PHP CONTENT STARTS HERE*/
.hider {
visibility:hidden;
display:none;
}
.shower {
visibility:visible;
}
#phplogger {
background-color:#333;
color:blue;
position:absolute;
height:100%;
width:100%;
margin:0;
top:0;
bottom:0;
}
/*PHP CONTENT ENDS HERE*/
答案 8 :(得分:1)
不是解决方案,而是为了您的兴趣:当PHP作为Apache模块运行时,HTTP身份验证才有效。大多数托管商仅提供PHP作为CGI版本。
答案 9 :(得分:1)
您可以在php代码中指定密码,并且只允许拥有秘密网址的用户:
mywebsite.com/private.php?pass=secret
在您的文件中:
<?php
if(isset($_GET["pass"] && $_GET["pass"]=="secret"){
//put your code here
}
else{
echo "you're not allowed to access this page";
}
?>
答案 10 :(得分:0)
一种无需单独登录页面即可保护文件的简单方法-只需将其添加到页面顶部即可:
将secretuser和secretpassword更改为您的用户名/密码。
$user = $_POST['user'];
$pass = $_POST['pass'];
if(!($user == "secretuser" && $pass == "secretpassword"))
{
echo '<html><body><form method="POST" action="'.$_SERVER['REQUEST_URI'].'">
Username: <input type="text" name="user"></input><br/>
Password: <input type="password" name="pass"></input><br/>
<input type="submit" name="submit" value="Login"></input>
</form></body></html>';
exit();
}