无法使用PHP登录mySQL帐户


Unable to log into account on mySQL using PHP

我在试图让我创建的网站的帐户登录时遇到了很多麻烦。我使用PHP在树莓pi上登录访问SQL数据库。它正在发挥作用。我会记账;但是,我无法登录它们。

我正在使用这里的代码

我调整了名字,但大部分都是一样的。至于我的名为Users的数据库,它只是一个名为Users的表中的ID(int)、用户名(varchar)和密码(char)。这是我的登录页面代码,它将信息发送到login_submit。

这是登录页面

<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Here</h2>
<form action="login_submit.php" method="post">
<fieldset>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username" value="" maxlength="20" />
</p>
<p>
<label for="password">Password</label>
<input type="text" id="password" name="password" value="" maxlength="20" />
</p>
<p>
<input type="submit" value="Login" />
</p>
</fieldset>
</form>
</body>
</html>

这是登录提交页面:

<?php
/*** begin our session ***/
session_start();
/*** check if the users is already logged in ***/
if(isset( $_SESSION['user_id'] ))
{
    $message = 'Users is already logged in';
}
/*** check that both the username, password have been submitted ***/
if(!isset( $_POST['username'], $_POST['password']))
{
    $message = 'Please enter a valid username and password';
}
/*** check the username is the correct length ***/
elseif (strlen( $_POST['username']) > 20 || strlen($_POST['username']) < 4)
{
    $message = 'Incorrect Length for Username';
}
/*** check the password is the correct length ***/
elseif (strlen( $_POST['password']) > 20 || strlen($_POST['password']) < 4)
{
    $message = 'Incorrect Length for Password';
}
/*** check the username has only alpha numeric characters ***/
elseif (ctype_alnum($_POST['username']) != true)
{
    /*** if there is no match ***/
    $message = "Username must be alpha numeric";
}
/*** check the password has only alpha numeric characters ***/
elseif (ctype_alnum($_POST['password']) != true)
{
        /*** if there is no match ***/
        $message = "Password must be alpha numeric";
}
else
{
    /*** if we are here the data is valid and we can insert it into database ***/
    $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
    /*** now we can encrypt the password ***/
    $password = sha1( $password );
    /*** connect to database ***/
    /*** mysql hostname ***/
    $mysql_hostname = 'localhost';
    /*** mysql username ***/
    $mysql_username = 'root';
    /*** mysql password ***/
    $mysql_password = 'raspberry';
    /*** database name ***/
    $mysql_dbname = 'Users';
    try
    {
        $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
        /*** $message = a message saying we have connected ***/
        /*** set the error mode to excptions ***/
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        /*** prepare the select statement ***/
        $stmt = $dbh->prepare("SELECT user_id, username, password FROM users 
                    WHERE username = :username AND password = :password");
        /*** bind the parameters ***/
        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
        $stmt->bindParam(':password', $password, PDO::PARAM_STR, 40);
        /*** execute the prepared statement ***/
        $stmt->execute();
        /*** check for a result ***/
        $user_id = $stmt->fetchColumn();
        /*** if we have no result then fail boat ***/
        if($user_id == false)
        {
                $message = 'Login Failed';
        }
        /*** if we do have a result, all is well ***/
        else
        {
                /*** set the session user_id variable ***/
                $_SESSION['user_id'] = $user_id;
                /*** tell the user we are logged in ***/
                $message = 'You are now logged in';
        }

    }
    catch(Exception $e)
    {
        /*** if we are here, something has gone wrong with the database ***/
        $message = 'We are unable to process your request. Please try again later"';
    }
}
?>
<html>
<head>
<title>PHPRO Login</title>
</head>
<body>
<p><?php echo $message; ?>
</body>
</html>

非常感谢您的帮助。非常感谢。

对于记录,我能够让它工作!昨晚我一直工作到凌晨5点,试图把问题解决。然后,我今天早上醒来,不到十分钟就把它修好了。我想是新鲜的眼睛。

无论如何,对于任何想要使用此代码的人来说,我的问题是,当实际列名为id时,我的查询正在名为user_id的列中搜索user_id。

要自己使用这些代码,请创建与我相同的数据库/表。

CREATE DATABASE Users;

USE Users;

CREATE TABLE users (id int(10) NOT NULL auto_increment, username varchar(20) NOT NULL, password char(40) NOT NULL, PRIMARY KEY (id), UNIQUE KEY (username));

你可以走了。如果有人想让我发布带有这些变量的添加用户部分,请告诉他们。或者您可以在上面的链接中找到原始代码。(必须更改变量。不要像我一样错过ID字段。)

谢谢大家,

Peter

这是我最近为一个小网站的管理员登录页面使用的东西。这与创建用户登录的过程基本相同。您只需要连接到用户数据库或用户表(而不是管理员),并在身份验证成功后重定向到用户配置文件页面(您可以创建一个,类似于管理员页面)。如果你有时间玩一玩,你可以在xampp上测试一下。

以下是链接:http://www.intechgrity.com/login-logout-and-administrate-using-php-session-cookie-mysql-version-2-with-remember-me-option/#

这也是一个关于如何构建一个简单的管理员登录页面的很好的教程。

这不是一个完美的答案,但这个项目的一些部分可能会有所帮助。