$errors[] 不返回 [英] $errors[] not returning

查看:50
本文介绍了$errors[] 不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站构建 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆