Gwayn Posted February 12, 2007 Share Posted February 12, 2007 Hi all, i got a question: i'm creating a cms, i want to handle the language choose, and i thought to handle it by setting a variable in the index.php... a variable that every other script and page can read. Now, i think it can be done using globals, but how? If i define "global $var;" in index.php, the other scripts do not see that variable... so how can i do? Thanks for the help. Bye, Stefano. Link to comment https://forums.phpfreaks.com/topic/38168-php-globals/ Share on other sites More sharing options...
kenrbnsn Posted February 12, 2007 Share Posted February 12, 2007 To use variables that can been seen from different scripts you need to use Sessions. Put <?php session_start(); ?> at the start of each script. To store a session variable use <?php $_SESSION['somevar'] = $somevar; ?> To use it or retrieve it use <?php echo $_SESSION['somevar']; $var2 = $_SESSION['somevar']; ?> Ken Link to comment https://forums.phpfreaks.com/topic/38168-php-globals/#findComment-182690 Share on other sites More sharing options...
steelmanronald06 Posted February 12, 2007 Share Posted February 12, 2007 That isn't a global var...globals is by default nulled on most servers, and destroyed in future versions of PHP because it presents a security risk. Ken showed one way of doing it. Another is to put the variable in master.php or some other file and then include it at the top of every file you want that variable to appear on: master.php <?php $global = "somevar"; ?> index.php <?php include ($_SERVER['DOCUMENT_ROOT'] . '/index.php'); echo $global; ?> there are a few other ways to do it as well. Auto_prepend is one choice. Link to comment https://forums.phpfreaks.com/topic/38168-php-globals/#findComment-182727 Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 You could also use Constants if they're non-changing values, as long as you include that file in every other. Link to comment https://forums.phpfreaks.com/topic/38168-php-globals/#findComment-182745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.