登录问题。我正在使用php版本7.2

时间:2019-04-11 09:20:00

标签: php

我正在使用PHP 7.2版本的登录问题。成功登录后,我想直接进行一次会话。但是总是直接重定向登录页面。可以正常使用PHP 5.6版本,但仅发行php 7.2版本,请检查它并分享想法。

Login.php

<?php
session_start();
session_regenerate_id(true);
require 'setting/config.php';
error_reporting(0);
$username  =  $_POST['username'];
$password  =  $_POST['password'];
$login     =  $_POST['login'];
if(isset($login)){
  $res = $conn->query("SELECT * FROM login where username='$username' and password='$password'");
  $row = $res->fetch_assoc();
  $name = $row['name_login'];
  $user = $row['username'];
  $pass = $row['password'];
  $type = $row['type_login'];
  if($user==$username && $pass=$password){

    if($type=="admin"){ 
        $_SESSION['mysesi']=$name;
        $_SESSION['mytype']=$type;
        session_regenerate_id(false);
        echo "<script>window.location.assign('index.php')</script>";

    } else if($type=="user"){
        $_SESSION['mysesi']=$name;
        $_SESSION['mytype']=$type;
        echo "<script>window.location.assign('index.php')</script>";

    } else if($type=="super"){
        $_SESSION['mysesi']=$name;
        $_SESSION['mytype']=$type;
        echo "<script>window.location.assign('super.php')</script>";

    } else{
        $msg="your Login successfully !";
    }
  } else{
    $msg=" worng please try again!";
  }
}
?>

session.php

<?php 
session_start();
require 'setting/config.php';
if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='admin')
{
  echo "<script>window.location.assign('sign-in.php')</script>";
}
?>

config.php

<?php

$conn = new mysqli("localhost", "root", "", "proyello_db");
  if ($conn->connect_errno) {
    echo "Failed to connect to MySQL: " . $conn->connect_error;
  }
  ?>

1 个答案:

答案 0 :(得分:1)

我认为问题出在这部分:

<?php 
session_start();
require 'setting/config.php';
if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='admin')
{
  echo "<script>window.location.assign('sign-in.php')</script>";
}
?>

每个页面中都包含session.php,对吗? 它所做的是检查是否设置了$_SESSION['myesi'](登录后为true)和$_SESSION['mytype'] === 'admin'

问题是,mytype的{​​{1}}索引只有以admin身份登录才是admin。作为普通用户或“超级”用户,if的正文将被执行,将您转发到$_SESSION。这是您想要的行为吗?