忘记密码成功连接到电子邮件

时间:2014-10-21 01:10:58

标签: php mysql email

我想将我忘记的密码连接到客户端电子邮件地址,我会尝试一下,但这不起作用。请帮忙,也许我的代码有问题。

这是我的PHP代码:

forgotpassform.php

 <?php
    session_start();
    require_once('config.php');

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
      mysql_select_db($mysql_database, $bd) or die("Could not select database");

$email=$_POST['email'];
$username=$_POST['username'];

$sql = mysql_query("SELECT email, fname, lname, password, minitial, username FROM reg_form_students where username='$username'") 
or die(mysql_error());  
$row = mysql_fetch_array($sql);
$rownum = mysql_num_rows($sql);

 $to= $row['email']; 
 $subject= "Online Library System Password Retrieval MESSAGE"; 
if(!$rownum  ) {
echo "We can not find your email";
}
if($rownum ==1  ){

$message   .= "Your account details:" . "\n\n";

$message   .= "Username:". "   " . $row['username']. "\n\n";

$message   .= "Name:". "   " . $row['lname'] . ", ". $row['fname'] . ", ". $row['minitial'] . "\n\n";

$message   .= "Password:". "   " . $row['password']. "\n\n";

 $header = "";    

 $sent =  mail($to,$subject,$message,$header);


if($sent)
{
print "Your password has been sent to your e-mail address </br>" ;

}

}
?>

和我的HTML:

forgotpass.php

<!doctype html>
 <html>
  <head id="headm">
   <meta charset="utf-8">
<link rel="shortcut icon" href="images/logo.png">
<link rel="stylesheet" type="text/css" href="Styles/Master.css"  />
<link rel="stylesheet" type="text/css" href="Styles/menu.css"  />
<link href="Styles/demo7.css" type="text/css" rel="stylesheet" />
<script src="Scripts/jquery.slides.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-2.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript" src="Scripts/jquery.js"></script>
<script type="text/javascript" src="Scripts/menu.js"></script>


<title>Saint Francis of Assisi College Online Library System</title>

<style type="text/css">

html { background:#000; }
div#menu { margin:65px 0 0 40px; }
</style>
<script>
              $(function () {
            $(document).scroll(function () {
                if ($(this).scrollTop() != 0) {
                    $('#toTop').fadeIn();
                } else {
                    $('#toTop').fadeOut();
                }
            });

            $('#toTop').click(function () {
                $('body,html').animate({ scrollTop: 0 }, 800);
            });
        });

</script>
</head>


<body>
<div id="toTop">Back to Top</div>

<div class="Master">

        <div class="Header">
            <div class="HeaderContent">
                        <div class="quicklinks">
                        <ul class="qlinks" style="margin-right: 0px;">
                        <li><a href="libsystem.php" target="_blank" onclick="_gaq.push(['_trackEvent', 'Header', 'Library']);
">Our Library</a></li>
                        <li class="divider">|</li>
                        <li class="drop" style="width:auto;">About Library System

                            <div class="down">
                                <div>
                                <a href="TeamPaper.html" title="TeamPaper">Developers</a>
                                <a href="Advice.html" title="User Friendly">For Users</a>
                                 <a href="mvision.html" title="Mission and Vision">Mission and Vision</a>

                                </div>
                            </div>
                        </li>

                        <li class="divider">|</li>
                        <li class="drop">OtherLinks

                            <div class="down">
                                <div>
                                <a href="http://www.stfrancis.edu.ph/" target="_blank" onclick="_gaq.push(['_trackEvent', 'Header', 'SFAC Main']);
">SFAC Main Website</a>

                                <a href="http://stfrancisbacoor.site40.net/" target="_blank" onclick="_gaq.push(['_trackEvent', 'Header', 'SFAC Bayanan']);
">SFAC Bayanan</a>


                                </div>
                            </div>
                        </li>
                        <li class="divider">|</li>
                        <li><a href="myprofile.php" target="_blank" onclick="_gaq.push(['_trackEvent', 'Header', 'MyProfile']);
">MyProfile</a></li>
                        <li class="divider">|</li>
                        <li><a href="https://hotmail.com/" target="_blank" onclick="_gaq.push(['_trackEvent', 'Header', 'myE-Mail']);
">MyE-Mail</a></li>
                    </ul>
                </div>
            </div>
        </div>
      </div>


      <div class="main"> 
            <div class="banner">
<table width="1160" height="858" border="0">
  <tr>
  <div class = "header">
    <td width="229" height="146" align="center"><a href = "index.html" title = "SFAC Homepage"><img src="images/SFAC.png" width="147" height="142"></a></td>
    <td width="1015">&nbsp;<img src = "images2/banner4.jpg" width="900"></td>

    </div>
  </tr>
  <tr>
    <td align="left" valign="top"><table width="160" height="170" border="0">
    <tr>
     <div id="menu">
    <ul class="menu">
        <li><a href="index.html"><span>Home</span></a></li>
        <li><a href="gallery.php"><span>Gallery</span></a></li>
        <li><a href="search.php"><span>Search Books</span></a></li>
        <li class="last"><a href="aboutus.php" class="parent"><span>About</span></a>
        <div><ul>
                <li><a href="mvision.html"><span>Mission and Vision</span></a></li>
                <li><a href="libsystem.php"><span>Our Library</span></a></li>
                <li><a href="TeamPaper.html"><span>Developers</span></a></li>
                <li><a href="Advice.html"><span>For Users</span></a></li>
            </ul></div>
        </li>
    </ul>
</div>

</tr>
<tr>
<td>
</tr>
    </table>
      </td>
    <td align="left"><table width="900" height="117" border="0">
      <tr align="left">
        <td>&nbsp;

        <center>
        <h4 style="margin-left: 280px">Online Library System Password Retrieval</h4>
        <form action="<?php $_SERVER['PHP_SELF'];?>" method="POST" style="margin: 0px" >
        <fieldset>
        <p style="font-weight: bold; text-align:center">Please enter your username:</p><p style="text-align:center"><label for="username"><input name="username" type="text" /></label></p>
        <p style="text-align:center"><input name="submit" type="submit" value="Submit" /></p>
        </fieldset>
        </center>
        </form>

</td>
      </tr>
    </table>
      <div align="right"><br>
        <table width="213" height="419" border="0">
          <tr>

            <th width="267" align="center" valign="top" scope="col">&nbsp;
         </th>
          </tr>
        </table>
      </div>
      <p>&nbsp;</p>
    </td>
  </tr>
</table>
</div>
</div>
<div class = "area">
<div id = "footer">
<font color="#FF0000">Copyright &copy; TeamPaper 2013  </font>|<a href="#" title="Terms of Use "font-family: Arial;"> Terms of use</a>
</div>
</div>

</div>
</map>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

请确保,如果您在本地运行代码,请设置您的SMTP身份验证&amp; PHP.ini文件中的主机配置。

您的代码

if(!$rownum  ) {
echo "We can not find your email";
}
if($rownum ==1  ){

更改为:

if(!$rownum  ) {
echo "We can not find your email";
}
else
{
$to= $row['email']; 
$subject= "Online Library System Password Retrieval MESSAGE";

根据您的信息:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home/a4664463/public_html/forgotpass.php on line 144

问题似乎从您的身份验证到连接到数据库。确保您输入的用户名和密码正确无误。密码。

尝试此更新:

if($rownum > 0)
{
$to= $row['email']; 
$subject= "Online Library System Password Retrieval MESSAGE";
}
else
{
echo "We can not find your email";
}

答案 1 :(得分:0)

将密码通过电子邮件发送给用户不是一个好方法,而是让用户通过发送提供的电子邮件地址的链接重置密码。

以下是mysqli编写的更好方法的版本。

创建一个名为“lost_password”的表,其中包含列,id,user_id,token。

以下脚本可能需要一些修复,但这比直接发送密码更好,一方面,因为您向用户发送密码,这意味着您在db中保存了普通密码,这是不推荐的。相反,你应该保存哈希值,如果你想这样做,你需要遵循以下方法。

<强> request_password.php

<?php
$db = new mysqli("localhost", "root", "password", "database_name");
if(isset($_POST['submit'])){
    $email = $_POST['email'];
    $stmt = $db->prepare("SELECT * FROM `users` where `email` = ?");
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $res = $stmt->get_result();
    if($res->num_rows < 1){
        echo "No such email has been found";
    }
    else{
    $fetch = $res->fetch_assoc();
    $userid = $fetch['id'];
    $token = bin2hex(openssl_random_pseudo_bytes(45));
    $url = 'http://yourwebsite.com/passwordreset.php?token='.$token;

    if(mail($email, $url, $from)){
    $stmt = $db->prepare("INSERT INTO `lost_password`(user_id, token) values(?,?)");
    $stmt->bind_param('is', $userid, $token);
    $stmt->execute();
    if($stmt->affected_rows == 1){
       echo "We have emailed you instructions on how to reset your password";
     } 
    else{
     echo "there was an error";
     }
   }
  }
}
?>
<form action="" method="post">
<<input type="email" name="email">
input type="submit" name="submit">
</form>

<强> reset_password.php

if($_GET['token']){
 $display_form = 0;
$stmt = $db->prepare("select * from `lost_password` where token = ? and `time` >= DATE_SUB(NOW(), INTERVAL 1 DAY");
 $stmt->bind_param('s', $_GET['token']);
 $stmt->execute();
 $result = $stmt->get_result();
 if($result->num_rows < 1){
    echo "link has expired or token doesn't match the one we have on record";
 }
 else{
     $display_form =1;
     if(isset($_POST['submit'])){
     $fetch = $res->fetch_assoc();
     $id = $fetch['id'];
     $hash = password_hash($newpass, PASSWORD_BCRYPT, array('cost' => 10));
     $stmt = $db->prepare("update `users` set `password` = ? where `id` = ?");
     $stmt->bind_param('ss', $hash, $id);
     $stmt->execute();
     if($stmt->affected_rows ==1){
        echo "you have reset your password successfully";
      }
   }
 }
}
if($display_form == 1){?>
    <form action="" method="post">
    <input type="text" name="newpass">
    <input type="submit" name="submit">
    </form>
<?php } ?>

答案 2 :(得分:0)

这是我的更新,我仍在努力并且它有效,但它显示“我们找不到您的电子邮件”。

<?php
    session_start();
    require_once('config.php');

$username=$_POST['fp'];

$sql = mysql_query("SELECT * FROM reg_form_students where User_Name='".$username."'") 
or die(mysql_error());  
$row = mysql_fetch_array($sql);
$rownum = mysql_num_rows($sql);

if($rownum > 0)
{
$to= $row['E_Mail']; 

$subject= "SFAC-OPAC Password Retrieval MESSAGE";

$message   .= "Your account details:" . "\n\n";

$message   .= "Username:". "   " . $row['User_Name']. "\n\n";

$message   .= "Name:". "   " . $row['Last_Name'] . ", ". $row['Name'] . ", ". $row['Middle_Name'] . "\n\n";

$message   .= "Password:". "   " . $row['Password']. "\n\n";

$from = "From: admin@teampaper.com";    

mail($to,$subject,$message,$from);

echo "Your password has been sent to your e-mail </br>" ;

}

else
{
echo "We can not find your email";
}
?>