2016-04-20 60 views
1

我确实收到一封电子邮件重置我的密码,但每当我点击链接,它应该去我的“change_password.php”,但它直接到我的“index.php “这使得用户无法更改或重置密码。链接发送邮件重置密码不起作用

我的链接包含我设置的idcode,每当发送邮件时数据库中的代码都会更新,但链接不起作用。

这是我的链接是什么样子:

mywebsite.com/change_password.php?user_id=29 &键= 233602

但是,当我点击,就应该直接我这个页面,我的change_password.php

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

include 'lib/password.php'; 
$db = new mysqli('localhost', 'user', 'pass', 'db'); 
if($_GET['user_id']!="" && $_GET['key']!=""): 
    $id=mysqli_real_escape_string(trim($db,$_GET['user_id'])); 
    $code=mysqli_real_escape_string(trim($db,$_GET['key'])); 
    $fetch=$db->query("SELECT * FROM `profile` WHERE id='$id' AND `code` = '$code'"); 
    $count=mysqli_num_rows($fetch); 
    if($count!=1) : 
     header("Location:index.php"); 
    endif; 
else : 
    header("Location:index.php"); 
endif; 
?> 
    <?php include 'home-header.php'; ?> 
</head> 
<body> 
<?php include 'home-navbar.php'; ?> 

     <div class="modal-dialog"> 
     <h2>Forgot Password Recovery</h2> 

     <div class="modal-content col-md-8"> 
     <div class="modal-header"> 
     <h4 class="modal-title"><i class="icon-paragraph-justify2"></i> Change New Password</h4> 
     </div> 
     <form method="post" autocomplete="off" id="password_form"> 
      <div class="modal-body with-padding">        
      <div class="form-group"> 
      <div class="row"> 
       <div class="col-sm-10"> 
       <label>New Password</label> 
       <input type="password" id="passwords" name="password" class="form-control required"> 
       </div> 
      </div> 
      </div> 
      <div class="form-group"> 
      <div class="row"> 
      <div class="col-sm-10"> 
       <label>Confirm password</label> 
       <input type="password" id="cpassword" name="cpassword" title="Password is mismatch" equalto="#passwords" class="form-control required" value=""> 
      </div> 
      </div> 
      </div>   
      </div> 
      <div id="error_result"></div> 
      <!-- end Add popup --> 
      <div class="modal-footer"> 
      <input type="hidden" name="id" value="<?php echo $id; ?>" id="id"> 
      <button type="submit" id="btn-pwd" class="btn btn-primary">Submit</button>    
      </div> 
     </form>   
     </div>   
     </div> 

<?php include 'home-footer.php';?> 
<script> 
    $(document).ready(function(){ 
    $(document).on('click','#btn-pwd',function(){ 
     var url = "new_password.php";  
     if($('#password_form').valid()){ 
     $('#error_result').html('<img src="ajax.gif" align="absmiddle"> Please wait...'); 
     $.ajax({ 
     type: "POST", 
     url: url, 
     data: $("#password_form").serialize(), 
      success: function(data) {      
      if(data==1) 
      { 
       $('#error_result').html('Password reset successfully.'); 
       window.setTimeout(function() { 
       window.location.href = 'index.php?sucess=1'; 
       }, 1000); 
      } 
      else 
      { 
       $('#error_result').html('Password reset failed. Enter again.');    
      } 
      } 
     }); 
     } 
     return false; 
    }); 
}); 
</script> 

但它不指挥我该网页,它进入我index.php的那一刻我点击次e链接。为什么?

我的PHP邮件:

<?php 
    $db = new mysqli('localhost', 'user', 'pass', 'db'); 
    if($_POST['email']!=""): 
     $email=mysqli_real_escape_string($db,$_POST['email']); 
     $db_check=$db->query("SELECT * FROM `profile` WHERE email='$email'"); 
     $count=mysqli_num_rows($db_check); 
     if($count==1) : 
     $row=mysqli_fetch_array($db_check); 
     $code= rand(10000,1000000); 
     $link = 'mywebsite.com/change_password.php?user_id='.$row['id'].'&key='.$code;   
     $fetch=$db->query("UPDATE `profile` SET `code` = '$code' WHERE `email`='$email' "); 
     $to="$email"; 
     $strSubject="Password Recovery Link"; 
     $message = '<p>Password Recovery Link : '.$link.'</p>' ;    
     $headers = 'MIME-Version: 1.0'."\r\n"; 
     $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; 
     $headers .= "From: [email protected]";    
     $mail_sent=mail($to, $strSubject, $message, $headers); 
      if($mail_sent) echo 1; 
      else echo 0; 
     else : 
     echo 0; 
     endif; 
    else : 
     header("Location:index.php"); 
    endif; 
?> 
+0

哪里是你的代码的PHP'邮件'部分? – andre3wap

+0

哦好吧,我会发布它的先生。 @ andre3wap – Felix

+0

这条语句的结果是什么:'SELECT * FROM profile WHERE id = '29'AND code ='233602''?如果除了返回的一行之外还有其他东西,你可以期待'header(“Location:index.php”);'重定向你。 –

回答

6

其他人正在查看if声明,不知其是否是您的trim调用是问题所在。

改变这种

$id=mysqli_real_escape_string(trim($db,$_GET['user_id'])); 
$code=mysqli_real_escape_string(trim($db,$_GET['key'])); 

$id=mysqli_real_escape_string($db,trim($_GET['user_id'])); 
$code=mysqli_real_escape_string($db,trim($_GET['key'])); 

你 “微调” $db,而不是你的变量。

+0

好听的。 –

+0

所有这一次,这是我的错误@@我错位了修剪。非常感谢您的注意!这两行谢谢你! :d – Felix

1

这应该是相对直接的解决。试试这个:

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

include 'lib/password.php'; 
echo 'STILL GOOD!'; 
exit; 
$db = new mysqli('localhost', 'user', 'pass', 'db'); 
if($_GET['user_id']!="" && $_GET['key']!=""): 
    echo 'DATA!'; 
    exit; 
    $id=mysqli_real_escape_string(trim($db,$_GET['user_id'])); 
    $code=mysqli_real_escape_string(trim($db,$_GET['key'])); 
    $fetch=$db->query("SELECT * FROM `profile` WHERE id='$id' AND `code` = '$code'"); 
    $count=mysqli_num_rows($fetch); 
    if($count!=1) : 
     echo 'NO ROWS!'; 
     exit; 
     header("Location:index.php"); 
    endif; 
else : 
    echo 'NO DATA!'; 
    exit; 
    header("Location:index.php"); 
endif; 

注意附加echo S和exit语句。点击你的链接,看看你收到哪条消息,删除并重复,直到你缩小失败的位置。

正如@ imvain2指出的那样,mysqli_real_escape_string(trim($db,$_GET['user_id']))应该是mysqli_real_escape_string($db, trim($_GET['user_id'])),因此这是您的SQL查询无法提取任何结果并导致重定向到index.php的地方。