change session variable value on another page | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

change session variable value on another page

I want the $_SESSION['userShort'] value to change when the if condition is met in the register.act.php page, so that the value is different even in the register.php page, once the submit button in clicked in this same page, but I'm doing something wrong... REGISTER.PHP <?php session_start(); include 'includes/header.php' ?> <div class="container w-50 mx-auto"> <div class="alert alert-danger" <?php echo $_SESSION["userShort"] = 'style="display: none;"' ?>>The name is too short</div> <form action="action/register.act.php" method="POST"> <div class="form-group"> <label for="username">Username</label> <input type="text" name="username_reg" class="form-control"> <label for="pwd">Password</label> <input type="password" name="pwd_reg" class="form-control"> <button type="submit" name="submit_reg" class="mt-3 btn btn-success">Register</button> </div> </form> </div> </body> </html> <?php var_dump($_SESSION["userShort"]); ?> REGISTER.ACT.PHP <?php session_start(); if(isset($_POST['submit_reg'])){ if(strlen($_POST['username_reg']) < 8){ header('Location: ../register.php'); $_SESSION['userShort'] = "style='display: block;'"; } }

12th May 2020, 3:25 PM
Francesco Paolini
Francesco Paolini - avatar
1 Answer
+ 1
You might want to use a condition in register.php if(isset($_SESSION["userShort"])) Display the <div class="alert alert-danger><xdiv> Don't diaplay the <div> if the <userShort> variable does not exist in session. I think you want to name it <errMsg> instead, you may use <errMsg> to store different messages, for each unsatisfactory input, rather than a certain one (user name length being too short). (Edit) I just noticed that in register.act.php, you changed the <userShort> session variable after redirection by calling header() function. I think that's why you don't see the <userShort> variable from register.php; because of the redirection.
14th May 2020, 9:11 AM
Ipang