2009-12-10 50 views
0

我想創建一個簡單的登錄系統。當我運行登錄表單(使用正確的用戶名和密碼)時,它似乎無法運行php。有什麼建議麼?主要的PHP問題

<?php 
$host="linuxserver"; // Host name 
$username="jparry2"; // Mysql username 
$password=""; // Mysql password 
$db_name="jparry2"; // Database name 
$tbl_name="customer"; // Table name 

// Connect to server and select databse. 
mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysqli_query($sql); 

// Mysql_num_row is counting table row 
$count=mysqli_num_rows($result); 
// If result matched $myusername and $mypassword, table row must be 1 row 

if($count==1){ 
// Register $myusername, $mypassword and redirect to file 「login_success.php」 
session_register("myusername"); 
session_register("mypassword"); 
header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 

<html> 
<body> 

</body> 
</html> 

編輯添加的登錄表單代碼

<html> 
<head><title>Login</title></head> 
<body> 


<form action='checklogin.php' 
       method='POST' style='margin: .5in'> 
    <p><label for='user_name' style='font-weight: bold; 
      padding-bottom: 1em'>USER ID: </label> 
     <input type='text' name='myusername' id='myusername' 
      value='' /></p> 
    <p><label for='password' style= 'font-weight: bold'>Password: </label> 
     <input type='password' name='mypassword' id='mypassword' 
      value='' /></p> 
    <p><input type='submit' value='Login'> </p> 
     <input type='hidden' name='sent' value='yes'/> 

<a href= "/home/jparry2/public_html/register.php">Register</a> 

    </form> 

</body> 
</html> 
+1

是什麼瀏覽器的源代碼說呢?你在那看到什麼? – 2009-12-10 17:03:28

+1

我想你應該指定更多的信息,然後這個。例如你在使用什麼操作系統。什麼是問題?沒有顯示輸出。做一個簡單的<?phpinfo(); ?>工作? – Alfred 2009-12-10 17:06:35

+0

您可以爲您的登錄表單添加代碼嗎? – 2009-12-10 17:07:03

回答

5

如果瀏覽器提示您下載PHP文件時,它意味着不被調用PHP解釋器。即您沒有安裝或配置正確。

1

您是否收到任何錯誤訊息?對我來說似乎沒問題。您是否嘗試過在if-block中回顯某些內容?這可能會幫助你瞭解什麼是錯的。

有些東西你可以檢查或嘗試:

  • 你有錯誤報告嗎?
  • 把`var_dump($ _ POST);死();在頁面頂部查看是否正確提交了$ _POST變量。
  • 確保您不是header()功能之前任何輸出到瀏覽器。如果你有error_reporting關閉,你輸出的東西給瀏覽器,使用header()將導致致命的錯誤,可能會導致一個空白的白頁。

其他一些從你的代碼註釋:

  • 你並不需要把雙引號中的變量,他們對自己的工作:mysqli_select_db("$db_name")成爲mysqli_select_db($db_name)
  • 你並不需要stripslashes()如果你在做mysql_real_escape_string。後者將自行處理這項工作。
1

在某些瀏覽器中,Location標題是大小寫敏感的,因此您的通話header("location:login_success.php");可能不工作(上一個header documentation pagecomment表明,這種發生在IE7)。嘗試在位置中使用大寫l

+1

IIRC,在大多數瀏覽器標題中,請遵循標準:「:」後面的大寫名稱和空格。 – 2009-12-10 17:14:45

1

你沒有做任何「在session_start()」,所以你會無法使用。

也許你需要它在你的「login_success.php」腳本啓動。

1

我同意丹尼爾,通過在寫這篇的時間校正header("Location: login_success.php");

此外,作爲一個側面說明,因爲,它沒有明確解釋了沒有工作,但你加入會話變量,當您需要有session_start()

也可以嘗試使用$_SESSION['variable']因爲session_register()被棄用PHP 5.30從PHP: session_register採取的嘗試這樣的事情

if($count==1){ 
session_start(); 
// Register $myusername, $mypassword and redirect to file 「login_success.php」 
$_SESSION['username'] = $myusername; 
$_SESSION['mypassword'] = $mypassword; 
session_write_close(); // makes sure nothing was lost during redirect 
header('Location: nextpage.php'); 
}