PHP页面密码保护
我通过在线搜索为PHP网页创建了一个简单的密码保护页面。下面是代码 protect.php:PHP页面密码保护,php,password-protection,Php,Password Protection,我通过在线搜索为PHP网页创建了一个简单的密码保护页面。下面是代码 protect.php: <?php namespace Protect; function with($form, $password, $scope=null) { if( !$scope ) $scope = current_url(); $session_key = 'password_protect_'.preg_replace('/\W+/', '_', $scope)
<?php
namespace Protect;
function with($form, $password, $scope=null) {
if( !$scope ) $scope = current_url();
$session_key = 'password_protect_'.preg_replace('/\W+/', '_', $scope);
session_start();
if( $_POST['password'] == $password ) {
$_SESSION[$session_key] = true;
redirect(current_url());
}
if( $_SESSION[$session_key] ) return;
require $form;
exit;
}
function current_url($script_only=false) {
$protocol = 'http';
$port = ':'.$_SERVER["SERVER_PORT"];
if($_SERVER["HTTPS"] === 'on') $protocol .= 's';
if($protocol === 'http' && $port === ':80') $port = '';
if($protocol === 'https' && $port === ':443') $port = '';
$path = $script_only ? $_SERVER['SCRIPT_NAME'] : $_SERVER['REQUEST_URI'];
return $protocol."://".$_SERVER[SERVER_NAME].$port.$path;
}
function redirect($url) {
header("Location: ".$url);
exit;
}
<html>
<body>
<form method="POST">
<?php
if( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
?>
Invalid password
<?php
}
?>
<p>Enter password for access:</p>
<input type="password" name="password">
<button type="submit">Submit</button>
</form>
</body>
</html>
无效密码
输入访问密码:
提交
在要使用安全密码保护的php网页顶部:
<?php
require_once('protect.php');
Protect\with('form.php', 'demo'); // demo is the password
?>
它工作得很好,但我得到了一个错误
未定义索引:第9行C:\xampp\htdocs\iv\admin\protect.php中的密码和会话开始()已定义
(位于要保护的php页面顶部)
当我试图做出任何改变时,它完全不起作用
任何人,请帮助并指导我错误的确切位置。您必须首先检查您的
功能中是否已提交密码
// this has to be checked first
// added isset to check if its existing
if( isset($_SESSION[$session_key]) && $_SESSION[$session_key] ) return;
^-------------------------------^
if( isset($_POST['password']) && $_POST['password'] == $password ) {
^--------------------------^
...
}
您必须首先使用
功能检查密码是否已在中提交
// this has to be checked first
// added isset to check if its existing
if( isset($_SESSION[$session_key]) && $_SESSION[$session_key] ) return;
^-------------------------------^
if( isset($_POST['password']) && $_POST['password'] == $password ) {
^--------------------------^
...
}
正如@Martin在几条评论中指出的,阅读链接的问题/答案可以轻松解决您的两个问题
第一个问题是会话已启动错误,可以通过从函数中取出session\u start()
并在顶级php文件中只放一次来轻松解决
第二个问题通过使用empty()
或isset()
解决
要设置会话,请执行以下操作:
<?php
# Just add by default, don't use an "if" clause
session_start();
# Do the rest of your script
require_once('protect.php');
$Protect->with('form.php', 'demo');
正如@Martin在几条评论中指出的,阅读链接的问题/答案可以轻松解决您的两个问题
第一个问题是会话已启动错误,可以通过从函数中取出session\u start()
并在顶级php文件中只放一次来轻松解决
第二个问题通过使用empty()
或isset()
解决
要设置会话,请执行以下操作:
<?php
# Just add by default, don't use an "if" clause
session_start();
# Do the rest of your script
require_once('protect.php');
$Protect->with('form.php', 'demo');
hi@Martin,我没有收到你的评论aSaif,@Martin的意思是你不能在关闭一次之前打开PHP两次。请查看和。谢谢。hi@Martin,我没有收到你的评论aSaif,@Martin的意思是你不能在关闭一次之前打开PHP两次。请查看和。谢谢