$errors[] 不返回 [英] $errors[] not returning
问题描述
我正在为我的网站构建 PHP 登录和注册系统.如果表单字段为空,则编码要求使用 $errors[] 并用于检查输入的用户名是否存在于数据库中.即使我没有输入任何信息,我也没有收到任何错误.
login.php
clients.php(用户)
调用 login.php 的代码
<h2>登录|注册</div><!--结束小部件类标签--><div class="inside"><form action="login.php" method="post"><ul id="登录"><li>用户名:
<input type="text" name="username"/><li>密码:<br/><input type="password" name="password"/><li><input type="submit" value="登录"/><li><a href="register.php">注册</a></表单></div><!--在类标签内结束--> 解决方案 login.php:
文档:
isset
空
ini_set
error_reporting
print_r
将 your_submit_button
替换为 HTML 表单中提交按钮的真实名称.
使用 empty()
不是一个好主意,因为字符串0"被认为是空的.您需要检查的是字符串是否至少包含一个字符(长度不为零).
I am working on building a PHP login and registration system for my website. The coding calls for using $errors[] if form fields are empty and functions to check if the input username exist on the database. I am not recieving any errors, even if I do not put in any information.
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 (users)
<?php
function user_exists($username) {
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT userId FROM clients WHERE
username = '$username'"), 0) === 1) ? true : false;
}
?>
code that calls 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-->
解决方案 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);
}
?>
Documentation:
isset
empty
ini_set
error_reporting
print_r
Replace your_submit_button
with the real name of the submit button in your HTML form.
Using empty()
is not a good idea since the string "0" is considered empty. What you need to check is if the strings contain at least a character (length not equal to zero).
这篇关于$errors[] 不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- Codeignitervalidation_errors()总是返回空;
- Spring 3 MVC - form:errors not showing the errors;
- Python返回不返回变量;
- Ember数据模型的错误属性(DS.Errors)不填充;
- fopen不返回;
- GetGeopositionAsync 不返回;
- XMaskEvent 不返回;
- precision errors with numpy;
- Return list of errors;
- Mongoid :: Errors :: MixedRelations;
- Graphene errors messages;
- Xcode errors disappear;
- Watch console for errors;
- Errors with dotnet publish;
- PG Upgrade Errors;
- Ruby on Rails:errors.add_to_base与errors.add;
- Ruby on Rails:errors.add_to_base 与 errors.add;
- Golang返回nil不返回nil;
- 返回与不返回的功能?;
- getActiveUser()不返回用户?;
- isEqualToString不返回true;
- 续集:不返回密码;
- 房间不返回重复;
- 相机不返回OnActivityResult;
- 文档加载不返回;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;