Php, MySql And Webform


Php, MySql And Webform

我已经试着弄清楚这一点两天了,我正在遵循youtube教程,为我的Android应用程序进行基本登录,但在此之前,我想测试.php脚本。

我在想,当我按下登录按钮时,我应该会成功,但我得到了无效的凭据,我知道用户名和密码是正确的

下面是我的login.php脚本。

require("config.inc.php");
if (!empty($_POST)) {
    //gets user's info based off of a username.
    $query = "SELECT id, username, passwrd
            FROM application_users
            WHERE
                username = :username
        ";
    $query_params = array(
        ':username' => $_POST['username']
    );
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message.
        //die("Failed to run query: " . $ex->getMessage());
        //or just use this use this one to product JSON data:
        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));
    }
    //This will be the variable to determine whether or not the user's information is correct.
    //we initialize it as false.
    $validated_info = false;
    //fetching all the rows from the query
    $row = $stmt->fetch();
    echo $row;
    if ($row) {
        //if we encrypted the password, we would unencrypt it here, but in our case we just
        //compare the two passwords
        if ($_POST['password'] === $row['password']) {
            $login_ok = true;
        }
    }
    // If the user logged in successfully, then we send them to the private members-only page
    // Otherwise, we display a login failed message and show the login form again
    if ($login_ok) {
        $response["success"] = 1;
        $response["message"] = "Login successful!";
        die(json_encode($response));
    } else {
        $response["success"] = 0;
        $response["message"] = "Invalid Credentials!";
        die(json_encode($response));
    }
} else {
?>
    <h1>Login</h1>
    <form action="login.php" method="post">
        Username:<br />
        <input type="text" name="username" placeholder="username" />
        <br /><br />
        Password:<br />
        <input type="password" name="password" placeholder="password" value="" />
        <br /><br />
        <input type="submit" value="Login" />
    </form>
    <a href="register.php">Register</a>
    </form>
<?php

}?>

因此,当脚本加载并且我从远程MYSQL服务器输入值时,消息将作为无效凭据返回。我只想确保我的登录成功,然后再转到android部分,这本身就是一个很大的待办事项。

我还没有机会用真实的数据库测试它,但这应该可以工作。您仍然需要在文件顶部添加require("config.inc.php");,我已经添加了一个自定义数据库连接。我还使用PDO,所以查询看起来可能与您迄今为止使用的不同。

<?php
// Database connection
try
{
    $db = new PDO('mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword');
    $db->exec('SET CHARACTER SET UTF8');
}
catch (Exception $e)
{
    //Message in case of error when connecting to the database
    die('Erreur : ' . $e->getMessage());
}
// *** End database connection


$username = ""; // Initialize value in order to keep its value so the user can still see it in his form
if (isset($_POST['login'])) { // if the "login" button is pressed
    $username = $_POST['username']; // retrieve username value from the form
    $password = $_POST['password']; // retrieve password value from the form
    /* 
    *   If a username is unique then a way to do it is to count how many times
    *  the couple with this username and this password appears in our database.
    */
    $query = $db->prepare("SELECT COUNT(*) userAmount ".
                            "FROM application_users ".
                            "WHERE username = $username ".
                            "AND password = $password;");
    $query->execute();
    $query->closeCursor();
    $resultAmount = $query->fetch();
    if ($resultAmount['userAmount'] == 0){ // If the couple username-password is unfound
        $message = "Username or password unknown";
    } else {
        $message("Login successful");
    }
}
?>

<h1>Login</h1>
<form action="login.php" method="post">
    Username:<br />
    <input type="text" name="username" placeholder="username" value="<?php echo($username); ?>" />
    <br/><br/>
    Password:<br/>
    <input type="password" name="password" placeholder="password" value="" />
    <br/><br/>
    <input type="submit" name="login" value="Login" />
<a href="register.php">Register</a>
</form>