PHP会话错误会话已经启动并且未定义变量:_session

时间:2017-04-19 17:31:48

标签: php session

我想用验证码创建。如果验证码不正确,则在表单中显示相同的值。

这是将数据添加到会话的页面。它可以显示会话中的数据,例如echo $_session['txtShippingFirstName'];

// Use captcha
if(!((md5((isset($_POST['captcha'])?$_POST['captcha']:"")) == $_SESSION['captchaKey']) || (isset($_SESSION['plaincart_customer_id'])))){            

$_session['txtShippingFirstName'] = $_POST['txtShippingFirstName'];
$_session['txtShippingLastName'] = $_POST['txtShippingLastName'];
$_session['txtShippingEmail'] = $_POST['txtShippingEmail'];
$_session['txtShippingAddress1'] = $_POST['txtShippingAddress1'];
$_session['txtShippingAddress2'] = $_POST['txtShippingAddress2'];
$_session['txtShippingPhone'] = $_POST['txtShippingPhone'];
$_session['txtShippingCity'] = $_POST['txtShippingCity'];
$_session['txtShippingState'] = $_POST['txtShippingState'];
$_session['txtShippingPostalCode'] = $_POST['txtShippingPostalCode'];

//setError($_session['txtShippingFirstName']);
setError("CAPTCHA not correct");
header("Location: checkout.php?step=1"); 

}else{
    $includeFile = 'checkoutConfirmation.php';
} 

如果验证码不正确,这是表格页面,然后返回此页

if(isset($_session['txtShippingFirstName'])){
            $customerFirstName = $_session['txtShippingFirstName'];
            $customerLastName = $_session['txtShippingLastName'];
            $customerEmail = $_session['txtShippingEmail'];
            $customerAddress = $_session['txtShippingAddress1'];
            //$_session['txtShippingAddress2']
            $customerPhone = $_session['txtShippingPhone'];
            $customerCity = $_session['txtShippingCity'];
            $customerState = $_session['txtShippingState'];
            $customerPostalCode = $_session['txtShippingPostalCode'];

}

echo $_session['txtShippingFirstName'];

它在行echo $_session['txtShippingFirstName'];上显示错误未定义变量:_session。如果我在页面顶部添加session_start();,则会显示错误Notice: A session had already been started - ignoring session_start(),因为我已在头文件中添加了session_start。如何解决此错误?

1 个答案:

答案 0 :(得分:0)

PHP超级全局需要大写:

$_SESSION['txtShippingFirstName'] 

在这种情况下。