Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] Probably easy PHP question

Started by ed209 Feb 25, 2008 at 1:17 PM 3 replies 900+ views
Original Post
ed209
ed209
How do you pass a variable to another script without using a form?
rogierpennink
rogierpennink
You can use sessions for simple numeric or text variables...

i.e. simply call session_start() at the beginning of the scripts where you want to use session variables, after which you can set session variables by simply adding to the $_SESSION array.

Note, it is important that you call session_start before any output is sent..

Atom
Atom
If you know the url of where the script is at then you can pass a variable like so:

www.someurl.com/script.extentsion?variable_name=$variable_value

Edit: Forgot to mention that the variable value can be anything to you want.

www.someurl.com/script.extentsion?user_info=$age:$name:$address

and now in the other script that is receiving the data I can call

$data = explode(":", variable_name)
Wan
Wan
A page (request) or a script?
Cygnus_X
Cygnus_X
It depends on your definition of script. If you're referencing a second page, then you can either use $_SESSION or $_GET (of course, you use $_POST when working with forms)

With $_SESSION, you'll need to start a session for the user (by calling session_start(); at the beginning of both pages), then you can store variables in the form of $_SESSION['Variable'] = Value; The next page can then reference this value by looking at $_SESSION['Variable']

With $_GET, you can store the value in the URL. This does not require a session. So, with the following link..... www.somepage.index.php?Var1=value&Var2=value2, you can use $_GET['Var1'] and $_GET['Var2'] to get the respective values.


If by 'script', you mean function.... then simply write your functions this way.

function my_function($Variable)
{
//do something
return $SomeValue;

}//end of funciton


you can then call $SomeValue = my_function('1');

I hope that helps.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.