Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session variable problem

Status
Not open for further replies.

dazza12345

Programmer
Nov 25, 2005
35
GB
Hi all, what im trying to do is as follows:

I have a form, when the submit button is clicked, the values from the form elements will be saved into Session variables before a different php file is loaded. I require the session variable in the new php file that is loaded.

Here is the code that I have already:

index.php
Code:
<?
ob_start(); 
include './cookie-include.php'; 

if(isset($_POST['submit_main'])) { 
foreach($_POST as $key => $data) {        
    $_SESSION[$key] = $data; 
    } 
header("Location: ./test.php"); 
exit(); 
} 
?>

<form method=post action=<?php echo $_SERVER['PHP_SELF']; ?> name="myform" id="myform"> 

MANY FORM ELEMENTS

<input type="submit" name="submit_main" id-"submit_main" />

test.php
Code:
<?
session_start(); 
echo $_SESSION['banking'];
?>

However, at present, I cannot get any of the session variables to print on the screen in test.php, all I can get is the follwoing error:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/fhlinux203/b/bewisebets.co.uk/user/htdocs/bewisepoker/test.php:1) in /home/fhlinux203/b/bewisebets.co.uk/user/htdocs/bewisepoker/test.php on line 2

Im sure this has something to do with the include './cookie-include.php'; line in index.php. As this line includes a php file that will save cookies.

has any1 got any idea how I could get around this error?
 
You need to ensure that session_start() is not called if any other output has already been sent to the page. This is the same as for when you are sending headers. Just move it to the very top of the page and then try.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
i have session_start(); right at the top of test.php page. Do i need to add it to the top of index.php too?
 
Yup, you would need it at the top of index.php too since you're populating the session variables there. Also, know that output is also any html tags (or doctype) before your opening <?php tag and any whitespace (space, carriage return) before that tag as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top