Session variables lost

Discussion in 'PHP' started by billybrag, Nov 7, 2006.

  1. #1
    Hello all,

    im not sure if anyone will be able to help with this as it is a bit complex to explain but here goes.

    I have a file called Checkout.php which includes 3 files through the checkout process. In this process it assigns the value of orderId to the session so that it can be used to send the email once the order is completed. however when i include the file success, the value in the session is lost.
    this can be seen if i show you the 2 files that i am working with

    checkout.php
    
    <?php
    ini_set('display_errors', 1);
    ini_set('log_errors', 0);
    ini_set('error_reporting', E_ALL);
    ?>
    <?php
    require_once "include/header.php";
    ?>
    
    
    <title><?php echo $pageTitle="Checkout Process";echo" | ";?> Second Hand Books | Sharp's Book Co.</title>
    </head>
    <body>
    
    <div class="wrapper">
    
    <div class="wide top">
    <a href="http://www.weblinker.co.uk" class="no-border"><img src="images/logo.gif" alt="Sharp's Book Co." /></a>
    
    
    <div id="topbasket">
    <?php
    require_once "include/miniCart.php";
    ?>
    </div>
    <div id="topsearch">
    <?php
    require_once "include/minisearch.php";
    ?>
    </div>
    
    </div> 
    
    <div class="outer">
    
    <div class="float-wrap">
    
      <div class="center">
    <div id="crumb">
    <?
    require_once 'include/breadcrumb.php';
    ?>
    <? echo " >> " .$pageTitle; ?>
    </div>
      <div id="prodDetail">
    
    
    <?php
    
    if (isCartEmpty()) {
    	// the shopping cart is still empty
    	// so checkout is not allowed
    	header('Location: cart.php');
    } else if (isset($_GET['step']) && (int)$_GET['step'] > 0 && (int)$_GET['step'] <= 3) {
    	$step = (int)$_GET['step'];
    
    	$includeFile = '';
    	if ($step == 1) {
    		$includeFile = 'include/shippingAndPaymentInfo.php';
    		$pageTitle   = 'Checkout - Step 1 of 2';
    	} else if ($step == 2) {
    		$includeFile = 'include/checkoutConfirmation.php';
    		$pageTitle   = 'Checkout - Step 2 of 2';
    	} else if ($step == 3) {
    		$orderId     = saveOrder();
    		$orderAmount = getOrderAmount($orderId);
    		
    		echo $_SESSION['orderId'] = $orderId ;
    		
    		//-----this session is dumped out containing data-----
    		echo"<br /><br />the variable is...";
    		var_dump($_SESSION);
    		
    		
    		// our next action depends on the payment method
    		// if the payment method is COD then show the 
    		// success page but when paypal is selected
    		// send the order details to paypal
    		if ($_POST['hidPaymentMethod'] == 'cod') {
    			$includeFile = WEB_ROOT . 'success.php';
    			
    		} else {
    			$includeFile = 'include/paypal/payment.php';	
    		}
    	}
    } else {
    	// missing or invalid step number, just redirect
    	header('Location: index.php');
    }
    
    
    ?>
    <script language="JavaScript" type="text/javascript" src="library/checkout.js"></script>
    <?php
    include "$includeFile";
    ?>
    </div> 
    </div>
    <!-- end centered div -->
    
    <div class="left">
    <div class="container-left">
    
    <h3>Categories...</h3>
    
    <?php
    require_once "include/leftNav.php";
    ?>
    
    
    </div>
     </div> <!-- end left div -->
    
    </div>
    
    <div class="right">
    
    <div class="container-right">
    
    <h3>Featured Books...</h3>
    <?php
    require_once "include/rightNav.php";
    ?>
    </div>
    </div> <!-- end right div -->
     
    <br class="clear" />  <!-- using a <br /> here is less buggy than other choices -->
     
    </div>
    
    <div class="wide bottom clear">
    <?php
    require_once "include/footer.php";
    ?>
    
    </div> 
    
    </div>
    
    </body></html>
    
    PHP:
    which includes
    success.php
    
    <?php
    //-----this session is blank???-----
    echo"<br /><br />the variable is...";
    var_dump($_SESSION);
    
    $order_id = $_SESSION['orderId'];
    
    // send notification email
    
    	$subject = "[New Order] " .$order_id;
    	$email   = "mesharp@gmail.com";
    
    	$message = "You have a new order. Check the order detail here \n http://www.weblinker.co.uk/admin/order/index.php?view=detail&oid=" .$orderId;
    	mail($email, $subject, $message, "From: $email\r\nReturn-path: $email");
    
    
    
    
    //$result = MYSQL_QUERY("SELECT * from tbl_order WHERE od_id=$_SESSION[orderId];")
    //or die ("Name and password not found or not matched");
    
    ?>
    <p>&nbsp;</p><table width="100%" border="0" align="center" cellpadding="1" cellspacing="0">
       <tr> 
          <td align="left" valign="top" bgcolor="#333333"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr> 
                   <td align="center" bgcolor="#EEEEEE"> <p>&nbsp;</p>
                            <p>Thank you for shopping with us! We will send the purchased 
                                book(s) as soon as we confirm reciept of payment. </p>
    			<br />
    			<p>To continue shopping please <a href="index.php">click 
                                here</a></p>
                      <p>&nbsp;</p></td>
                </tr>
             </table></td>
       </tr>
    </table>
    
    
    PHP:
    If you look at the 2 bits where i dump the session (var_dump($_SESSION);) the first one before the include outputs it fine where as the second outputs an array(0)

    Any ideas on anything obvious that i could have missed,

    sorry about the post length
     
    billybrag, Nov 7, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Did you put session_start(); at the top of your pages? I can't see it anywhere.
     
    nico_swd, Nov 7, 2006 IP
  3. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the session_start is in the header file that is called, though it is not in the second file (sucess.php)
     
    billybrag, Nov 7, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It needs to be in all pages where you want to use $_SESSION variables.
     
    nico_swd, Nov 7, 2006 IP
  5. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmm, i tried adding it, and adding
    print SID; and the first one has a session id but the second does not, as though it doesnt exist
     
    billybrag, Nov 8, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Try adding this at the top of the page where you receive the error.
    
    error_reporting(E_ALL);
    
    PHP:
    Any new errors?

    Also, the session ID might not be passed to the second page. This could be cause you have cookies disabled, or PHP doesn't rewrite the urls and adds the session ID at the end of it.
     
    nico_swd, Nov 8, 2006 IP