Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple variables between pages?

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
US
Hello,

I want to send multiple variables between two pages. I realize I can do this with sessions, but is there a more straight-forward manner? I don't need cookies or to save any user information.

Any help appreciated.
 
Like this:

Code:
### Page 1 ###

<?php

$sessionId = 233;

echo &quot;<a href=\&quot;page2.php?sessionId=$sessionId\&quot;>next page</a>&quot;;

?>

### Page 2 ###

<?php

$sessionId = $_GET[sessionId];

echo &quot;Your sessionId is: $sessionId&quot;;

?>
Regards
David Byng
bd_logo.gif

davidbyng@hotmail.com
 
Misunderstanding here, I believe.

JediDan,

You can send many variables in a GET (URL query string, simply by separating them with &quot;&&quot;:


Code:
echo &quot;<a href=\&quot;page2.php?var1=$var1&var2=$var2&var3=$var3_etc...\&quot;>next page</a>&quot;;
You might have to urlencode your variables before sending them, if they contain complex characters, spaces, etc... (
On page2.php, depending on whether you have register_globals enabled, you can either directly refer to $var1, var2, etc... or use the $_GET array: $_GET['var1'], $_GET['var2'], etc...

Note: GET requests usually allow you to send a maximum of 1K characters, or something like that, for most browsers, while a POST often allows for several MB of data. -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Suspicion is Trust&quot;
(
 
Read the post. JediDan is wondering how you do it without sessions.

By the way, there is are a couple additional ways to send multiple variables:

1. &quot;argv&quot; which can be nice for sending numerically indexed arrays: &quot;page.php?element1+element2+element3+element4&quot; (notice there is no equals sign here). This will be stored in $_SERVER[&quot;argv&quot;], and produces the following array:

Code:
Array
(
    [0] => element1
    [1] => element2
    [2] => element3
    [3] => element4
)

2. Fake directory paths: &quot;page.php/string1/string2/string3/string4&quot;. This produces a string stored in $_SERVER[&quot;PATH_INFO&quot;]. It's just one string, but it can be easily separated into an array:
Code:
$my_array = explode('/', $_SERVER[&quot;PATH_INFO&quot;]);

//produces array
Array
(
    [0] => string1
    [1] => string2
    [2] => string3
    [3] => string4
)

(Note: PATH_INFO doesn't work with PHP/IIS on Windows, or at least, I haven't been able to find a way. But it does work with Apache on Windows.)

Here's a fun exercise: run the standard &quot;phpinfo&quot; page ( <?php phpinfo(); ?> ), and experiment with putting different values on the query string, as demonstrated here. The output of the phpinfo page will show you how these variables are translated in PHP. You can also submit forms to the phpinfo page as a form handler and see how your form variables are received. -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Suspicion is Trust&quot;
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top