我正在阅读我的“学习PHP”书籍中相当有趣的一章,并且遇到了一个代码示例,我想在我的个人网站上修改和使用该代码示例(为了保护简单的文档, “大”,这就是为什么我也不加密密码)。PHP中的简单密码保护
我已经使用了php-sample,并且我不能让它工作。
这是(不得到由长度吓坏了,它真的很简单):
<?php
if ($_POST['_submit_check']) {
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
process_form();
}
} else {
show_form();
}
function show_form($errors = '') {
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
if ($errors) {
echo '<br>'
echo implode('<br>', $errors);
echo '<br>';
}
echo 'Username: ';
echo '<input type="text" name="username" value="Username goes here">';
echo '<br>'
echo 'Password: ';
echo '<input type="password" name="password">';
echo '<br>'
echo '<input type="submit" name="submit" value="Log In">';
echo '<input type="hidden" name="_submit_check" value="1">'; //when the form is entered, this returns true and the first line of the document is good to go
echo '</form>';
}
function validate_form() {
$errors = array();
$users = array('admin' => 'pass123',
'notsoadmin' => 'pass1234');
if (!array_key_exists($_POST['username']) {
$errors[] = "Please enter username and password";
}
$saved_password = $users[ $_POST['password'] ];
if ($saved_password != $_POST['password']) {
echo "Password and username don't match. Please try again";
}
return $errors;
}
function process_form() {
$_SESSION['username'] = $_POST['username'];
echo "Welcome, $_SESSION[username]";
}
?>
我的HTML和东西之前,我还添加了这一点:
<?php session_start(); ?>
显然我错过了什么.. 。也许它在开始的时候就是$form_errors
,这会导致问题(这是“什么都没有发生”),这是在我的书中,但我不知道为什么/从哪里来的?
你期待看到什么?反而发生了什么? (你知道,“它不工作”和“什么都没有发生”是一种......模糊) – Piskvor 2010-09-09 13:29:49
你是对的,不是很清楚......基本上,我想要的是,如果表单已经最近提交,显示“欢迎”的事情。如果尚未提交,请显示表单。如果提交的表单没有错误,没有问题。如果提交时出现错误,请再次使用“简易错误日志” – Latze 2010-09-09 17:27:52