Page 1 of 1

passing Variables with require

Posted: Fri Feb 23, 2007 11:14 am
by blast_Stu
Hi there,
I've just started trying to write my login script with Sessions (a different issue!), however I've put the function to check for login in a seperate php file called functions.php

In this file I write to a variable like this:

$loggedIn = true;

The php file is used in, for example, index.php using

require 'functions.php';

I can call the methods of the functions.php file, however index.php does not seem to see the $loggedIn variable.

Are variables not passed in with the require construct? How would I recode such that I can set the variable in the functions.php file and then just access is from the other php files?


Thanks in advance

Regards
Stu

Posted: Fri Feb 23, 2007 11:34 am
by Begby
Are you accessing or setting this variable within a function? If so then the problem is with your variable scope, read up on it at http://www.php.net.

Posted: Fri Feb 23, 2007 11:36 am
by blast_Stu
Begby wrote:Are you accessing or setting this variable within a function? If so then the problem is with your variable scope, read up on it at http://www.php.net.
I thought it could be the variable scope, so I changed the code such that it ends up like:

$loggedIn = false;

function foo(){
//stuff
$loggedIn = true;

}


This should mean that the variable does not fall out of scope should it not?

Will check php.net though, cheers!


Regards
Stu

Posted: Fri Feb 23, 2007 11:39 am
by blast_Stu
and the answer appears to be where you told me that it would be! lol

I guess i should RTFM :)


Regards
Stu