$_SESSION variable losing value

How can I fix this issue when php $_SESSION variable fails to keep value. Sometimes it does keep the value and sometimes the value is not keep

That is hard to answer without seeing your code. Could you please provide us with something to give us an idea of what you have so far?

@WebMachine

Am keeping the value enter in a html form to php variable and then assign the variable to the $_SESSION variable to move it to the next page and then insert it into database but sometimes it fails and make the mysql database generate error Uncaught exception
‘PDOException’ with message ‘SQLSTATE[23000]: Integrity constraint
violation: 1048 Column ‘email’ cannot be null’

session_start()


if (empty($_POST["bname"])) 
{
    $errName = 'Business Name Missing';
    $errorStatus = true;
}
else {
    $businessName = $_POST["bname"];
}

if (empty($_POST["bEmail"])) 
{
    $errEmailAdd = 'Email Address Missing';
    $errorStatus = true;
}

elseif(preg_match("/^[\w\.\-]+@([\w\-]+\.)+[a-z]+$/i", $_POST["bEmail"]) === 0) 
{
    $errEmailAdd = 'Email address must be in this format name@example.com only.';
    $errorStatus = true;
}
else {
    $bEmailAdd = $_POST["bEmail"];
}

if (isset($_POST['bEmail'])) { 
$bEmailAdd = filter_var($_POST["bEmail"], FILTER_SANITIZE_EMAIL);
$bEmailAdd = filter_var($_POST["bEmail"], FILTER_VALIDATE_EMAIL);
}

if(!$errorStatus) { 

$_SESSION['businessName'] = $businessName;
$_SESSION['bizEmailAddress'] = $bEmailAdd;

header('Location: createBusinessAccount/');
}

Sometimes the $_SESSION[‘bizEmailAddress’] get to the next page and sometimes not and the error comes up.

filter_var
http://php.net/manual/en/function.filter-var.php

Returns the filtered data, or FALSE if the filter fails.

I think if you test the truthiness of the return and if false set error status it may help.

You only set this session variable if $errorStatus is false.

$errorStatus is true if bEmail and bname are empty or bEmail is in the wrong format. Is that working the way you want it to?

$errorStatus = false; is set to false already…

I have it like this…

session_start()
$errorStatus = false;

if (empty($_POST["bname"])) 
{
    $errName = 'Business Name Missing';
    $errorStatus = true;
}
else {
    $businessName = $_POST["bname"];
}

if (empty($_POST["bEmail"])) 
{
    $errEmailAdd = 'Email Address Missing';
    $errorStatus = true;
}

elseif(preg_match("/^[\w\.\-]+@([\w\-]+\.)+[a-z]+$/i", $_POST["bEmail"]) === 0) 
{
    $errEmailAdd = 'Email address must be in this format name@example.com only.';
    $errorStatus = true;
}
else {
    $bEmailAdd = $_POST["bEmail"];
}

if (isset($_POST['bEmail'])) { 
$bEmailAdd = filter_var($_POST["bEmail"], FILTER_SANITIZE_EMAIL);
$bEmailAdd = filter_var($_POST["bEmail"], FILTER_VALIDATE_EMAIL);
}

if(!$errorStatus) { 

$_SESSION['businessName'] = $businessName;
$_SESSION['bizEmailAddress'] = $bEmailAdd;

header('Location: createBusinessAccount/');
}

You’re getting this error because you are inserting empty fields. You should use if(!filter_var($_POST['bEmail'], FILTER_VALIDATE_EMAIL)) { instead of your regex condition you set up. It might be the cause of it.

You should also set the email column to NULL so if you ever happen to insert blank data, you won’t receive that error message.

Also, you should exit or die whenever you are giving the client an error message. Therefore, I am assuming that your custom error message goes through at the same time you are still inserting the data when the data shouldn’t be inserted.

Crafting a good email address regex is notoriously not easy eg.
/^[\w\.\-]+@([\w\-]+\.)+[a-z]+$/i
would allow something like
_@-.z

compare to

/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD

@spaceshiptrooper

if am using
if(!filter_var($_POST[‘bEmail’], FILTER_VALIDATE_EMAIL))

I do not need to use this again?
preg_match(“/[1]+@([\w-]+.)+[a-z]+$/i”, $_POST[“bEmail”]) === 0)


  1. \w.- ↩︎

Correct. FILTER_VALIDATE_EMAIL is a default and should be good for use. Creating your own regex condition to handle this is kind of like what @Mittineague said above. It needs to be crafted in a way where it allows legitimate emails to pass through at the same time not allow emails like _@-.z to pass through. As said by @Mittineague in an earlier thread

Regex is like a language all its own and it’s not always so easy to get it right where it both matches exactly what you want and does not match what you want not.


  1. \w.- ↩︎

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.