English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to use the data in a variable created in a page and I am now on another page and there is no forms in the first page so I can't post the variable to the current one what can I do???

2006-11-29 04:48:48 · 3 answers · asked by I.M. 2 in Computers & Internet Programming & Design

3 answers

You have three basic choices:

First, you could store the data in a database or text file. Probably not a good solution if you are only using it once or a few times.

Second, you could use session variables. You will have to put session_start() at the top of your PHP script (before any HTML outputs to the browser). Then you can use $_SESSION['myvar'] like you would use $myvar, but it will exist for the duration of their session and not be erased when the page finishes loading. (Make sure that register_globals is not turned on - if it is, $_SESSION['myvar'] and $myvar will be the same thing. This is a security hole. With superglobals turned off, $_SESSION['myvar'] and $myvar are separate variables, and you will have to assign and read $_SESSION['myvar'] instead of $myvar. In PHP 4.2.0 and later, register_globals is OFF by default. In PHP 6, it was removed because of the security problem caused by having it turned ON.)

Third, you could generate an invisible form with elements containing the data. The next page would be accessed by submitting the form [either directly using a submit button, or via a javascript link which called form.submit()]. You could then access the data on the next page using $_GET[] or $_POST[] (depending on which method your form was submitted via).

2006-11-29 05:28:42 · answer #1 · answered by computerguy103 6 · 0 0

If I'm reading you correctly, you want to create a variable once and have it accessible by all other pages? If so, the way I would do this is to create a "config.php" file with all master variables contained therein.

Example:
$siteName = 'My Website'; // This variable can be changed further down the page
define("SITENAME", "My Website"); // This cannot be changed once defined

All other pages can include this "config.php" and use any variables as if they were defined in that page.

include("includes/config.php");
echo $siteName; // write out the variable
echo SITENAME; // write out the constant

2006-11-29 14:50:34 · answer #2 · answered by Poncho 3 · 0 0

There's always session variables. Be careful, though. They add a layer of insecurity that you'd need to work around.

http://us3.php.net/session

2006-11-29 04:51:11 · answer #3 · answered by BicMan11 2 · 0 0

fedest.com, questions and answers