Jump to content


Photo

Carrying variables to other pages


  • Please log in to reply
4 replies to this topic

#1 jo.nova

jo.nova
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 02 August 2006 - 07:26 PM

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?

#2 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 02 August 2006 - 07:32 PM

page1.php
<?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."");
?>

page2.php
<?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 />";
?>


#3 jo.nova

jo.nova
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 02 August 2006 - 07:47 PM

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?



#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 02 August 2006 - 08:00 PM

What are you printing to the browser in page1.php?

If you're printing anything out to the browser on page1.php, you won't be able to use the Header() 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:

page1.php
<?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>";

?>

page2.php
<?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 />";
?>


#5 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 02 August 2006 - 11:04 PM

look into using sessions

http://codewalkers.c...rials/32/1.html

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users