Page 1 of 1

header Location without using GET

Posted: Tue Oct 11, 2005 3:57 pm
by kristolklp
This is a newbie question, but is there a way I can pass variables therough the header without using GET? Basically I am passing variables from one PHP file to another but I was wondering if there was simple way to pass ALL variables (ie POST?).

Code: Select all

header("Location:next_page.php?first=$first&last=$last&email=$email&...........)
I appreciate any input.

Posted: Tue Oct 11, 2005 4:03 pm
by feyd
sessions.. however you need to make sure that the session header is output, because a header level redirection can often short circuit what php will actually send..

session_write_close()

Posted: Tue Oct 11, 2005 4:09 pm
by kristolklp
ok thanks, any good tutorials on sessions?

Posted: Tue Oct 11, 2005 5:27 pm
by kristolklp
Ok, I am trying to learn sessions and am putting together a quick example....but Iam getting header errors :(

sessions.php >> Enter info and send to payment page if no errors

Code: Select all

<?php
$last="";
$first="";
$error="";

session_start();

if ($_POST) {
if(!empty($_POST['first']) && !empty($_POST['last'])) {
	$_SESSION["first"] = $first;
	$_SESSION["last"] = $last;
	session_write_close();
	header("Location: ./payment.php");
	
}else{
	$error = "All fields are required.";
}
}
?>

<?php echo $error; ?>
<form method="post" action="sessions.php" enctype="multipart/form-data">
First Name: <input type="text" name="first" value="<?php echo $_POST['first']; ?>"><br>
Last Name: <input type="text" name="last" value="<?php echo $_POST['last']; ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
payment.php >> Trying to pull variable from session data and assign (instead I recieve header errors)

Code: Select all

<?php
session_start(); //this is causing an error

$last=$_SESSION['last'];
$first=$_SESSION['first'];
?>

<html>
<head>
<title>Payment</title>
</head>

<body>
First Name: <input type="text" name="first"value="<?php echo $first; ?>"><br />
Last Name: <input type="text" name="last" value="<?php echo $last; ?>"><br />
</body>
</html>
Thanks for any help!

Posted: Tue Oct 11, 2005 5:34 pm
by Chris Corbyn
Have you got any whitespace or outputted data before the session_start() ? That's the most common cause and a quick search on these forums should bring up some useful results :)

Posted: Tue Oct 11, 2005 5:37 pm
by kristolklp
Thanks, putting the session_start(); before my variable got rid of the error, but now my second page is still not pulling the variables from th session data. Looking into it.

Thanks.

Posted: Tue Oct 11, 2005 5:48 pm
by pickle
Kind of my own question: I haven't been able to find any resources about this, but shouldn't it be possible to send POST data with header()? All you're really doing is sending http headers correct?

Posted: Tue Oct 11, 2005 5:57 pm
by Ambush Commander
Yeah, but you're sending it to the client, right? So a webbrowser should ignore the headers I guess...

Posted: Tue Oct 11, 2005 6:14 pm
by kristolklp
Ok I can't seem to get this to work. I seem to be saving the variables fine, but am I doing something wrong in my code (payment.php) to retrieve the data from the session variables?

Posted: Tue Oct 11, 2005 8:13 pm
by kristolklp
figured it out.

Code: Select all

<?php
session_start();
header("Cache-control: private"); 
$error="";

if ($_POST) {
if(!empty($_POST['first']) && !empty($_POST['last'])) {
	$_SESSION['first'] = $_POST['first'];
	$_SESSION['last'] = $_POST['last'];

	header("Location: ./payment.php");
	
}else{
	$error = "All fields required.";
}
}
?>

<?php echo $error; ?>
<form method="post" action="sessions.php" enctype="multipart/form-data">
First Name: <input type="text" name="first" value="<?php echo $_POST['first']; ?>"><br>
Last Name: <input type="text" name="last" value="<?php echo $_POST['last']; ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
then:

Code: Select all

<?php
session_start();
header("Cache-control: private"); 
?>

<html>
<head>
<title>Payment</title>
</head>

<body>
First Name: <input type="text" name="first"value="<?php echo $first; ?>"><br />
Last Name: <input type="text" name="last" value="<?php echo $last; ?>"><br />
</body>
</html>

Posted: Wed Oct 12, 2005 8:08 am
by Jenk
On a side note, ensure that you use exit() immediately after calling header() to prevent the rest of the script running when it doesn't need to.