header Location without using GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

header Location without using GET

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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()
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

Post by kristolklp »

ok thanks, any good tutorials on sessions?
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yeah, but you're sending it to the client, right? So a webbrowser should ignore the headers I guess...
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

Post 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?
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

Post 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>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply