简体   繁体   中英

$_SESSION doesn't work. xampp php7

I have tried to make site with registrations on xammp but it looks like that $_SESSION doesn't work. I have session_start(); on every document. Here are my files. I hope you can help me :) <3

I also have some index.php in which there are only 2 redirecting buttons to register.php and login.php(it is not complete)

This is my 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> 

Here is my home.php where i want my session variables to be displayed:

<?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>

check the files with another editor, or copy past the code to new files. I had such issue before and it happened because my file included a character before the opening php tag.

When I clear the session with session_unset(); it works! Thanks for the help =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM