Page 1 of 1

Some session problems

Posted: Mon Apr 05, 2004 12:30 am
by John Cartwright
Whenever I add

Code: Select all

<?php
session_start();
?>
to any document on my server I get

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/jcart/public_html/active/register.php:3) in /home/jcart/public_html/active/register.php on line 4

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/jcart/public_html/active/register.php:3) in /home/jcart/public_html/active/register.php on line 4

Also, I have this script which is a register script for a forum software... it has a lot of erorr checking and is somewhat complex. This script was rushed and isnt quite as efficient as it should be but this is only version1 of this whole script that I'm doing...

here is the code

Code: Select all

<link href="inc/css.css" rel="stylesheet" type="text/css">

<?
session_start();
include("inc/connect.php");

//assigning empty variables
$missing = array();

//setting variables for which page should be loaded
if (isset($personal)){
$status=2;
}else{
$status=1;
}
$error=0;

//error checking
$missing = array(); 
if ($submit == 'Continue'){ 
  	if(!empty($_POST)) { 
    	if ($status==1){
			$required = array('hosting','timelicense');  }
		elseif ($status==2){
			$required = array('firstname','lastname','password','email','contact');  }	
    	foreach($required as $req){ 
      		if(empty($_POST[$req])){ 
        		$missing[] = $req; 
        		$error += 1; 
      		}else{ 
        		$status=0;
				
     } } } }  

if (($status==1 || $error==1 || $error==2) && (!$personal)){ 

//setting variables for filled in fields

if(!empty($_POST['submit'])){ 
	if ($timelicense=="Life Time Licence"){           $timelicense1 = "checked"; }
	elseif ($timelicense=="One Year License")   {     $timelicense2 = "checked"; }
	if ($hosting=="Forum hosting package AB100"){     $hosting1 = "checked"; }
	elseif ($hosting=="Forum hosting package AB150"){ $hosting2 = "checked"; }
	if ($optional=="Copywright Removal"){             $optional1 = "checked"; }
	} 

include("form.php");

//when form is submitted and no errors

}elseif ($status==0 && (!$personal)){

//assigning the sessions to variables
if (!isset($_SESSION['timelicense'])){ $_SESSION['timelicense'] = $timelicense; }
if (!isset($_SESSION['hosting'])){$_SESSION['hosting'] = $hosting; }
if (!isset($_SESSION['optional'])){$_SESSION['optional'] = $optional; }

//setting the query
   $result = @mysql_query("SELECT * FROM prices"); 
   if (!$result) { 
   echo("<p>Error performing query: ". mysql_error()); 
   exit(); 
   } 
 
//getting the database information
    while ( $row = mysql_fetch_array($result)) { 
	$lifetimeL  = $row[0]; 
	$oneyearL   = $row[1]; 
	$AB100      = $row[2]; 
	$AB150      = $row[3]; 
	$copywright = $row[4];
	}

//if no optional copywright is false
if (empty($optional)){ $copywright="0"; }

if ($timelicense=="Life Time License"){ $licenseprice=$lifetimeL; }else{ $licenseprice=$oneyearL; }
if ($hosting=="Forum hosting package AB100"){ $hostingprice=$AB100; }else{ $hostingprice=$AB150; } ## GOTTO FIX THE PRICING

?>
<div align="center" class="text">Thank you for showing an interest in AG.</div>
<br>
<? //confirming the users order ?>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0" class="error">
  <tr> 
    <td class="steps">Step 2/3 - Confirming your License</td>
  </tr>
</table>
<table class="text" align="center" width="50%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
    <tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">Your chosen Purchase Items: </td> </tr>
    <tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">&nbsp; - <? echo $_SESSION['timelicense']; echo " $".$licenseprice; ?> </td> </tr>
    <tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">&nbsp;</td> </tr>
    <? if ($_SESSION['hosting'] != "None" || (isset($_SESSION['optional']))) { ?>
	<tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">Your Purchase Optional Items:</td> </tr>
	<? } if ($_SESSION['hosting'] != "None"){ ?>
    <tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">&nbsp; - <? echo $_SESSION['hosting']; echo " $".$hostingprice; ?> </td> </tr>
    <? } if (!empty($optional)){ ?>
	<tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">&nbsp; - <? echo $_SESSION['optional']; echo " $".$copywright; ?> </td> </tr> 
	<? } ?>
    <tr> <td bordercolor="#EEEEEE" bgcolor="#EEEEEE">
	<div align="right">Total: <? $total=$licenseprice+$hostingprice+$copywright; echo "$".$total; ?></div>
	</td> </tr>
    </table>
    
	<br>

    <div align="center">
    
  <table width="50%" border="0" cellpadding="0" cellspacing="0">
    <tr bgcolor="#EEEEEE"> 
      <td width="50%" height="26"><input name="button" type="button" onClick="history.back()" value="Back"></td>
      <form name="register" method="post" action="register.php">
        <td>&nbsp;</td>
        <td width="50%"> <div align="right"> <input name="submit" type="submit" id="submit" value="Continue"></div></td>
        <input type="hidden" name="personal" value="2"/>
      </form>
    </tr>
  </table>
    </div>
<?

}elseif ($personal){

if (($personal == 2 && $error != 0) || $error==0){
	if ($error != 0){
		if ($email != $email2){
			$errormessage='Your emails do not match, please go back and enter identical emails. Make sure your emails are correct as they are required for futur uses.';
			$errormsg=1;
		}elseif ($password != $password2){
			$errormessage='Your passwords do not match, please go back and enter identical passwords.';
			$errormsg=1;
		}

include("form.php");
}elseif ($error=="0"){ 

$_SESSION['firstname'] = $firstname;
$_SESSION['lastname']  = $lastname;

?>



<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0" class="steps">
  <tr>
    <td>Purchase Summary</td>
  </tr>
</table>
<table width="50%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000" bgcolor="#EEEEEE" class="text">
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>LICENSE</td>
    <td>&nbsp;</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Type:</td>
    <td><? echo $_SESSION['timelicense']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Hosting:</td>
    <td><? echo $_SESSION['optional']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Optional:</td>
    <td><? echo $_SESSION['optional']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Total:</td>
    <td>$<? echo $_SESSION['total']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td colspan="2">PERSONAL</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td width="3%">&nbsp;</td>
    <td width="14%">&nbsp;</td>
    <td width="83%">&nbsp;</td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Firstname: </td>
    <td><? echo $_SESSION['firstname']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Lastname:</td>
    <td><? echo $_SESSION['lastname']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Contact #:</td>
    <td><? echo $_SESSION['contact']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td>&nbsp;</td>
    <td>Email:</td>
    <td><? echo $_SESSION['email']; ?></td>
  </tr>
  <tr bordercolor="EEEEEE"> 
    <td height="20">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

<?
	if ($confirmed==1)
		$sql = "INSERT INTO users SET firstname='$firstname',lastname='$lastname',password='$password',email='$email',contact='$contact',comments='$comments'"; 
   
  		if (@mysql_query($sql)) { 
    		echo("Your request for a license has be submitted to our database. Check your email to confirm your information and you will be contacted withen 48 business hours."); 
  		} else { 
    	echo ("Error adding your request, please try again later. " . mysql_error()); 
  		} 
	}
}
}

//debugging purposes
echo $email;
echo $personal;
echo $status;
echo $error;

echo $_SESSION['timelicense']; 

?>
As you can see after the first time they see form.php (which contains different forms and loads a certain one depending on what part your @ ), and they submit their info with no error it brings them to a place where they confirm it... the session is set b4 the page is loaded as you can see.. but after the next submit the session (timelicense, total, etc) is lost... any ideas why?

Posted: Mon Apr 05, 2004 12:34 am
by Paddy
session_start() has to be the absolutely first thing on your page.

Posted: Mon Apr 05, 2004 12:36 am
by John Cartwright
rofl rofl I never knew that :) :) you just made my life a million times easier

thanks bud
cheers

Posted: Mon Apr 05, 2004 10:35 am
by tim
viewtopic.php?t=6521
There is one catch when using sessions that most new people run into. You see, you CAN'T HAVE ANY OUTPUT before you start the session with session_start(). One bit of whitespace will give you a header error...