为什么我的php会话不会持续存在?

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.

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...

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.