【问题标题】:infinite redirect loop + php not echoing a variable无限重定向循环+ php不回显变量
【发布时间】:2018-01-08 11:59:08
【问题描述】:

我创建了一个登录系统,登录后用户将被重定向到profile.php。在profile.php 页面中,标题将显示Welcome [username]

Session.php:

$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, "id2290767_wp");

session_start();
$user_check = isset($_SESSION['login_user']);

$ses_sql = mysqli_query($connection, "select name from login where name='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session = $row['name'];
if(!empty($login_session)) {
    mysqli_close($connection);
    header('Location: members.php');
}

profile.php(带有问候的那个)

<?php
include('session.php');

if(!isset($_SESSION['login_user'])){
     header("location: profile.php");
}
?>

<h1>Welcome <i><?php echo $login_session; ?></h1>

members.php中与登录表单相关联的signin.php:

session_start(); // Starting Session
$error = ''; // Variable To Store Error Message
if(isset($_POST['submit'])) {
    if(empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Username or Password is invalid";
    } else {
        // Define $username and $password
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establishing Connection with Server by passing server_name, user_id and password as a parameter
        $connection = mysqli_connect("localhost", "root", "");
        // To protect MySQL injection for Security purpose
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($connection, $username);
        $password = mysqli_real_escape_string($connection, $password);
        // Selecting Database
        $db = mysqli_select_db($connection, "id2290767_wp");
        // SQL query to fetch information of registerd users and finds user match.
        $query = mysqli_query($connection, "select * from login where password='$password' AND name='$username'");
        $rows = mysqli_num_rows($query);
        if($rows == 1) {
            $_SESSION['login_user'] = $username; // Initializing Session
            header("location: profile.php"); // Redirecting To Other Page
        } else {
            $error = "Username or Password is invalid";
        }
        mysqli_close($connection); // Closing Connection
    }
}

members.php:

<?php
include('signin.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
     header("location: profile.php");
}

但是我在 PHP 页面中没有得到任何文本。它只是说“欢迎”。

我做错了什么?

编辑:更改后获得无限循环

$user_check = isset($_SESSION['login_user']);

$user_check=isset($_SESSION['login_user']) ? $_SESSION['login_user'] : "";

【问题讨论】:

标签: php mysql sql session login


【解决方案1】:

isset ()函数用于检查变量是否被设置。

改变

$user_check=isset($_SESSION['login_user']);

$user_check=isset($_SESSION['login_user']) ? $_SESSION['login_user'] : "";

【讨论】:

  • 我要提一下,问题在于 OP 将 $user_check 设置为 true/false,因为您的回答(对于新手开发人员)不太清楚。
  • 我认为它应该可以工作,但我现在陷入了无限重定向循环,
  • 它会起作用的。无限重定向循环是因为“检查会话条件”太多。根据您给定的代码,这就是问题所在。但是,您需要提供更多详细信息。因此,我们可以了解功能并可以提出一些建议@TheZZGamerz
猜你喜欢
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
  • 2013-01-25
相关资源
最近更新 更多