会话不重定向

时间:2019-08-11 05:11:34

标签: php session

我有一个登录页面,当检查数据库是否存在用户时,该页面不会重定向到面板 问题出在会话中,因为没有会话,它会正常工作

if($sql->RowCount()>0){

    $entrou = $_SESSION['entrou'];
    header('location: painel.php');

在这里,我们进行检查,如果未创建会话,这是自动的,因为未进行登录,如果没有会话,则将重定向到登录名

    if (!isset($_SESSION['entrou']) == true ) {
  unset($_SESSION['entrou']);
  header('location: index.php');
}

3 个答案:

答案 0 :(得分:0)

试一下。

// ensure to add session_start at the beginning of 
// all scripts that require use of $_SESSION[]
session_start();

if($sql->rowCount()){
    $entrou = $_SESSION['entrou'];
    header('location: panel.php');
}

if(isset($_SESSION['entrou'])){
    unset($_SESSION['entrou']);
    header('location: index.php');
}

答案 1 :(得分:0)

如上所述,您必须在要使用会话的每个文件的开头开始会话。

如果您要设置会话,请使用以下命令:

session_start();
$_SESSION[NAME] = VALUE;

,并在检查会话是否设置后重定向,您可以这样做:

session_start();
if (isset($_SESSION[NAME]){
   header(‘Location: index.php‘;
}

答案 2 :(得分:-1)

在这里尝试


<?php

session_start();

...

if($sql->RowCount()>0){

   $_SESSION['entrou'] = true;
   header('location: panel.php');
}
else {
    header('location: index.php'); // "no user in the db!"
}

...

if (!isset($_SESSION['entrou']) {
    header('location: index.php');
}

?>

但是,当然,如果要检查用户是否存在于数据库中,可以执行以下操作

<?php

$data = $sql->query('SELECT * WHERE username=$_SESSION["username"]') // where "username" is the username column

if ($data == "") {
    header('location: index.php'); // user is non-existent
}
else {
    header('location: panel.php'); // the user is in the database
}
// note this only applies if you're using SQLite
相关问题