重定向到感谢页面后SESSION值没有传递

时间:2014-01-21 02:27:04

标签: javascript php jquery

我正在尝试将$email字段传递给感谢您在用户提交查询表单后重定向后显示的页面。

这是一个两步查询表,谢谢你的页面是最后一页。

似乎SESSION在感谢页面上存在但是值丢失了。我想在感谢页面上将$email字段发布到iframe。请让我知道会话ID出错的地方?

以下是代码:

第1步:小型咨询表

 <?php 
 error_reporting(0);
 session_start();
 require_once('validation.class.php');
 if(isset($_REQUEST['btnSubmit']) == 'Next'){
 $obj = new validation();
 $obj->add_fields(trim($_POST['txt_fname']), 'req', 'Enter your first name.');
 $obj->add_fields(trim($_POST['txt_contact']), 'req', 'Enter phone number.');
 $obj->add_fields(trim($_POST['txt_finamount']), 'req', 'Enter the amount.');
 $obj->add_fields(trim($_POST['sel_loantype']), 'req', 'Please select vehicle type.');



$error = $obj->validate();



if($error){

            $error_msg =  "".$error."";     

            $_SESSION['error_msgs'] = $error_msg;

            header("location:".$_SERVER['HTTP_REFERER']."");

            exit(); 

}else{



             $_SESSION['form1data'] = $_REQUEST;

             header("location: quick-quote.php");

             exit();



            /*$fname = trim($_REQUEST["txt_fname"]);

            $surname = trim($_REQUEST["txt_surname"]);

            $phone = trim($_REQUEST["txt_contact"]);

            $finamount = trim($_REQUEST['txt_finamount']);

            $sel_loantype = trim($_REQUEST['sel_loantype']);



            $message = '<html><body>';

            $message .= '<table rules="all"  width="100%" style="border:1px solid #666;" cellpadding="10">';

            $message .= "<tr><td><strong>First Name:</strong> </td><td>" . strip_tags($fname) . "</td></tr>";

            if($surname != ''){

            $message .= "<tr><td><strong>Surname:</strong> </td><td>" . strip_tags($surname) . "</td></tr>";

            }

            $message .= "<tr><td><strong>Phone:</strong> </td><td>" . strip_tags($phone) . "</td></tr>";

            $message .= "<tr><td><strong>Amount to Finance:</strong> </td><td>" . strip_tags($finamount) . "</td></tr>";

            $message .= "<tr><td><strong>Loan Type:</strong> </td><td>" . strip_tags($sel_loantype) . "</td></tr>";

            $message .= "</table>";

            $message .= "</body></html>";





            $ToEmail       = 'testemail@gmail.com'; 

            $EmailSubject  = "GET A QUICK QUOTE from ".strip_tags($fname); 

            $mailheader    = "From: ".strip_tags($fname)."\r\n"; 

            //$mailheader   .= "Reply-To: ".$_REQUEST["txt_email"]."\r\n"; 

            $mailheader   .= "Content-type: text/html; charset=iso-8859-1\r\n";

            $MESSAGE_BODY  = $message;



            if(@mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)){

                  $_SESSION['sucess'] = "Your message has been sent successfully.";

                  $_SESSION['form1data'] = $_REQUEST;

                  header("location: quick-quote.php");

                  exit; 

            }else{

                  $_SESSION['sucess'] = "Sorry! Your message has not been sent.";

                  $_SESSION['form1data'] = $_REQUEST;

                  header("location: quick-quote.php");

                  exit; 

            }*/

}



 } 



 ?>

第2步代码:

 <?php 
 error_reporting(0);
 session_start();
 require_once('validation.class.php');
 ?>
 <script type="text/javascript">
 function submitToCRM()
 {
 $.ajax({
      type: 'POST',
      url: 'http://test.com.au/quick-quote/car-finance/quickquote-one.php',
      data: $("#applynowform").serialize(),
      beforeSend: function () {
                                 $("#loadingimg").show();
                              }, 
      success: function (){
        //alert(data);
         window.location.href = "http://www.test.com.au/thank-you";  
      }
});         

步骤3:上面的页面将数据发送到quickquote-one.php表单处理脚本,该脚本具有以下代码。

 <?php
 if(!isset($_SESSION)) 
 { 
  session_start(); 
 } 
 $_SESSION['user_email'] = $_POST['email'];

第4步:感谢您的页面(此页面包含以下代码)

 <?php
 if(!isset($_SESSION)) 
 { 
 session_start(); 
 $_SESSION['user_email'] = $_POST['email'];
 echo $_SESSION['user_email'];
 } 
 ?>

3 个答案:

答案 0 :(得分:0)

添加

session_set_cookie_params(0);

之前

session_start();

您还可以使用网址在页面之间传递SID(会话ID),以确保在转换过程中不会丢失。

url: 'http://test.com.au/quick-quote/car-finance/quickquote-one.php?<?php echo htmlspecialchars(SID); ?>',

window.location.href = "http://www.test.com.au/thank-you?<?php echo htmlspecialchars(SID); ?>";  

您正在丢失会话,因为您将浏览器发送到没有相对路径的下一个URL,而是一个完全限定的域。这是一种安全措施,可防止会话ID被无意中发送到错误的服务器。

另一个小解决方案是使用相对路径,例如/page.php而不是http://www.domain.com/page.php

Read more here (PHP Manual)

答案 1 :(得分:0)

在第4步中设置

$_SESSION['user_email']

一次。如果表单已刷新,则您的帖子为空,并且您使用空值覆盖会话。尝试从步骤4中删除它,然后离开。

 <?php
 if(isset($_SESSION['user_email']) && !empty($_SESSION['user_email'])) 
 { 
 echo $_SESSION['user_email'];
 } 
 ?>

如果未设置会话,您也会尝试回显会话电子邮件值。如果会话实际设置,我认为这不会起作用。

答案 2 :(得分:0)

我认为如果所有页面都相互连接,最好在一个页面中启动会话并访问所有会话变量。这将是我们创建登录页面的一些事情。当用户登录捕获时会在会话中显示所需的值,即使在刷新后也可以通过应用程序访问。

$query = "SELECT Useid,UserName,AccountStatus, FullName FROM Users WHERE UserName = :UserName"; 

从这个查询我们可以轻松获得会话变量。

 if($login_ok) 
        { 

             //for last visit
            $Month = 2592000 + time(); 
 //this adds 30 days to the current time 
 setcookie(AboutVisit, date("F jS - g:i a"), $Month);
 //last visit ends here.

            $_SESSION['user'] = $row['UserName']; 
            $_SESSION['userid'] = $row['Useid'];
            $_SESSION['fullname'] = $row['FullName'];
            }

你需要这个变量,你可以像这样使用

$username=$_SESION['user'];

我认为每次在每个页面中都会有效,而不是启动会话。 希望它有所帮助