1

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.

  1. User lands at the login page, clicks submit after entering the user code.
  2. The application goes to a control page that gathers the information and uses the data class to gather and store the information.
  3. The data class stores the information into an array and then puts that array into the session,
  4. the control page then uses that information to send an email, and forward the user to the welcome page.
  5. 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.

Kayce Basques
  • 23,849
  • 11
  • 86
  • 120
  • Nice checklist here: http://stackoverflow.com/a/17242347/2329487 – shudder Jul 31 '14 at 01:23
  • @shudder this was absolutely awesome, funny thing is I didn't realize the problem until I tried to switch servers and understood the issue, I will be answering my own problem for all to see so check it out below –  Jul 31 '14 at 17:17

2 Answers2

1

Ok so @shudder posted a list that was very helpful and I didn't realize it until I had switched hosts. The key to this one was the domain, and ensuring that I was staying in the current domain. When I was running on local host this wasn't an issue because it always loaded through localhost, and my www folder from WAMP. However when you deploy to the big bad world of hosting sites something changes. You can access the site by going to www.your_website.com or simply your_website.com. When you are trying to carry session variables and you are using header redirects going from www.your_website.com to www.your_website.com and likewise your_website.com to your_website.com the results are great, however if you try to carry a session from www.your_website.com to your_website.com doesn't work. They are different in the eyes of the session and therefore the session seemingly disappears. So how do we fix it you could do one of two things my first suggestion will be the easiest

1) Go to your hosting panel and redirect any possible domain name alternative of yours to the one you are using in the headers so if you have:

headers("Location: http://www.yourdomain.com/welcome.php");

so to speak you want to make sure that if someone types into the address bar yourdomain.com they get redirected to www.yourdomain.com/index.php so to speak so that the headers always line up and agree.

you could also do a address bar check like so

$server=$_SERVER['HTTP_HOST'];

use server in an if statement to test if the uri is equal to www.yourdomain.com or is equal to yourdomain.com and handle the headers that way.

If I have missed anything please let me know but this was definitely a great learning experience in the nature of session variables and the care that you have to take when deploying your final application. Thank all of you for all of your help and I look forward to this again.

0

Answer 2

Perhaps one of the files that you are including are also starting a new session. This would erase the current state of your session.

See PHP Session Variable Lost

Answer 1

Why are you closing the PHP tags after session_start() and then reopening them?

E.g rather than this:

<?php session_start(); ?>
<?php

function getDealer() {
require '../data/DealerDB.php';

Do this:

<?php 
session_start();

function getDealer() {
require '../data/DealerDB.php';

I haven't looked at your code. But, right off the bat, I can tell you that closing those tags will cause all sorts of wonky behavior.

Zend Framework's Guidelines on File Formatting

Why We Don't Close Tags...

Community
  • 1
  • 1
Kayce Basques
  • 23,849
  • 11
  • 86
  • 120
  • I just started that in an attempt to correct the problem thinking that the session start just wasn't close enough to the top of the page lol, I have tried just about every idea that I could think of, and nothing seemed to work so I tried that. it didn't help either, they were just under the first php opening tag before that though, I just really didn't have any other answers at the moment and did a spur of the moment change. The problem is really hard to narrow down because it doesn't happen all the time. –  Jul 30 '14 at 21:32
  • @raidenace do you happen to know any reason as to why they may not be persisting in one out of maybe ten cases but seem to work the rest of the time. –  Jul 30 '14 at 21:33
  • @Kayce Basques, Okay I thought that the first answer you gave was close to something that I had been experiencing, I took the session_start out of my email class to see if maybe that was the issue, still no luck I also rebuilt the control class to have the creation of the dealer outside the original function for the control page thinking that maybe I was dealing with some sort of loss to function issue, still no luck. Any ideas? –  Jul 30 '14 at 22:03
  • Nothing pops out at me. At this point, if I were in your shoes, I would start programming defensively by checking if a variable has been set before attempting to reference it. I like the tactics described in "Ways to deal with the issue" section in this answer from a related thread: http://stackoverflow.com/a/4261200/1669860 – Kayce Basques Jul 30 '14 at 22:33
  • @kayce basque I tried that I also tried declaring a second bauble just before the return to the welcome page and same results works on 90 percent of the attempts fails on 10 percent –  Jul 31 '14 at 02:08
  • @KayceBasques thank you for all of your help the answer to the question will be posted below, I figured it out today thanks to the help of the list that shudder posted, it actually kind of makes me laugh now. –  Jul 31 '14 at 17:18