$ _SESSION不起作用。 xampp php7

时间:2018-03-10 18:35:33

标签: php html session xampp

我试图在xammp上注册网站,但看起来$ _SESSION不起作用。我有session_start();在每个文件上。这是我的文件。我希望你能帮助我:)< 3

我也有一些index.php,其中只有2个重定向按钮来注册.php和login.php(它不完整)

这是我的register.php:

<?php
session_start();

$db = mysqli_connect("localhost", "root", "", "login");

if(isset($_POST['reg_btn'])) {
$username  = $_POST['username'];  //here I also tried with $_SESSION but it doesn't work
$email = $_POST['email'];         //
$password = $_POST['password'];   //
$password2 = $_POST['password2']; //

    if ($password == $password2){
        $password = md5($password);
        $sql = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
        mysqli_query($db, $sql);
        $_SESSION['message'] = "You are logged in!";
        $_SESSION['username'] = $username;
        header("location:home.php");
    }else{
        $_SESSION['message'] = "The passwords doesn't match!";
    }
}
?>

<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header"> 
    <h1>Register</h1>
</div>
<div id="frm"> 
<form action="register.php" method="POST">
<table>
    <tr>
        <td><label>Email:</label></td>
        <td><input type="email" name="email" class="textInput" /></td>
    </tr>
    <tr>
        <td><label>Username:</label></td>
        <td><input type="text" name="username" class="textInput" /></td>
    </tr>
    <tr>
        <td><label>Password:</label></td>
        <td><input type="password" name="password"     class="textInput"/></td>
    </tr>
    <tr>
        <td><label>Password Confirmation:</label></td>
        <td><input type="password" name="password2" class="textInput"/></td>
    </tr>
    <tr>
        <td><input type="submit" name="reg_btn" value="Register"/>
    </tr>
</table>
</form>

</div>
</body>
</html> 

这是我的home.php,我希望显示会话变量:

<?php 
session_start();

?>
<html>
<head>
<title>Home Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header"> 
    <h1>Welcome <?php echo ",".$_SESSION['username'];?></h1>
</div>
</html>

2 个答案:

答案 0 :(得分:0)

使用其他编辑器检查文件,或将代码复制到新文件。之前我遇到过这样的问题,因为我的文件在开启php标签之前包含了一个字符。

答案 1 :(得分:0)

当我用session_unset()清除会话时;有用! 谢谢你的帮助=)