PHP:重置密码代码问题

时间:2015-12-10 06:28:55

标签: php mysql

我在代码中遇到问题,因此当我收到邮件后点击链接时出现错误,指出无效的电子邮件链接。我将在下面发布我的代码供您参考。

forgotpassword.php:

<?php include('header.php'); ?>
<?php include('menu.php'); ?>
<?php
//file name: forgotpassword.php
//Title:Build your own Forgot Password PHP Script
if(!isset($_GET['email'])){
                  echo'
                    <h3>RESET PASSWORD</h3>
                    <hr>
                    <form action="forgotpassword.php">
                      <div class="col-md-12">
                      Enter Your Email Id:
                         <input type="text" name="email" />
                        <input type="submit" class="btn btn-primary" value="Reset My Password" />
                        </div>
                         </form>'; exit();
                   }
$email=$_GET['email'];
include("config.php");
$q="select recep_username from clinic_receptionist where recep_username='".$email."'";
$r=mysqli_query($conn,$q);
$n=mysqli_num_rows($r);
if($n==0){echo "Email id is not registered"; die();}
$token=getRandomString(10);
$q="INSERT into tokens (token,email) values ('$token','$email')";
mysql_query($q);
function getRandomString($length) 
   {
$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789";
$validCharNumber = strlen($validCharacters);
$result = "";

for ($i = 0; $i < $length; $i++) {
    $index = mt_rand(0, $validCharNumber - 1);
    $result .= $validCharacters[$index];
}
return $result;}

require_once('mailer/class.phpmailer.php');

                    $uri = 'http://'. $_SERVER['HTTP_HOST'].'/doctor' ;
                    $from_name = 'DOCTOR RIGHT';
                    $from = 'test@gmail.com';
                    $to = $email;
                    $to_name = 'Admin';
                    $message = 'Click on the given link to reset your password <a href="'.$uri.'/reset.php?token='.$token.'">Reset Password</a></p><br/>';

                    $message .= '<br>Regards<br>';
                    $message .= '<br>Doctor Right';

                    $mail = new PHPMailer();
                    //$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                    $mail->IsSMTP();    
                    $mail->Port = 465; // or 587                         // set mailer to use SMTP
                    $mail->Host = "ssl://smtp.mandrillapp.com";  // specify main and backup server
                    $mail->SMTPAuth = true;     // turn on SMTP authentication
                    $mail->Username = '****************';  // SMTP username
                    $mail->Password = '****************'; // SMTP password      // SMTP account password

                    $mail->SetFrom($from,$from_name );
                    $mail->AddReplyTo($from,$from_name );
                    $mail->Subject  = 'Password Change';
                    $mail->MsgHTML($message);
                    $mail->AddAddress($to,$to_name);

                    if(!$mail->Send())
                    {
                        echo "We have sent the password reset link to your  email id <b>".$to."</b>";
                    }

reset.php

<?php
session_start();
$token=$_GET['token'];
include("config.php");

if(!isset($_POST['password'])){
$q="select email from tokens where token='".$token."' and used=0";
$r=mysqli_query($conn,$q);
while($row=mysqli_fetch_array($r))
{
$email=$row['email'];
}
if ($email!=''){
      $_SESSION['email']=$email;
}
else die("Invalid link or Password already changed");}
$pass=$_POST['password'];
$email=$_SESSION['email'];
if(!isset($pass)){
echo '<form method="post">
enter your new password:<input type="password" name="password" />
<input type="submit" value="Change Password">
</form>
';}
if(isset($_POST['password'])&&isset($_SESSION['email']))
{
$q="update users set password='".md5($pass)."' where email='".$email."'";
$r=mysql_query($q);
if($r)mysql_query("update tokens set used=1 where token='$token'");echo "Your password is changed successfully";
if(!$r)echo "An error occurred";
}

我一直收到此错误Invalid link or Password already changed,当我查看令牌表以检查它是否已插入时,它不是。那么我的插入查询有问题吗?或者代码一般是错误的吗?

0 个答案:

没有答案