Jump to content

PHP OOP AND SESSIONS


jbwebdesign

Recommended Posts

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 by jbwebdesign
Link to comment
Share on other sites

yes i am using session_start() at the top of each page.....i found my problem! :D

 

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 by jbwebdesign
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...