PHP登录系统和会话

时间:2018-06-07 09:46:04

标签: php mysql mysqli

所以我的代码总是无法登录,它已通过检查方法,但它总是让我在if else部分中登录失败。我的代码有什么问题吗? 我不知道为什么我的代码适用于注册但登录页面失败

 <?php
require_once "core/init.php";

  $errors = array();

if( Input::get('submit')){

  //1. call object
$validation = new Validation();

  //2. check method
  $validation = $validation->check(array(
    'username' => array( 'required' => true),
    'password' => array( 'required' => true)
  ));

  //3. passed Validation
  if($validation->passed() ){

  if($admin->login_admin( Input::get('username'),Input::get('password') ) )
  {
    Session::set('username', Input::get('username'));
    header('Location: adminindex.php');
   }else{
     $errors[] = "login failed";

   }

  }else {
    $errors = $validation->errors();
  }
}

require_once "templates/header.php";
 ?>

<form class="" action="login2.php" method="post">

  <label>ID Admin</label>
  <input type="text" name="username">

  <label>Password</label>
  <input type="password" name="password">

    <?php if(!empty($errors)){ ?>
      <div id="errors">
        <?php foreach ($errors as $error) { ?>
          <li><?php echo $error; ?></li>
    <?php    } ?>
      </div>
    <?php } ?>

  <input type="submit" name="submit" value="Log In">

</form>

<?php
require_once "templates/footer.php";
?>

这是login_admin函数的代码

public function login_admin($username, $password){

    $data = $this->_db->get_info('admin', 'username', $username);
    // print_r($data);
    // die();

    if( password_verify($password, $data['password']) )
      return true;
      else return false;
}

这是获取信息功能的代码

 public function get_info($table, $column, $value){

    if(!is_int($value))
      $value = "'" .$value. "'";

    $query = "SELECT * FROM $table WHERE $column = $value";
    $result = $this->conn->query($query);

    while($row = $result->fetch_assoc()){
      return $row;
    }
  }

我也有类似的注册页面页面,但它设法设置会话并将我重定向到我想要的页面

<?php
require_once "core/init.php";

  $errors = array();

if( Input::get('submit')){

  //1. memanggil objek validasi
$validation = new Validation();
  //2. metode check
  $validation = $validation->check(array(
    'username' => array(
                          'required' => true,
                          'minimal' => 5,
                          'maximal' => 20,
                        ),
    'password' => array(
                      'required' => true,
                      'minimal' => 5,
                    ),
    'division' => array(
                      'required' => true,
                    )
  ));
  //3. passed Validation
  if($validation->passed() ){
  $admin->register_admin(array(
    'username' => Input::get('username'),
    'password' => password_hash(Input::get('password'), PASSWORD_DEFAULT),
    'division' => Input::get('div')
  ));

    Session::set('username', Input::get('username'));
    header('Location: adminindex.php');

  }else {
    $errors = $validation->errors();
  }
}

require_once "templates/header.php";
 ?>

<form class="" action="registeradmin.php" method="post">

  <label>ID Admin</label>
  <input type="text" name="username">

  <label>Password</label>
  <input type="password" name="password">

  <label>division</label>
  <input type="text" name="div">

    <?php if(!empty($errors)){ ?>
      <div id="errors">
        <?php foreach ($errors as $error) { ?>
          <li><?php echo $error; ?></li>
    <?php    } ?>
      </div>
    <?php } ?>

  <input type="submit" name="submit" value="add Admin">

</form>

<?php
require_once "templates/footer.php";
 ?>

0 个答案:

没有答案