Okay so I have a weird issue regarding php sessions. I am using sessions to transfer information across pages. My problem is that on some occasions (usually one in 10) I get an error saying PHP Notice: Undefined index: $myVar. The funky thing about it is that the session persist through the first part of the application and then seems to disappear.
The flow is as follows.
- User lands at the login page, clicks submit after entering the user code.
- The application goes to a control page that gathers the information and uses the data class to gather and store the information.
- The data class stores the information into an array and then puts that array into the session,
- the control page then uses that information to send an email, and forward the user to the welcome page.
- The email uses the session variable as well as the welcome page. The email always comes through with the information even when the welcome page does not.
This is the top of the welcome page, the session start is the very first line.
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="shortcut icon" href="./rmilogo.png" type="img/png" />
<title>Welcome!</title>
this is the control page
<?php
session_start();
function getDealer() {
require '../data/DealerDB.php';
$action = filter_input(INPUT_POST, 'response');
$dealerID = filter_input(INPUT_POST, 'dCode');
new DealerDB('****************', $dealerID);
$cmpDealer = $_SESSION['dealer'];
if (!$cmpDealer['dCode'] == '' || !is_null($cmpDealer['dCode'])) {
require '../business/AutoEmail.php';
try{
new AutoEmail($action, $cmpDealer['dCode']);
}catch(Exception $e) {
trigger_error('Message: ' . $e->getTraceAsString());
}
header("Location: http://www.*******.com/welcome.php");
} else {
header("Location: http://www.*******.com/error.html");
}
}
getDealer();
again with the session start at the top of the page. This is the top of the data retrieval class.
<?php
session_start();
$_SESSION['dealer'] = null;
require '../business/Dealer.php';
class DealerDB {
private $host;
private $user;
private $passkey;
private $db;
function __construct($db, $dealerCode) {
$this->host = '*******';
$this->user = '*******';
$this->passkey = '*******';
$this->db = $db;
$_SESSION['dealer'] = $this->buildDealer($dealerCode);
}
again with the session start at the top, The problem here is that when the control class calls the email it works, and most of the time so does the transfer of the session to the next page. However sometimes the session variable does not persist and the information is lost and can't be found anywhere else in the program either, I just get a $mySessionVar is not set error.