我正在为我的网站构建PHP登录和注册系统。如果表单字段为空,则编码要求使用$ errors [],并检查数据库中是否存在输入用户名。即使我没有提供任何信息,我也没有收到任何错误。
的login.php
<?php
include 'cic/initalize.php';
if (user_exists('cassey') === true) {
echo 'exists';
}
die();
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'Please provide your username and password.';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find the username entered, please enter a valid
username or register to continue';
}
}
?>
clients.php(用户)
<?php
function user_exists($username) {
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT userId FROM clients WHERE
username = '$username'"), 0) === 1) ? true : false;
}
?>
调用login.php的代码
<div class="widget">
<h2>Login | Register</h2>
</div><!--End widget class tag-->
<div class="inside">
<form action="login.php" method="post">
<ul id="logIn">
<li>
Username:<br/>
<input type="text" name="username"/>
</li>
<li>
Password:<br/>
<input type="password" name="password"/>
</li>
<li>
<input type="submit" value="Login"/>
</li>
<li>
<a href="register.php">Register</a>
</li>
</ul>
</form>
</div><!--End inside class tag-->
答案 0 :(得分:0)
<强>的login.php 强>:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include 'cic/initalize.php';
if (user_exists('cassey') === true)
echo 'exists';
else
die "User does not exist!";
if (isset($_POST['your_submit_button']) && isset($_POST['username']) && isset($_POST['password']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if (strlen($username)==0 || strlen($password)==0)
{
$errors[] = 'Please provide your username and password.';
}
elseif(user_exists($username) === false)
{
$errors[] = 'We can\'t find the username entered, please enter a valid
username or register to continue';
}
}
else
{
echo "Error: at least one field wasn't set in the form !<br>";
print_r($_POST);
}
?>
文档:
isset
empty
ini_set
error_reporting
print_r
将your_submit_button
替换为HTML表单中提交按钮的真实姓名
使用empty()
不是一个好主意,因为字符串“0”被认为是空的。你需要检查的是字符串是否至少包含一个字符(长度不等于零)。