2014-03-06 204 views
-3

我是PHP新手,login.php讓你使用你的用戶名而不是電子郵件。你如何使我的用戶登錄只需通過電子郵件,密碼而不是用戶名和密碼。用戶註冊/登錄?

的login.php

<?php 
session_start(); 
// Header file 
require_once "views/template/header.php"; 

if ($_GET["op"] == "login") 
{ 
if (!$_POST["username"] || !$_POST["password"]) 
{ 
die("You need to provide your e-mail and password."); 
} 

// Create query 
$q = "SELECT * FROM `users` " 
."WHERE `username`='".$_POST["username"]."' " 
."AND `password`=PASSWORD('".$_POST["password"]."') " 
."LIMIT 1"; 
// Run query 
$r = mysql_query($q); 

if ($obj = @mysql_fetch_object($r)) 
{ 
// Login good, create session variables 
$_SESSION["valid_id"] = $obj->id; 
$_SESSION["valid_user"] = $_POST["username"]; 
$_SESSION["valid_time"] = time(); 

// Redirect to member page 
Header("Location: members.php"); 
} 
else 
{ 
// Login not successful 
die("Sorry, could not log you in. Wrong login information."); 
} 
} 
else 
{ 
//If all went right the Web form appears and users can log in 
echo "<form action=\"?op=login\" method=\"POST\">"; 
echo "Username: <input name=\"username\" size=\"15\"><br />"; 
echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />"; 
echo "<input type=\"submit\" value=\"Login\">"; 
echo "</form>"; 
echo "Don't have account <a href='register.php'>create account now!</a>"; 
} 
require_once "views/template/footer.php"; 
?> 

Register.php

 <?php 
    // dbConfig.php is a file that contains your 
    // database connection information. This 
    // tutorial assumes a connection is made from 
    // this existing file. 
     require_once "views/template/header.php"; 

     //Input vaildation and the dbase code 
    if ($_GET["op"] == "reg") 
     { 
     $bInputFlag = false; 
     foreach ($_POST as $field) 
     { 
     if ($field == "") 
     { 
     $bInputFlag = false; 
     } 
     else 
     { 
     $bInputFlag = true; 
     } 
     } 
    // If we had problems with the input, exit with error 
    if ($bInputFlag == false) 
     { 
     die("Problem with your registration info. " 
     ."Please go back and try again."); 
     } 
     $profile=$_POST['profilename']; 
     $password=$_POST['password']; 
     $email=$_POST['email']; 
     $fname=$_POST['firstname']; 
     $lname=$_POST['lastname']; 
    // Fields are clear, add user to database 
    // Setup query 
    $q = "INSERT INTO users (`profilename`,`password`,`email`,`firstname`,`lastname`) 
      VALUES ('$profile','$password','$email','$fname','$lname')"; 
    // Run query 
    $r = mysql_query($q); 

    // Make sure query inserted user successfully 
    if (!mysql_insert_id()) 
     { 
     die("Error: User not added to database."); 
     } 
    else 
     { 
     // Redirect to thank you page. 
     Header("Location: register.php?op=thanks"); 
     } 
    } // end if 


    //The thank you page 
    elseif ($_GET["op"] == "thanks") 
    { 
    echo "<h2>Thanks for registering!</h2>"; 
    } 

    //The web form for input ability 
    else 
    { 
    echo "<form action=\"?op=reg\" method=\"POST\">\n"; 
    echo "Profile Name: <input name=\"profilename\" MAXLENGTH=\"16\"><br />\n"; 
    echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n"; 
    echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n"; 
    echo "First Name: <input name=\"firstname\" MAXLENGTH=\"25\"><br />\n"; 
    echo "Last Name: <input name=\"lastname\" MAXLENGTH=\"25\"><br />\n"; 
    echo "<input value='Submit' type=\"submit\">\n"; 
    echo "</form>\n"; 
    } 
    // EOF 
    require_once "views/template/footer.php"; 
?> 
+4

更改'Header(「Location:members.php」);'到'Header(「Location:http://www.php.net」);' –

+0

您需要同時在您的GET和POST信息中設置前兩個if-conditions是不可能的。如果沒有GET信息,你會看到這個表單,如果有GET信息,'!_ POST ['username']'將評估爲真,並執行die()。 –

+0

@baerbjoern,雖然它不是REST式正確的,但您可以簡單地將'?yourgetvariable = value'放在** post **表單方法的末尾。 –

回答

1

只要看看郵件,而不是用戶名:

// Create query 
$q = "SELECT * FROM `users` " 
."WHERE `email`='".$_POST["username"]."' " 
."AND `password`=PASSWORD('".$_POST["password"]."') " 
."LIMIT 1"; 

,改變你的表單標籤:

echo "Email: <input name=\"username\" size=\"15\"><br />"; 

這是最快的修復方法。顯然要更徹底,您需要用電子郵件替換您的登錄腳本中的「用戶名」或$_POST['username']的所有實例,而you should stop using the mysql_* library因爲它是deprecated and soon to be removed