2013-12-08 56 views
0

這裏是我做的一些代碼。如果我的用戶名/密碼都正確,我正在嘗試登錄到某個頁面 - 但是,即使用戶名/密碼是正確或錯誤,我仍會繼續獲取空白頁面!我有3個主要文件,我做了。 Home.php,enter.php和connection.php。我得到一個空白的屏幕上我的pdo登錄

下面是Home.php代碼:

<?php 
session_start(); 
?> 

<html> 
<h1 align="center">Welcome To Home Page</h1> 
<form method="POST"> 
<table border="0" align="center"> 

<tr> 

<td> 

<?php 
echo $_SESSION["firstname"]; 
if($_SESSION["firstname"]==null) 
{ 
?> 
    <a href="enter.php">Login</a> 
    <a href="registration.php">Registration</a> 
    <a href="enter.php">Logout</a> 
<?php 
} 
else 
{ 
?> 
<a href="enter.php">Logout</a> 
<?php 
} 
?> 
</td> 

</tr> 

<tr> 
<td> 
<img src="pix.jpg" width="500" length="500"/> 
</td> 

</table> 
</form> 
</html> 

下面是Enter.php代碼:

<html> 

    <form action="connection.php" method="POST"> 
         <ul> 
          <li> 
           Email: 
           <input type="text" maxlength="30" name="emails" /> 
          </li> 

          <li> 
           Password : 
           <input type="password" maxlength="30" name="passwords" /> 
          </li> 
          <input type="submit" maxlength="30" name="logins" value="Login" /> 

         </ul> 
        </form> 
    </html> 

下面是Connection.php代碼:

<?php 
function getConnection(){ 
$db = new PDO("mysql:host=localhost;dbname=demodatabase", "root","********"); 
$db ->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPION); 
return $db; 

$db = getConnection(); 
$stmt = $db->prepare(" 
    SELECT * FROM subscriber WHERE Email_id = :emails AND Password = :passwords 
"); 
$stmt->bindParam(":emails" , $users ); 
$stmt->bindParam(":passwords", $passwords); 
$stmt->execute(); 
$total = $db->rowCount(); 
$count = $stmt->rowCount(); 
    if ($count > 0) { 
    session_start(); 
    $_SESSION['firstname'] = 'yes'; 
    header('location: home.php'); 
    exit; 
    } 
} 
?> 

所以爲什麼我一直留空白頁?

+2

死亡白屏=錯誤檢查\顯示關閉 – 2013-12-08 21:00:50

+0

所以我怎麼解決這個問題? – user3047348

+0

一旦你向瀏覽器輸出任何東西,你就不能調用'session_start()'或'header()',除非我從你的示例代碼中弄錯了 - 看起來你是 –

回答

1

看看你的代碼:

<?php 
function getConnection(){ 
    // … 
} 
?> 

你有一個功能一切,永不調用。這裏需要一個閉環大括號:

function getConnection(){ 
    $db = new PDO("mysql:host=localhost;dbname=demodatabase", "root","********"); 
    $db ->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPION); 
    return $db; 
} // here 

$db = getConnection(); 

正確縮進代碼會有幫助。

在附註中,很明顯您以純文本格式存儲密碼。 千萬別這麼做!使用good password hashing algorithm來散列密碼!

此外,使用session_startheader後,東西已發送到瀏覽器是沒用的。在<?php之前觀看有害空間。

+0

現在我該怎麼辦?我收到以下錯誤:致命錯誤:帶有消息'SQLSTATE [42S02]的未捕獲異常'PDOException':未找到基本表或視圖:1146 C:\ xampp \ htdocs \ Registra2中'demodatabase.subscriber'表不存在' \ connection.php:13堆棧跟蹤:#0 C:\ xampp \ htdocs \ Registra2 \ connection。php(13):PDOStatement-> execute()#1 {main}在第13行拋出C:\ xampp \ htdocs \ Registra2 \ connection.php – user3047348

+0

爲什麼我得到上面的錯誤 - 讓我再次重複它 - 致命錯誤:未找到帶有消息'SQLSTATE [42S02]'的異常'PDOException':未找到基本表或視圖:1146 C:\ xampp \ htdocs \ Registra2 \ connection.php中的表'demodatabase.subscriber'不存在':13堆棧跟蹤: #0 C:\ xampp \ htdocs \ Registra2 \ connection.php(13):PDOStatement-> execute()#1 {main}拋出C:\ xampp \ htdocs \ Registra2 \ connection.php on line 13 – user3047348

+0

@ user3047348它是正是它所說的:你的表'用戶'不存在。您必須先創建它,然後才能從中檢索記錄。 –