使用$ _SESSION PHP多级导航栏

问题描述 投票:1回答:1

我会直接去点。 我建立一个登录系统,我有我的登录都包起来。当我试图登录并改变我的登录状态,我换款导航栏我根据其工作原理,但是当我退出它抛出我一些误差约$ _SESSION是NULL用户类型的问题就来了。 所以我基本上是在寻找一种方式来纠正我的错误。

我这里还有代码和一些截图。

这里的头PHP文件。

<?php
session_start();
global $tipo_usuario;
if($_SESSION['tipo_usuario']){
 $tipo_usuario = $_SESSION['tipo_usuario'];
}

?>
<!DOCTYPE html>
<html lang="es">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
 <link rel="stylesheet" href="css/login.css">
</head>
<?php
if ($tipo_usuario == "Admin") {
 require("navbarIniciadoAdmin.html");
 //echo($tipo_usuario);
} else if ($tipo_usuario == "User") {
 require("navbarIniciadoUser.html");
 //echo($tipo_usuario);
} else {
 require("navbarNoIniciado.html");
 //echo($tipo_usuario);
}
?>
</html>

而这里的登录-inc.php文件。

<?php
if (isset($_POST['login-submit'])) {
 login();
} else {
 header("Location: ../index.php");
 exit();
}

function login() {
 require 'dbc-inc.php';

 $nombreUsuario = $_POST['username'];
 $password = $_POST['password'];

 if (empty($nombreUsuario) || empty($password)) {
  header("Location: ../index.php?error=emptyfields&username=" . $nombreUsuario);
  exit();
 } else {
  $sql = "SELECT * FROM usuarios  WHERE NOMBRE_USUARIO = ?;";
  $stmt = mysqli_stmt_init($connection);
  if (!mysqli_stmt_prepare($stmt, $sql)) {
   header("Location: ../index.php?error=sqlerror");
   exit();

  } else {

   mysqli_stmt_bind_param($stmt, "s", $nombreUsuario);
   mysqli_stmt_execute($stmt);
   $result = mysqli_stmt_get_result($stmt);

   if ($row = mysqli_fetch_assoc($result)) {
    $checkPassword = password_verify($password, $row['PWD_USUARIO']);
    if ($checkPassword == false) {
     header("Location: ../index.php?error=ContraseñaIncorrecta");
     exit();
    } else if ($checkPassword == true) {
     $contador = 1;
     session_start();
     $_SESSION['id_usuario'] = $row['ID_USUARIO'];
     $_SESSION['nombre_usuario'] = $row['NOMBRE_USUARIO'];
     $_SESSION['tipo_usuario'] = $row['TIPO_USUARIO'];


     header("Location: ../documentos.php?success=login");
     exit();
    } else {
     header("Location: ../index.php?error=ContraseñaIncorrecta");
     exit();
    }
   } else {
    header("Location: ../index.php?error=noUser");
    exit();
   }
  }
 }
}

In index page, first error. $_SESSION not indexed

Already logged in, no errors

After log out, the same message

php session login
1个回答
1
投票

代替 :

if($_SESSION['tipo_usuario']){
 $tipo_usuario = $_SESSION['tipo_usuario'];
}

检查isset,而不是空的:

if (isset($_SESSION['tipo_usuario']) && !empty($_SESSION['tipo_usuario'])) {
    $tipo_usuario = $_SESSION['tipo_usuario'];
}
© www.soinside.com 2019 - 2024. All rights reserved.