Php 会话变量设置不正确
我正在尝试将会话变量从一个页面设置到另一个页面。它在我的本地主机上工作,但后来我把它上传到我的服务器上,现在它失败了。我已经做了一些基本的bug测试,它将在特定点打印,我在代码中标记了它们。这是我正在使用的代码 //第1页Php 会话变量设置不正确,php,session,session-variables,Php,Session,Session Variables,我正在尝试将会话变量从一个页面设置到另一个页面。它在我的本地主机上工作,但后来我把它上传到我的服务器上,现在它失败了。我已经做了一些基本的bug测试,它将在特定点打印,我在代码中标记了它们。这是我正在使用的代码 //第1页 session_start(); if(isset($_REQUEST['id'])){ $_SESSION['id'] = $_REQUEST['id']; //prints here just fine then fails after head
session_start();
if(isset($_REQUEST['id'])){
$_SESSION['id'] = $_REQUEST['id'];
//prints here just fine then fails after
header('location: app.php');
}elseif($_POST){
if(isset($_SESSION['error'])){
unset($_SESSION['error']);
}
$questions = array();
for($i = 1; $i <= 11; $i++){
$questions['q'.$i] = $_POST['q'.$i];
}
$check = "";
foreach($questions as $key => $val){
$check .= $val;
}
$metreq = strcmp($check, "1111-1-1-1-11-1-1");
if(!$metreq){
$_SESSION['questionaire'] = $questions;
header("location: app.php");
}else{
header('location: nq.php');
}
}
session_start();
如果(isset($_请求['id'])){
$\会话['id']=$\请求['id'];
//这里的打印很好,但之后就失败了
标题('location:app.php');
}elseif(美元邮政){
如果(isset($\u会话['error'])){
取消设置($_会话['error']);
}
$questions=array();
对于($i=1;$i$val){
$check.=$val;
}
$metreq=strcmp($check,“1111-1-1-11-1-1”);
如果(!$metreq){
$_课时['questionaire]=$问题;
标题(“位置:app.php”);
}否则{
标题('location:nq.php');
}
}
///第2页
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
require_once('inc/state_list_options.php');
if(isset($_SESSION['questionaire']) || isset($_SESSION['id'])){
$id = $first_name = $middle_name = $last_name = $suffix = $address = $city = $state = $zip = $phone_area = $phone_exch =
$phone_num = $phone2_area = $phone2_exch = $phone2_num = $email_address = $birth_month = $birth_day = $birth_year =
$job = $referred = "";
$selected = "selected";
$questions = array();
//here we will generate a session id that is the code passed from the link clicked by the user
$conn = mysql_connect('host', 'user', 'password');
mysql_select_db('db', $conn);
if($conn){
if(isset($_SESSION['id'])){
$id = $_SESSION['id'];
$q = "SELECT * FROM eagle.pre_application WHERE id = '$id'";
$r = mysql_query($q, $conn);
$row = mysql_fetch_assoc($r);
$first_name = $row['first_name'];
$middle_name = $row['middle_name'];
$last_name = $row['last_name'];
$suffix = $row['suffix'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$phone_area = $row['phone_area'];
$phone_exch = $row['phone_exch'];
$phone_num = $row['phone_num'];
$phone2_area = $row['phone2_area'];
$phone2_exch = $row['phone2_exch'];
$phone2_num = $row['phone2_num'];
$email_address = $row['email_address'];
$birth_month = $row['birth_month'];
$birth_day = $row['birth_day'];
$birth_year = $row['birth_year'];
$questions['q1'] = $row['q1'];
$questions['q2'] = $row['q2'];
$questions['q3'] = $row['q3'];
$questions['q4'] = $row['q4'];
$questions['q5'] = $row['q5'];
$questions['q6'] = $row['q6'];
$questions['q7'] = $row['q7'];
$questions['q8'] = $row['q8'];
$questions['q9'] = $row['q9'];
$questions['q10'] = $row['q10'];
$questions['q11'] = $row['q11'];
$_SESSION['questionaire'] = $questions;
}
$q2 = "SELECT job_posting.id, job_posting.title, location.city, location.state
FROM eagle.job_posting
LEFT JOIN eagle.location
ON job_posting.location__Id = location.id";
$j = mysql_query($q2, $conn);
?>
<!-- job application goes ehre -->
<?
}else{
$_SESSION['error'] = "Please fill out the questionaire before completing the applicationn.";
header('location: index.php');
exit;
}
}else{
echo "There is an error";
}
?>
我不能100%确定从这一点上向前走。我原以为是头球造成的,但这从来没有出现过,所以现在我很困惑。请让我知道你对此的看法。如果你需要额外的代码或进一步的例子,请让我知道,我可以为你提供它们。提前感谢任何前来帮忙的人
编辑/发展/更新
上午9:07:使用chrome上的开发者工具,它现在在提交到第2页时收到一个500服务器错误
上午9:16删除了一次在require_中发现的错误(“”);路径中有一个错误。现在,它会自动转到第2页的重定向,即使通过地址栏手动在标题中传递变量也是如此。我还是被难住了。更新代码以反映更改侧注:
标题('location:xxx.php');退出这是我以前从未见过的一种新的标准实践,它有什么好处?它阻止了脚本的其余部分执行。(退出)将其添加到所有标题中。这从永远以来都是标准做法。它可以防止不需要的信息(如果有的话)在发送头后被进一步发送