Jump to content

PHP Globals?


Gwayn

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.