[PHP] login with sessions small problem...

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
hey, i'm trying to write a function that checks a username and password (on submission) against a database and then sets a global session variable as a user type for authentication on other pages. my function is as follows:

Code:
function userCheck()
{
	
	$this->buildArray("SELECT * FROM member WHERE username='$_REQUEST[username]' AND password='$_REQUEST[password]'");
		
	if ($this->arrayCount !== 1)
	{
		header("Location: error.php");
	}
	else
	{
		print_r($this->blog);
		$_SESSION['usertype'] = $this->blog['user_type'];
		$_SESSION['username'] = $this->blog['username'];
		
		echo "$_SESSION[usertype]";
	}
}

i've got a few things in there because i'm trying to get it to work at the moment. buildArray() queries the database and returns the array $this->blog...which looks something like this:

Code:
Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Jasper [username] => Jasper [2] => DELETED** [password] => DELETED** [3] => admin [user_type] => admin ) )

however, my session variables aren't setting, and it's really starting to confuse me. upon echoing the $_SESSION[usertype], it's not doing anything. i'm thinking this is because i've set the variable incorrectly, but i can't see how. can someone please help me?
 
na, that was the first thing i checked!

when setting the sessions i should've been using

$_SESSION['usertype'] = $this->blog[0]['user_type'];
$_SESSION['username'] = $this->blog[0]['username'];

derr! :o
 
Dfhaii said:
Amazed I'm the first to say it but you should make those $_REQUEST variables safe before putting them in a query.

excellent point, thanks for that. i'll do that right now :)

Dj_Jestar said:
Indeed, and tight coupling globals into a class/object is a booboo

sorry, could you be more precise - what have i done and what does it need to change to? i've only just started using objects, and i'm still getting to grips with it :o
 
Back
Top Bottom