Cobby Posted October 5, 2007 Share Posted October 5, 2007 Hi all, I'm developing my own little OOP-based PHP-MySQL Framework. Its derived from the MVC framework. I'm making a login script that uses sessions. I don't think I have ever used sessions in a class before, but I didn't think I would need to do anything fancy to use them as they are a global. In short, on the index page, the first thing it does is call the checkLogIn() function from the login class to see whether there logged in or not. It simply checks the values of $_SESSION['auth'] and returns true or false depending on that value. But as soon as I load the index page, I get an error saying undefined index: auth. The session is started. Thanks, Cobby Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/ Share on other sites More sharing options...
teng84 Posted October 5, 2007 Share Posted October 5, 2007 then its not defined lol please post your code. like you said session is global so i guess theres no issue with th oo better to post your code! Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-362977 Share on other sites More sharing options...
Cobby Posted October 6, 2007 Author Share Posted October 6, 2007 I know it undefined, I am asking why. login.class.php <?php class login { var $reg = array(); function login($reg){ $this->reg = $reg; } function checkLogIn($type=NULL){ if($type == 'login'){ if($_SESSION['auth'] == 'yes'){ $this->reg['redirect']->redir(site_url.'admin/index', 0); } }else{ if($_SESSION['auth'] != 'yes'){ $this->reg['redirect']->redir(site_url.'admin/login', 0); } } } function log_in($username, $password){ if(empty($username)){ $error[0] = "You haven't entered a username"; } if(empty($password)){ $error[1] = "You haven't entered a password"; } $encrypt = $this->reg['crypt']->encrypt($password); $sql = "SELECT * FROM `".tbl_prefix."users` WHERE `username` = '".mysql_escape_string($username)."' AND `password` = '".mysql_escape_string($encrypt)."'"; if($this->reg['db']->dbNumRows($sql) == 1){ $_SESSION['auth'] = 'yes'; $user = $this->reg['db']->dbFetchArray($sql); foreach($user as $key => $val){ $_SESSION['user'][$key] = $val; } $this->reg['smarty']->assign('userData', $user); return 'true'; }else{ $error[2] = "You have provided invalid login credentials"; } if(isset($error)){ return $error; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363024 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 inside your function where you use that session print it like print_r($_SESSION); to check whether your session is really set Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363037 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 if that is not set then check whether you have session start on that page or where the page is included Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363039 Share on other sites More sharing options...
Cobby Posted October 6, 2007 Author Share Posted October 6, 2007 The session is start otherwise it would be throwing a different error. I did print_r($_SESSION) and I get Array() on the screen. Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363046 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 so session is not register The session is start otherwise it would be throwing a different error. no other error that undefined index can you post how you include that class together with your session start Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363048 Share on other sites More sharing options...
roopurt18 Posted October 6, 2007 Share Posted October 6, 2007 When you have error reporting set to report everything, the interpreter will raise errors whenever you use a variable or array index that has not yet been defined. <?php session_start(); if($_SESSION['auth']){ echo "logged in"; } ?> On the first hit to the page, session_start() will create an empty session, meaning that $_SESSION is empty and the index 'auth' in $_SESSION doesn't exist. In that case, you do one of two things: 1) Change the level of your error reporting. 2) Change the comparison <?php session_start(); if(isset($_SESSION['auth']) && $_SESSION['auth']){ echo "logged in"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363067 Share on other sites More sharing options...
Cobby Posted October 6, 2007 Author Share Posted October 6, 2007 Theres a lot of code here: config.php <?php // set database information $settings['server'] = 'localhost'; $settings['username'] = 'cobby'; $settings['password'] = '*******'; // intentionally left blank for the purpose of this post $settings['db'] = 'framework'; $settings['tblpre'] = 'spf_'; $settings['urlformat'] = 'index.php?file='; if(!defined('tbl_prefix')){ define('tbl_prefix', $settings['tblpre']); } if(!defined('urlformat')){ define('urlformat', $settings['urlformat']); } $settings['adminemail'] = 'admin@localhost'; ?> include.php <?php // Set error reporting error_reporting(E_ALL); // get url function function url(){ // get server protocol $sp = $_SERVER['SERVER_PROTOCOL']; $sp = explode ('/', $sp); $protocol = strtolower($sp[0]); // add protocol $url = $protocol."://".$_SERVER['SERVER_NAME']; // get the script path $path = $_SERVER['PHP_SELF']; // separate path by / $path = explode('/', $path); $final = ''; // join parts, start at 1 to remove first blank and end 2 early to skip includes and filename name for($i=1; $i<count($path)-1; $i++){ $final .= "/".$path[$i]; } // add trailing / $final .= "/"; // join parts $ret = $url.$final; return $ret; } // Set Constants if(!defined('DIRSEP')){ define('DIRSEP', DIRECTORY_SEPARATOR); } $site_path = realpath(dirname(__FILE__) . DIRSEP . '..' . DIRSEP) . DIRSEP; if(!defined('site_path')){ define('site_path', $site_path); } if(!defined('site_url')){ define('site_url', url()); } include site_path.DIRSEP.'classes'.DIRSEP.'reg.class.php'; $reg = new reg; $scandir = scandir(site_path."classes\\"); foreach($scandir as $val){ if(preg_match("+.class.php+", $val)){ $val = substr($val, 0 ,-10); if(($val != 'Smarty_Compiler') && ($val != 'Config_File') && ($val != 'reg')){ include_once site_path."classes\\".$val.".class.php"; $reg[$val] = new $val($reg); } } } include site_path.DIRSEP.'includes'.DIRSEP.'config.php'; $reg['smarty']->assign('settings', $settings); $reg['smarty']->assign('siteurl', site_url); ?> index.php <?php // include includes.php include 'includes/include.php'; // connect to database $reg['db']->dbConnect($settings); // run request handler $reg['handler']->page(); // close database connection $reg['db']->dbClose(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363076 Share on other sites More sharing options...
Cobby Posted October 6, 2007 Author Share Posted October 6, 2007 When you have error reporting set to report everything, the interpreter will raise errors whenever you use a variable or array index that has not yet been defined. You hit the jack pot mate, just removed error_reporting() and it all works fine now. Thanks, Cobby Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363080 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 yah but i believe your problem with setting the session is not solve Quote Link to comment https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363084 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.