2013-02-13 75 views
0

有人可以請幫我我的密碼重置表格是一個混亂,即時通​​訊真的是新的PHP和MySQL所以請不要怪我得到它錯了。但我真的需要一些幫助。密碼重置表格mysql和php

基本上它都可以工作,它可以讓用戶輸入一個電子郵件,然後將它們發送給他們一個新的密碼。 (我喜歡附上發送電子郵件/密碼位代碼,因爲我不需要幫助那個位)唯一的問題是它不檢查電子郵件,它不檢查電子郵件是否存在於我的數據庫中,它允許用戶輸入在他們想要的任何郵件中。但如果他們輸入的電子郵件不在數據庫中,我希望它說對不起,沒有該類型的電子郵件存在或什麼的。我需要知道我怎樣才能檢查用戶輸入的電子郵件實際上是否存在。

然後要麼說,發送電子郵件或有問題。

<?php 
    $page_title = "Login"; 
    include('includes/header.php'); 
    include ('includes/mod_login/login_form2.php'); 

    if(!isset($_GET['err']) || isset($_GET['err'])=='') { 
    $_GET['err'] = '0'; 
    }  
    ?> 

    <? 
    $html_form = "<form name=\"forgotpasswordform\" action=\"sendpassword.php\" method=\"post\"> 
       <table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%\"> 
       <tr> 
        <td width=\"15%\">Email Address:</td> 
        <td><input name=\"forgotpassword\" type=\"text\" value=\"\" id=\"forgotpassword\" size=\"30\"/></td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
        <td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"Submit\" class=\"mainoption\" /></td> 
       </tr> 
       </table> 
      </form>"; 


    if ($_GET['err'] == '1') { //error if message is not filled ?> 

      <? echo $html_form; ?> 

     <? 

    } else if ($_GET['err'] == '2') { // error if email is not found 

     ?> 
      <h1>&nbsp;</h1> 
      <p> 
      Email Not Found! 
      </p> 
      <br /> 
     <? 

    } else if ($_GET['err'] == '3') { // EMAIL SENT :D 

     ?> 
      <h1>&nbsp;</h1> 
      <p> 
      Thanks! Your new password has been sent. 
      </p> 
      <br /> 
     <? 

    } else if ($_GET['err'] == '0') { // otherwise print email form 
     ?> 


      <? echo $html_form; ?>  


     <? 
    } 

    ?> 

那麼我們鏈接到sendpassword.php:

<?php 

/** 
* ShuttleCMS - A basic CMS coded in PHP. 
* Password Reset - Used for allowing a user to reset password 
* 
* @author Dan <[email protected]> 
* @version 0.0.1 
* @package ShuttleCMS 
*/ 
define('IN_SCRIPT', true); 
// Start a session 
session_start(); 

//Connect to the MySQL Database 
include 'includes/_config/connection.php'; 


/* 
* Checks form field for email 
*/ 

if ($_POST['forgotpassword']=='') { 
     header('Location: http://localhost/ptb1/recover_password.php?err=1'); 
} 
if(get_magic_quotes_gpc()) { 
     $forgotpassword = htmlspecialchars(stripslashes($_POST['forgotpassword'])); 
} 
else { 
     $forgotpassword = htmlspecialchars($_POST['forgotpassword']); 

} 


/* 
* Checks if the email exists 
*/ 

$sql = "SELECT COUNT(*) FROM ptb_users WHERE email = '$forgotpassword'"; 
$result = mysql_query($sql)or die('Could not find member: ' . mysql_error()); 

if (!mysql_result($result,0,0)>0) { 
    header('Location: http://localhost/ptb1/recover_password.php?err=2'); 
} 
+1

亂七八糟的一點是輕描淡寫。你爲什麼這樣做,這是非常可怕的[SQL注入漏洞](http://bobby-tables.com/),而不是使用[流行的PHP框架](http://www.phpframeworks。 COM /前10名的PHP的框架/)?需要注意的是,新應用程序絕對不應該使用'mysql_query',因爲它將在未來版本的PHP中被刪除,並且如果使用不正確會非常危險。 – tadman 2013-02-13 21:30:21

+0

$ _GET ['err']是如何設置的,爲什麼它是GET? – 2013-02-13 21:31:12

回答

1

您正在尋找這樣的事情:

if(mysql_num_rows($result)) { 
    // user found 
} else { 
    // no user found 
} 

記住你的代碼是開放的SQL注入和你不應該使用mysql_擴展名。改爲使用PDO

+0

PDO比'mysql_query'更好。 – tadman 2013-02-13 21:33:34