0

Web サイト用の 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';
        }
}
?>

client.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-->
4

1 に答える 1

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_buttonHTML フォームの送信ボタンの実際の名前に置き換えます。文字列「0」は空と見なされるため、
使用はお勧めできません。empty()チェックする必要があるのは、文字列に少なくとも 1 文字 (長さが 0 でない) が含まれているかどうかです。

于 2012-07-07T21:46:39.777 に答える