2013-02-13 73 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