会话仅在php中的一页上起作用

时间:2019-05-30 07:05:35

标签: php session

我为用户登录设置了会话变量,但除login.php之外,它在其他页面上均不起作用。

login.php:

<?php
    session_start();

    if(isset($_POST['submit'])) {       

        $conn = mysqli_connect('localhost', 'cp72722_lucky', '3AIp!9f4', 'cp72722_lucky');
        $user_username = mysqli_real_escape_string($conn, trim($_POST['username']));
        $user_password = mysqli_real_escape_string($conn, trim($_POST['password']));
        if(!empty($user_username) && !empty($user_password)) {

            $query = "SELECT user_id , username FROM users WHERE username = '$user_username' AND pwd = SHA('$user_password')";
            $data = mysqli_query($conn,$query);
            if(mysqli_num_rows($data) == 1) {

                $row = mysqli_fetch_assoc($data);                               
                $user_value = $row['username'];             
                $user_id_value = $row['user_id'];               
                $_SESSION['username'] = $user_value;
                $_SESSION['user_id'] = $user_id_value;              
                echo "Hello," . $_SESSION['username'];          
                $home_url = 'http://' . $_SERVER['SERVER_NAME'];                
                header('Location: ' . $home_url);               
                print_r($home_url);
                exit();
            }
            else {
                echo 'Извините, вы должны ввести правильные имя пользователя и пароль';
            }
        }
        else {
            echo 'Извините, вы должны заполнить поля правильно';
        }
    }

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="style/style.css" rel="stylesheet">
</head>
<body>
<header>
<ul>
    <li><a href="/">Главная</a></li>

</ul>
</header>
<h2>Вход</h2>
<div class="container">
<form action="http://<?php echo $_SERVER['SERVER_NAME']; ?>" method="POST">
        <label for="username">Логин:</label>
    <input type="text" name="username">
    <label for="password">Пароль:</label>
    <input type="password" name="password">
    <button type="submit" name="submit">Вход</button>
    <a href="auth/signup.php">Регистрация</a>
    </form>
    </div>
<footer>
    <p>Все права защищены</p>
</footer>

</body>

</html>

我的header.php几乎显示在所有页面上,登录后我想欢迎用户。如果未设置会话,则应显示表单进行登录。问题是,如果用户已登录并且配置文件链接,则由于未设置会话,它始终显示第二选择。我该如何解决这个问题?

header.php:

<?php

session_start();

print_r($_SESSION);
?>
...
<header id="header">
    <?php
echo 'Glad to see you,'. htmlspecialchars($_SESSION['username']);
?>
...
<div>
<?php
    if(!isset($_SESSION['username'])) {
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <label for="username">Login:</label>
    <input type="text" name="username">
    <label for="password">Password:</label>
    <input type="password" name="password">
    <button type="submit" name="submit">Enter</button>
    <a href="auth/signup.php">Registration</a>
    </form>
<?php
}
else {
    ?>
    <p><a href="myprofile.php">Profile</a></p>
    <p><a href="exit.php">Exit(<?php echo $_SESSION['username']; ?>)</a></p>
<?php   
}
?>
</div>

2 个答案:

答案 0 :(得分:1)

if($_SESSION['username'])表示$_SESSION['username']为真。相反,您应该使用isset函数来检查是否设置了会话。您的代码应如下所示

if(!isset($_SESSION['username'])) {//If $_SESSION['username'] is not set then show the login form
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <label for="username">Login:</label>
    <input type="text" name="username">
    <label for="password">Password:</label>
    <input type="password" name="password">
    <button type="submit" name="submit">Enter</button>
    <a href="auth/signup.php">Registration</a>
    </form>
<?php
}

答案 1 :(得分:-1)

session_start()函数之前删除多余的空间

转换:

    <?php

    session_start();

对此

<?php
session_start();

谢谢。