Jump to content

Carrying variables to other pages


jo.nova

Recommended Posts

[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]
Link to comment
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?

Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.