Passing variables between same script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fetty
Forum Newbie
Posts: 2
Joined: Wed Sep 01, 2004 1:26 pm

Passing variables between same script

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://www.domain.com?foo=bar

and you can access the variable $foo by using

echo $_GET["foo"] // returns bar
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

yes his right...

coding an entire site in one php script is complicated... php will be slow,, and not so efficient...
fetty
Forum Newbie
Posts: 2
Joined: Wed Sep 01, 2004 1:26 pm

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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 =]
Post Reply