dumdumsareyum Posted April 13, 2009 Share Posted April 13, 2009 I'm working on a login class. I am trying to send a file with settings to the constructor function, like this: public function __construct($settings_file) { session_start(); $this->connectToMySQL(); //$this->checkUserIP(); if(!isset($_SESSION['auth'])){ $_SESSION['auth'] = 0; if(!include("$settings_file")) throw new Exception("Unable to find settings"); } } The settings file contains the name of the database table etc. in form of $this->dbName = "dbName"; Before I was doing it this way, all my variables were just declared inside the class, but then I thought it would be nice to use the same class for regular accounts and administrator accounts and just include whichever file I wanted to change the db table and other settings for an admin. However, now it is not finding those variable values from the external file, and it doesn't throw the exception I have either shown here in the constructor, my queries are just returning exceptions and when I echo the variables in question they are null.....So, am I going about this wrong or what? Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/ Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 You need to global variables being used within a function that are defined outside of the function. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808735 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 if(!include("$settings_file")) Should be: if(!include($settings_file)) No quotes... it's a variable, not a string. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808738 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 You need to global variables being used within a function that are defined outside of the function. The value is being passed to the constructor. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808740 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 One more thing... move session_start out of the constructor. SESSION is a global array. I would never start my session inside a class, but that's just me. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808741 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Oh yeah...my mistake. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808742 Share on other sites More sharing options...
dumdumsareyum Posted April 13, 2009 Author Share Posted April 13, 2009 yeah I had $session_file without the quotes first, I was just trying different things because I couldn't figure out why it didn't seem to be including the file Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808751 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 So if you take the quotes out, does it work? Can you echo the contents of what is passed to see if it is actually getting anything? Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808758 Share on other sites More sharing options...
dumdumsareyum Posted April 13, 2009 Author Share Posted April 13, 2009 Here is what the stuff in the file looks like /*DABATASE TABLE*/ $this->accountTable = "clients"; /*path to redirect successful login to*/ $this->login_success_path = "client.php"; /*path to redirect a failed login to*/ $this->login_failed_path = "client.php"; /*path to page to process email verifications, using get input*/ $this->verification_path = "verifyAcct.php"; /*Do email accounts have to be verified before logging in? 0 = accounts do not need to be verified, 1 = accounts do need to be verified*/ $this->using_verified = 1; /*Method of verification -- administrator confirmed = 0, user confirmed = 1 Administrator emails account details to admin and they log in and approve/disprove. User confirmed emails user and has them confirm receipt of email*/ $this->verification_method = 0; which before i moved it just looked like private $accountTable = "clients.php"; and now I declare private $accountTable; in the class also, I tried echoing something, anything, from the constructor, should I be able to do that? Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808759 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 Well, you don't have to echo the variable per-se... just set it to a class variable and then: print_r(get_object_vars($class), true); After the class is initiated. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808763 Share on other sites More sharing options...
dumdumsareyum Posted April 13, 2009 Author Share Posted April 13, 2009 No, it doesn't work without the quotes either unfortunately. The first error it gets to is where I am checking to see if the username exists. when I echo this query I get SELECT * from WHERE username = '' which is very strange to me too because the value that should be in the username is also now not appearing, and it is set from that function ($this->username = $username, and $username is a function parameter), and has nothing to do with the variables I am trying to include in the constructor but for some reason is now not working either. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808766 Share on other sites More sharing options...
dumdumsareyum Posted April 13, 2009 Author Share Posted April 13, 2009 whoops....clumsy me. I accidentally nested the include in the if statement that checks the Session authorization. No wonder nothing was happening! Thanks for all ur suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/153880-solved-include-in-construct-function/#findComment-808795 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.