Php 重定向后未存储会话?

Php 重定向后未存储会话?,php,session,login,undefined,Php,Session,Login,Undefined,正在尝试使用存储用户的角色 $\u会话['role']==$row['role'] 当用户名“admin”登录时,它会检查SQL中的用户角色admin,如果存在admin,则会将用户重定向到admin.php 但是,当重定向到admin.php时,页面会显示 $\u会话['role'] 在该页上未定义 我想我是用 $\u会话['role']==$row['role'] 在login_action.php上,并在login_action.php和admin.php中使用start_session(

正在尝试使用存储用户的角色
$\u会话['role']==$row['role']
当用户名“admin”登录时,它会检查SQL中的用户角色admin,如果存在admin,则会将用户重定向到admin.php
但是,当重定向到admin.php时,页面会显示
$\u会话['role']
在该页上未定义

我想我是用
$\u会话['role']==$row['role']
在login_action.php上,并在login_action.php和admin.php中使用start_session()

怎么了

login\u action.php

<?php
      session_start();
include("connect.php"); 
$tbl_name="users"; 

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

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($conn,$username);
$password = mysqli_real_escape_string($conn,$password);
$password = sha1($password);

$result = mysqli_query($conn, "SELECT * FROM $tbl_name WHERE user='$username' AND password='$password'");

if(mysqli_num_rows($result) != 1){
      echo "<script>alert(' Wrong Username or Password Access Denied !!! Try Again');
      window.location='index.php';
      </script>";
     }else{
      $row = mysqli_fetch_assoc($result);
      $_SESSION['role'] == $row['role'];

      if($row['role'] == 'Admin'){
       header('location: admin.php');
       exit;

      else{
       echo "<script>alert('Wrong username or password. Try again');
      window.location='index.php';
      </script>";
      }
     }
 <?php
    session_start();
    if (isset($_SESSION['role']) != 'Admin') {
        echo "You are not the admin";
    }
    ?>

    <html>
      <head>
        <title> Administrator Page </title>
     <head>

    <body><br>
     <h1 align="center">
      Welcome To Administrator Page <br>



      <a href='logout.php'>Click here to log out</a>
      </h1>
     </body>
     </html>
else{
      $row = mysqli_fetch_assoc($result);
      $_SESSION['role'] = $row['role']; // <--- Notice only one equal

      if($row['role'] == 'Admin'){ // = inside if statement is always true
       header('location: admin.php');
       exit;
错误在这里:

login\u action.php

<?php
      session_start();
include("connect.php"); 
$tbl_name="users"; 

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

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($conn,$username);
$password = mysqli_real_escape_string($conn,$password);
$password = sha1($password);

$result = mysqli_query($conn, "SELECT * FROM $tbl_name WHERE user='$username' AND password='$password'");

if(mysqli_num_rows($result) != 1){
      echo "<script>alert(' Wrong Username or Password Access Denied !!! Try Again');
      window.location='index.php';
      </script>";
     }else{
      $row = mysqli_fetch_assoc($result);
      $_SESSION['role'] == $row['role'];

      if($row['role'] == 'Admin'){
       header('location: admin.php');
       exit;

      else{
       echo "<script>alert('Wrong username or password. Try again');
      window.location='index.php';
      </script>";
      }
     }
 <?php
    session_start();
    if (isset($_SESSION['role']) != 'Admin') {
        echo "You are not the admin";
    }
    ?>

    <html>
      <head>
        <title> Administrator Page </title>
     <head>

    <body><br>
     <h1 align="center">
      Welcome To Administrator Page <br>



      <a href='logout.php'>Click here to log out</a>
      </h1>
     </body>
     </html>
else{
      $row = mysqli_fetch_assoc($result);
      $_SESSION['role'] = $row['role']; // <--- Notice only one equal

      if($row['role'] == 'Admin'){ // = inside if statement is always true
       header('location: admin.php');
       exit;
else{
$row=mysqli\u fetch\u assoc($result);
$\u SESSION['role']=$row['role'];//错误在这里:

login\u action.php

<?php
      session_start();
include("connect.php"); 
$tbl_name="users"; 

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

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($conn,$username);
$password = mysqli_real_escape_string($conn,$password);
$password = sha1($password);

$result = mysqli_query($conn, "SELECT * FROM $tbl_name WHERE user='$username' AND password='$password'");

if(mysqli_num_rows($result) != 1){
      echo "<script>alert(' Wrong Username or Password Access Denied !!! Try Again');
      window.location='index.php';
      </script>";
     }else{
      $row = mysqli_fetch_assoc($result);
      $_SESSION['role'] == $row['role'];

      if($row['role'] == 'Admin'){
       header('location: admin.php');
       exit;

      else{
       echo "<script>alert('Wrong username or password. Try again');
      window.location='index.php';
      </script>";
      }
     }
 <?php
    session_start();
    if (isset($_SESSION['role']) != 'Admin') {
        echo "You are not the admin";
    }
    ?>

    <html>
      <head>
        <title> Administrator Page </title>
     <head>

    <body><br>
     <h1 align="center">
      Welcome To Administrator Page <br>



      <a href='logout.php'>Click here to log out</a>
      </h1>
     </body>
     </html>
else{
      $row = mysqli_fetch_assoc($result);
      $_SESSION['role'] = $row['role']; // <--- Notice only one equal

      if($row['role'] == 'Admin'){ // = inside if statement is always true
       header('location: admin.php');
       exit;
else{
$row=mysqli\u fetch\u assoc($result);

$\u SESSION['role']=$row['role'];//在您的login\u action.php中:

将$_会话['role']=$row['role'];更改为$_会话['role']=$row['role']

在admin.php文件中:

isset($\u会话['role'])!='Admin'

isset($var):如果设置了$var且不为null,则返回true

解决办法是:

<?php
session_start();
if (isset($_SESSION['role']) && strtolower($_SESSION['role']) != 'admin') {
    echo "You are not the admin";
}
?>

<html>
  <head>
    <title> Administrator Page </title>
 <head>

<body><br>
 <h1 align="center">
  Welcome To Administrator Page <br>



  <a href='logout.php'>Click here to log out</a>
  </h1>
 </body>
 </html>

管理员页面

欢迎来到管理员页面

在您的登录\u action.php中:

将$_会话['role']=$row['role'];更改为$_会话['role']=$row['role']

在admin.php文件中:

isset($\u会话['role'])!='Admin'

isset($var):如果设置了$var且不为null,则返回true

解决办法是:

<?php
session_start();
if (isset($_SESSION['role']) && strtolower($_SESSION['role']) != 'admin') {
    echo "You are not the admin";
}
?>

<html>
  <head>
    <title> Administrator Page </title>
 <head>

<body><br>
 <h1 align="center">
  Welcome To Administrator Page <br>



  <a href='logout.php'>Click here to log out</a>
  </h1>
 </body>
 </html>

管理员页面

欢迎来到管理员页面

谢谢Savandy!很简单,但仍能帮我很多忙。从我自己的错误中学习。谢谢Savandy!很简单,但仍能帮我很多忙。从我自己的错误中学习。谢谢!不仅解决了我的问题,还用strtolower增强了我的问题。非常感谢!当然,朋友,不要犹豫问问题,这就是为什么:)注意到了吗当角色中的值不是“admin”并且包含特殊字符(如å、ä或ö)时,我无法登录。相反,我被重定向回index.php。你知道为什么吗?数据库和表都使用UTF8_binutf8_bin编码,如果字符不同,则通过字符串中每个字符的二进制值比较字符串(无论是大小写还是变音符号差异)然后,您的查询可能会产生不同的结果。请改用utf8\U unicode\U ci。将用户表和数据库更改为utf8\U unicode\U ci但不起作用。可能与缺少的元标记有关?谢谢!不仅解决了我的问题,而且还使用strtolower对其进行了增强。非常感谢!当然,伙计,请不要犹豫提问,这就是为什么:)注意,当角色中的值不是“admin”并且包含特殊字符(如å、ä或ö)时我无法登录。相反,我被重定向回index.php。你知道为什么吗?数据库和表都是用UTF8\u binutf8\u bin编码的,如果字符不同(无论是大小写还是字符差异),则通过字符串中每个字符的二进制值来比较字符串然后,您的查询可能会产生不同的结果。请改用utf8\U unicode\U ci。将用户表和数据库更改为utf8\U unicode\U ci,但不起作用。可能与缺少元标记有关?