2016-04-20 62 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的地方。