douketangyouzh5219 2014-07-30 21:16
浏览 45

为什么我的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.

  • 写回答

2条回答 默认 最新

  • duanpu4143 2014-07-30 21:28
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向