2015-12-17 45 views
1

我遇到了一个麻烦的世界,试图让我登录的帐户访问我的网站。我使用PHP来记录在树莓派上访问SQL数据库。它正在工作。我可以做账;但是,我无法登录到它们。无法使用PHP在mySQL上登录帐户

我使用的代码here

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

这里是登录页面

<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> 

这里是Login_submission页:

<?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> 

任何帮助是极大的赞赏。谢谢!

+0

'mysql'在本地主机上运行?你尝试没有密码? – chriz

+4

你看到了什么错误?这会有很大的帮助。 –

+1

请给出你得到的错误。谢谢 – Thomas

回答

0

对于记录,我能够得到它的工作!我一直工作到凌晨5点,试图让昨晚的问题得到解决。然后,我今天早上醒来并在十分钟内修复它。我猜想,新鲜的眼睛。

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

要自己使用此代码,请创建我拥有的相同数据库/表。

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字段像我一样。)

谢谢大家,

彼得

0

这是我最近使用管理员登录页面我正在写一个小型网站。这与创建用户登录的过程基本相同。您只需连接到用户数据库或用户表,而不是管理员,并在身份验证成功时重定向到用户个人资料页面(您可以创建一个,如管理员页面)。如果你有时间玩一点,你可以在xampp上测试它。

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

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

这不是您问题的完美答案,但该项目的某些部分可能会有所帮助。

+0

谢谢!我必须检查一下。这个项目是晚上到期的,所以我没有时间去改变它,但我一定会考虑以后再处理它! -Peter –

相关问题