2015-12-17 47 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 –

相關問題