jbwebdesign Posted April 30, 2010 Report Share Posted April 30, 2010 (edited) Hey everyone, i am working on a class for members section and i am having trouble with the sessions. They don't seem to be working because it's not storing the value of 1 in $_SESSION['res'] Can someone please help me out and let me know what i am doing wrong? here is the class: class member{ var $host; var $dbc; var $sel; var $db_user; var $db_pass; var $sql; var $res; var $user; var $pass; var $status; var $row; var $sql2; function __construct(){ $this->dbc = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die(mysql_error()); $this->sel = mysql_select_db(DB_DATABASE) or die(mysql_error()); } function login(){ if($_GET['login'] == "yes"){ $this->dbc = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die(mysql_error()); $this->sel = mysql_select_db(DB_DATABASE) or die(mysql_error()); //--build query $this->sql = mysql_query("SELECT count(id) FROM " . TABLE_ADMIN . " WHERE username='" . user . "' AND password='" . pass . "'"); //Hacker SQL injection Proof $this->user = mysql_escape_string(user); $this->pass = mysql_escape_string(pass); $this->res = mysql_result($this->sql,0); if($this->res!=1){ $this->status = '<div class="error"><span>Invalid Login!</span> <p> you have entered wrong username or password!</p></div>'; } else{ $_SESSION['username'] = $this->user; $_SESSION['password'] = $this->pass; $_SESSION['result'] = $this->res; header('Location:index.php'); } } } //This function will logout user function logout(){ if(isset($_GET['logout'])){ $_SESSION = array(); if($_COOKIE[session_name()]){ setcookie(session_name(),'', time()-4200,'/'); } session_destroy(); header('Location: login.php'); } } //This function will check if user is logged in function login_check(){ //CHECK LOGIN if($this->res!=1){ header('Location: login.php'); } } }//End member class Edited April 30, 2010 by jbwebdesign Quote Link to comment Share on other sites More sharing options...
falkencreative Posted April 30, 2010 Report Share Posted April 30, 2010 Are you using session_start() on each page where you want to access the session? http://php.net/manual/en/function.session-start.php I'm not seeing that anywhere in your code. Quote Link to comment Share on other sites More sharing options...
jbwebdesign Posted April 30, 2010 Author Report Share Posted April 30, 2010 (edited) yes i am using session_start() at the top of each page.....i found my problem! it turns out that when i put print_r($_SESSION); the sessions were working fine. the problem was that //This function will check if user is logged in function login_check(){ //CHECK LOGIN if($this->res!=1){ header('Location: login.php'); } needed me to use the actual $_SESSION['result'] instead of $this->res so the working code is: //This function will check if user is logged in function login_check(){ //CHECK LOGIN if($_SESSION['result']!=1){ header('Location: login.php'); } Edited April 30, 2010 by jbwebdesign Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.