jo.nova Posted August 2, 2006 Share Posted August 2, 2006 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? Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 2, 2006 Share Posted August 2, 2006 [b]page1.php[/b][code=php:0]<?php// Define variables$var1 = "foo";$var2 = "bar";// Go to a new location, with the vars carried overHeader("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 variablesecho "Var 1: ".$var1."<br />";echo "Var 2: ".$var2."<br />";?>[/code] Quote Link to comment Share on other sites More sharing options...
jo.nova Posted August 2, 2006 Author Share Posted August 2, 2006 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 16I 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? Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 2, 2006 Share Posted August 2, 2006 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 overecho "<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 variablesecho "Var 1: ".$var1."<br />";echo "Var 2: ".$var2."<br />";?>[/code] Quote Link to comment Share on other sites More sharing options...
litebearer Posted August 2, 2006 Share Posted August 2, 2006 look into using sessionshttp://codewalkers.com/tutorials/32/1.htmlLite... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.