Jump to content

Session Validation Logic


nblackwood

Recommended Posts

Hey everyone. I have this code that I put together which is intended to check if the user has selected an answer within the survey. I know how to check if a session or post variable is empty or not. The issue is that the I am unable to use "else" before the "die()" function, and also, the error is echoed on the page . My question is, how do I validate the session, and only display the error message in a certain place on the page without killing the entire script after that point?

The script I am using for my validation logic is:

 

<?php session_start();

 

$gender = $_POST['gender'];

$_SESSION['gender']=$gender;

 

if (empty($_SESSION['gender'])) die; echo 'Selection cannot be empty';

 

else require_once('page2.php');

 

?>

Link to comment
https://forums.phpfreaks.com/topic/201992-session-validation-logic/
Share on other sites

That script isn't giving you a parse error? It should.

Why not validate the form field before trying to set the session var? If you don't want to kill the script, don't tell it to die().

 

if (empty($_POST['gender']))  {
     echo 'Selection cannot be empty';
} else {
     $_SESSION['gender'] = $_POST['gender'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.