简体   繁体   中英

PHP - $_SESSION doesn't set

I have a register form for users. When they click in create account it goes to the insertUser.php page that will insert the user in the database and will create a $_SESSION["message"]="Registration Complete. Proceed to login" in case the insert is done successfully. the inserUser.php page then redirects to the index.php page with the register form open so the user can see the message. But it shows an error of Undefined Index: message . I am doing the same thing in the login form to show errors and everything works fine so I don't know what is the problem.

here is my code, index.php:

<div class="register-form">
    <form action="insertUsers.php"  method="POST" id="formregisto" name="formregisto">
            <div>
            <input type="text"  name="username" placeholder="Nome de Utilizador" required/>
            </div>

            <div>
            <input type="email" name="email" placeholder="E-mail" required/>
            </div>
            <div>
            <input type="password" name="password" id="password" placeholder="Password" required/>
            </div>
            <div id="message"><?php if (!isset($_SESSION["message"])) print $_SESSION["message"]?> </div>
            <div>
            <input type="submit" name="criar" value="Create Account"/>
            </div>
    </form>
</div>

insertUsers.php:

<?php
    include "include/config.php"; 

 $username= $_REQUEST['username'];
 $email= $_REQUEST['email'];
 $password= md5($_REQUEST['password']);

 if( !empty ($username) AND !empty ($email) AND !empty ($password)){
    $sql="insert into users(email, username, password) values ('$email', '$username','$password')"; 


    $result=mysqli_query($link, $sql);


if ($result)
    $_SESSION["message"]= "Registration Complete. Proceed to login";
     header('Location: index.php?openr=1');

 } else {
     $_SESSION["message"]= "Something went wrong, try again";
     header('Location: index.php?openr=1');
 }

?>

<?php if (!isset($_SESSION["message"])) print $_SESSION["message"]?>

That's what you wrote, it's not gonna display because of the ! you added. Kindly remove it and your code should work fine but you might still have a warning saying that message is undefined.

But your SQL syntax is really bad, you didn't escape user inputs. Don't trust your visitors!

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