Global vars accessing in other pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Global vars accessing in other pages

Post 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.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Re: Global vars accessing in other pages

Post 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';
?>
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post 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.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post 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.
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post 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&#39;m naughty, are you naughty?'>smurf</span> (Thank you very much).
Post Reply