无限重定向循环+ php不回显变量

时间:2017-08-01 14:38:38

标签: php mysql sql session login

我创建了一个登录系统,登录后,用户将被重定向到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>

signin.php与members.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页面中没有任何文字。它只是说&#34;欢迎&#34;。

我做错了什么?

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

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

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

1 个答案:

答案 0 :(得分:2)

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

更改

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

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