如果返回成功消息,则会话不起作用(代码已更新)

时间:2018-12-11 22:23:04

标签: php session

会话如果返回成功消息则不起作用。如果我直接echo,则此消息会话可以正常工作。但是,当我退回它时,它不起作用。返回邮件后,我需要销毁会话。我已经更新了User.php类和forgotpass.php页面

User.php

 <?php 
$filepath = realpath(dirname(__FILE__));
include_once ($filepath.'/../lib/Session.php');
include_once ($filepath.'/../lib/Database.php');
include_once ($filepath.'/../helpers/Formate.php');
 ?>

 <?php 

/
class User{
    private $db;
    private $fm;

    public function __construct(){

        $this->db = new Database();
        $this->fm = new Formate();

    }

*Password Change Method*/

  public function updatePass($userId, $data){
  $oldpass   = $this->fm->validation($data['oldpass']);
  $newpass   = $this->fm->validation($data['newpass']);
  $conpass   = $this->fm->validation($data['conpass']);

  $oldpass   = mysqli_real_escape_string($this->db->link,$oldpass);
  $newpass   = mysqli_real_escape_string($this->db->link,$newpass);
  $conpass   = mysqli_real_escape_string($this->db->link,$conpass);

  if (empty($oldpass) || empty($newpass) || empty($conpass)) {
  $errormsg = "<div class='alert alert-danger alert-dismissible'>
        <button type='button' class='close' data-dismiss='alert' aria- 
       label='Close'><span aria-hidden='true'>&times;</span></button>
      <strong>Error!</strong>  Filed must not be empty.
       </div>";
  return $errormsg;
 }else {
  $oldpass = md5($oldpass);
  $newpass = md5($newpass);
  $conpass = md5($conpass);
  $passquery = "SELECT password FROM tbl_user WHERE password = '$oldpass' 
  AND userId = '$userId' ";
  $passcheck = $this->db->select($passquery);
  if ($passcheck == false) {
  $errormsg = "<div class='alert alert-danger alert-dismissible'>
               <button type='button' class='close' data-dismiss='alert' 
              aria-label='Close'><span aria-hidden='true'>&times;</span> 
              </button><strong>Error!</strong>  The Password doesn't 
              exist.</div>";
   return $errormsg;
  }else {
  if ($newpass == $conpass ) {
    $query = "UPDATE tbl_user 
        SET 
        password = '$newpass' 
        WHERE userId = '$userId'";

        $updatepass = $this->db->update($query);

        if ($updatepass) {

         $successmsg ="<div class='alert alert-success alert-dismissible'>
                     <button type='button' class='close' data- 
                     dismiss='alert' aria-label='Close'><span aria- 
                     hidden='true'>&times;</span></button>
                    <strong>Success!</strong>  Password Changed 
                    Successfully.
                    </div>";
               return $successmsg;
               Session::destroy();
                echo '<script type="text/javascript">setTimeout(function() 
                {window.top.location="index.php"} , 5000);</script>';
                exit();


              }else {
              $errormsg = "<div class='alert alert-danger alert
                           dismissible'><button type='button' 
                           class='close' data-dismiss='alert' aria- 
                            label='Close'><span aria-hidden='true'>&times; 
                            </span></button> <strong>Error!</strong>  
                             Somthing went wrong.</div>";
                          return $errormsg;
                        }

                    }else {
                          $errormsg = "<div class='alert alert-danger 
                           alert-dismissible'><button type='button' 
                           class='close'data-dismiss='alert' aria- 
                           label='Close'><span aria-hidden='true'>&times; 
                            </span></button><strong>Error!</strong>  
                              Password does not match.
                           </div>";
                           return $errormsg;
                           }
                         } 
                       }

          }/*End Method*/

Forgotpass.php

  <?php include 'inc/header.php'; ?>
  <?php $userId = Session::get("userId"); ?>
 <?php
      if ($_SERVER['REQUEST_METHOD'] == 'POST' && 
         isset($_POST['submitpass'])) {
          $updatePass = $user->updatePass($userId,$_POST);
    } 
   ?>
    <!-- Return message will be show here -->
   <?php if (isset($updatePass)) {echo $updatePass;} ?>




<div id="tab3" class="tab-pane fade">
   <form class="form-horizontal" action="" method="post">
    <div class="form-group">
        <label class="col-sm-3 control-label">Old Password</label>
        <div class="col-sm-5">
            <input style="margin-bottom: 0px;" type="password" 
             name="oldpass" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">New Password</label>
        <div class="col-sm-5">
            <input style="margin-bottom: 0px;" type="password" 
        name="newpass" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">Confirm Password</label>
        <div class="col-sm-5">
            <input style="margin-bottom: 0px;" type="password" 
        name="conpass" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-5">
            <button type="submit" name="submitpass" class="btn btn-sm btn- 
             primary">
                Update Password
            </button>
        </div>
    </div>
</form>
</div>

Session.php

<?php
class Session{
    public static function init(){
       session_start();
    }
    public static function destroy(){
      session_destroy();
      session_unset();
    }
 }
 ?>

1 个答案:

答案 0 :(得分:2)

返回时,基本上是在说您已经完成了当前函数“返回此值”。此后什么都不会执行。只需将语句移到返回之前即可:

if ($updatepass) {
 $successmsg ="<div class='alert alert-success alert-dismissible'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button><strong>Success!</strong>  Password Changed Successfully.</div>";

Session::passdestroy();
return $successmsg;