Page 1 of 1

Passing variables between same script

Posted: Wed Sep 01, 2004 1:39 pm
by fetty
Okay I am working on my first site made entirely in php and I would like to have the entire site in one script. I want it too start by displaying one page and when a person clicks on a link I want it too pass a variable so the script and it use that variable to decide which part of the script to do.

Posted: Wed Sep 01, 2004 1:43 pm
by John Cartwright
http://www.domain.com?foo=bar

and you can access the variable $foo by using

echo $_GET["foo"] // returns bar

Posted: Thu Sep 02, 2004 7:34 am
by duk
you can use $_GET or $_POST

using hidden variables...

<form method=post action=same_page.php>
<input type=hidden name=var1>
<input type=hidden name=var2> etc...

<input type=submit value=next>

you use $_POST["var1"];

if ($var1)
{

page_2();

}

will execute the function that have the page_2

Posted: Thu Sep 02, 2004 7:45 am
by CoderGoblin
Just as an aside....

In most cases coding an entire site with one PHP script is not efficient. The entire PHP code needs to be loaded and processed.

Splitting the php scripts into useable components not only decreases the time it takes to serve the web page it makes the code more maintainable.

If you have not already seen them look up the commands include and require in the php manual and only load the code you need to.

The key to good, maintainable code is a proper, efficient design. It may seem to be an overhead when building the system but it saves lots of time later.

Posted: Thu Sep 02, 2004 8:13 am
by duk
yes his right...

coding an entire site in one php script is complicated... php will be slow,, and not so efficient...

Posted: Thu Sep 02, 2004 4:27 pm
by fetty
Normally I would say you were right but since this is not a large site, just a website about myself, and only one part of the site will be changed from page to page( ie keeping the top pannel and the side links) and only changing the content part I figured the best part would just put the different content pages into different variables and load them in as needed.

Posted: Thu Sep 02, 2004 5:27 pm
by tim
if your accessing the variables on different pages (going by the above advice) you should for sure assign them session vars.

$_SESSION is a super global that should be your friend =]