2013-10-29 65 views
-5

所以我想做一個基本的網站,你可以在那裏註冊和登錄,所有這一切。註冊完好無損,它將記錄保存在數據庫中,如果您擁有正確的用戶名和密碼,則登錄無誤。如果您沒有填寫所有字段,則會返回到loginform.php頁面並顯示「登錄失敗!」。這也適用,沒關係。但是,如果您沒有輸入正確的信息,則應該只是回顯「不正確!」。出於某種原因,而不是迴應我所說的內容,它只是進入一個空白的白頁。這是登錄表單代碼:我的登錄頁面不會返回任何東西

<?php 
    echo include("php_includes/header.php"); 
?> 

<h3 class="first">Member Login</h3>   
<form name="loginform" method="post" action="login.php"> 
<strong></strong><br> 
Username: <input name="username" type="text" id="username" /><br><br> 
Password: <input name="password" type="password" id="password" /><br><br> 
<input type="submit" name="Submit" value="Login" /></form><br> 
<a href='register.php'>Register</a> 

<?php echo include("php_includes/footer.php");?> 

這是實際的登錄過程的代碼:

<?php 

session_start(); 
include ("db_connect.php"); 

$username = $_POST["username"]; 
$password = $_POST["password"]; 

if($username && $password) 
{ 
    $password = md5($password); 
    $queryget = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'"); 
    $numrows = mysql_num_rows($queryget) or die(mysql_error()); 
    if($numrows != 0) {//if they can find it in the database 
    $_SESSION['username'] = $username; //starts the session 
    header("Location: members.php"); //log in 
    } else { 
     echo "Incorrect!"; 
    } 
} else { 
    echo "Login failed!"; 
    include("loginform.php"); 
} 
+0

空白頁通常表示服務器錯誤。檢查你的服務器日誌 – 2013-10-29 22:40:32

+0

你通常不會回顯'include'文件,它不需要放在括號內。 – PHPglue

+1

我在這裏聞到SQL注入... –

回答

0

試試這個:

<?php 
session_start(); 
include ("db_connect.php"); 

$errors = []; 

// Assigning the variables 
if (isset($_POST['username']) && !empty($_POST['username'])) { 
    $username = $_POST['username']; 
} else { 
    $errors[] = "Username cannot be blank <br />"; 
} 

if (isset($_POST['password']) && !empty($_POST['password'])) { 
    $password = $_POST['password']; 
} else { 
    $errors[] = "Password cannot be blank <br />"; 
} 

if(isset($username) && isset($password)) 
{ 
    $password = sha1($password); 
    $queryget = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'"); 
    $numrows = mysql_num_rows($queryget) or die(mysql_error()); 
    if($numrows != 0) {//if they can find it in the database 
    $_SESSION['username'] = $username; //starts the session 
    header("Location: members.php"); //log in 
    exit; 
    } else { 
    $error[] = 'Incorrect Username or password'; 
    } 
} else { 
    foreach ($errors as $error) { 
    echo $error . "<br />"; 
    } 
} 

希望這有助於。