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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.