Jump to content

Archived

This topic is now archived and is closed to further replies.

jo.nova

Carrying variables to other pages

Recommended Posts

So, I have some variables in a PHP script that I would like to carry over to a different page when it loads. How do I do it?  With global variables? How would I use them?

Share this post


Link to post
Share on other sites
[b]page1.php[/b]
[code=php:0]
<?php

// Define variables
$var1 = "foo";
$var2 = "bar";

// Go to a new location, with the vars carried over
Header("Location: page2.php?var1=".$var1."&var2=".$var2."");
?>
[/code]

[b]page2.php[/b]
[code=php:0]
<?php

// Grab the variables the from the $_GET superglobal array
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

// Echo the variables
echo "Var 1: ".$var1."<br />";
echo "Var 2: ".$var2."<br />";
?>
[/code]

Share this post


Link to post
Share on other sites
Hmmm...That didn't work.

Here's what I got as a response:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/r/o/c/rocknroll124/html/site/sendfile/test1.php:9) in /home/content/r/o/c/rocknroll124/html/site/sendfile/test1.php on line 16

I read in the manual:

"Note:  Since PHP 4.4.2 and PHP 5.1.2 this function prevents more than one header to be sent at once as a protection against header injection attacks."

Is there another way?

Share this post


Link to post
Share on other sites
What are you printing to the browser in [b]page1.php[/b]?

If you're printing anything out to the browser on [b]page1.php[/b], you won't be able to use the [b]Header()[/b] method, as not other output can be made to the browser. This method works well for pages that only process data, not display it.

If you want to print something out to the first page, and then carry the variables to another page, you can use a similar method with a hyperlink:

[b]page1.php[/b]
[code=php:0]
<?php

// Define variables
$var1 = "foo";
$var2 = "bar";

// Link to a new location, with the vars carried over
echo "<a href=\"page2.php?var1=".$var1."&var2=".$var2."\">Page2</a>";

?>
[/code]

[b]page2.php[/b]
[code=php:0]
<?php

// Grab the variables the from the $_GET superglobal array
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

// Echo the variables
echo "Var 1: ".$var1."<br />";
echo "Var 2: ".$var2."<br />";
?>
[/code]

Share this post


Link to post
Share on other sites
look into using sessions

http://codewalkers.com/tutorials/32/1.html

Lite...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.