Page 1 of 1
Global vars accessing in other pages
Posted: Fri Jun 15, 2007 2:42 am
by rozvinbm_jp
Code: Select all
//globals.inc
<?php
global $data;
?>
Code: Select all
//page1.php
<?php
require_once 'globals.inc';
echo '<br /><a href="page2.php">page2</a>';
?>
Code: Select all
//page2.php
<?php
require_once 'globals.inc';
if (isset($GLOBALS['data'])){
echo "was set.";
}else{
echo "was not set.";
}
?>
Problem: why it always set to
NULL the
$GLOBALS['data'] in page2. Global vars are not accessible outside of every php file?
Please teach me.
Re: Global vars accessing in other pages
Posted: Fri Jun 15, 2007 3:18 am
by Gente
rozvinbm_jp wrote:
Code: Select all
//page2.php
<?php
require_once 'globals.inc';
if (isset($GLOBALS['data'])){
echo "was set.";
}else{
echo "was not set.";
}
?>
Strange comment
At the first please assign some value to data

At the second please check register_globals php flag.
At the third use following syntax (to avoid this situdation):
Code: Select all
<?php
//globals.inc
$GLOBALS['data'] = 'Your value';
?>
Posted: Fri Jun 15, 2007 3:36 am
by rozvinbm_jp
Code: Select all
//page1.php
<?php
require_once 'globals.inc';
$GLOBALS['data'] = 123;
echo '<br /><a href="page2.php">page2</a>';
?>
opppss sorry i forgot to assign the value. The $GLOBALS['data'] was initialized in page1.php. Then, I tried to access the value of $GLOBALS['data'] in other php pages.
Please teach me.
Posted: Fri Jun 15, 2007 3:44 am
by Gente
Oh... If you assign some global value in page1.php it doesn't means it appears in all of your scripts.
It means you can use it inside function, classes etc. but only in page1.php (sure if it's your final script)
Don't mix $GLOBALS and $_SESSION
Code: Select all
<?php
//globals.php
$GLOBALS['data'] = 123;
?>
This way $GLOBALS['data'] will be set in every script where you include globals.php
Posted: Fri Jun 15, 2007 3:47 am
by phpdevuk
you'd be wanting to use session variables if you want a value to persist across subsequent page requests.
something like
Code: Select all
$_SESSION['somevar'] = 'some value';
remember you might need to call session_start() at the top of each script tho.
Posted: Fri Jun 15, 2007 4:49 am
by rozvinbm_jp
Code: Select all
//clsrecord.php
<?php
require_once 'globals.inc';
class Record
{
public function Record(){
$GLOBALS['data'] = 0987;
}
public function getGlobalData(){
return $GLOBALS['data'];
}
}
?>
Code: Select all
//page1.php (revised)
<?php
require_once 'clsrecord.php';
require_once 'globals.inc';
echo '<br /><a href="page2.php">page2</a>';
$rec = new Record();
if (isset($GLOBALS['data'])){
$GLOBALS['data'] = 4567;
}
$gdata = $rec->getGlobalData();
echo $gdata;
/* 4567 is the result. Therefore global $data declared in globals.inc
* is accessible even outside the page1.php if the other php file is
* included in page1.php by using include/require.
*
* On the other hand, It will be different if you call other php file
* by using header().
*/
?>
Please confirm this conclusion.
Code: Select all
<span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> (Thank you very much).