Hey,
I have successfully managed to get my login code working (Recently switched from php4 to php5, as my company wanted to move forward :))
Anyway, I have one final problem. I am enabling normal users to login which is fine, but i need to check for admin users. So the way i am doing this is, i have a members table and i will have a type column saying “admin” or “normal” depending on the user privilege.
Anyway in php4 i did this:
if($row['type'] == "admin")
{
$_SESSION['normalUser'] = $row['username'];
}else{
$_SESSION['adminUser'] = $row['username'];
}
Or something like that anyway…
Now i have this code, apologies in advance as there is write a bit of code
Models/database.class.php
<?php
require_once('Person.class.php');
class Database {
protected $mysqli;
public function sanitise($data) { // Escapes parameters before sending SQL query
foreach($data as $key => $value){
$data[$key] = $this->mysqli->real_escape_string($value);
}
return $data;
}
public function __construct($host, $username, $password, $dbname) {
$this->mysqli = new mysqli($host, $username, $password, $dbname);
if ($this->mysqli->errno){
echo 'Unable to connect'.$this->mysqli->error;
exit();
}
}
public function login($username, $password) { // Login
$data = $this->sanitise(array('email'=>$username, 'password'=>$password));
$sql = 'SELECT * FROM hussaini_members
WHERE email = \\''.$data['email'].'\\' AND password = \\''.$data['password'].'\\'';
$results = $this->mysqli->query($sql);
$personArray = array();
while($row = $results->fetch_array(MYSQLI_ASSOC)) {
$personArray[] = new Person($row);
}
return $personArray;
}
public function __destruct() {
$this->mysqli->close();
}
}
Person.class is just a class which uses getter’s to retrieve user details. Anyway in my login.php i have this code:
require_once('Models/Database.class.php');
$database = new Database('localhost', '****', '', '****');
session_start();
$message = "";
if(isset($_POST['submit']))
{
$personArray = $database->login($_POST['email'], $_POST['password']);
if(sizeof($personArray) == 1) {
$_SESSION['normalUser'] = $personArray[0];
$_SESSION['loggedin'] = true;
$message = "";
header ("Location: homepage");
}
else {
$message = "<span style=\\"color:#00cc00\\">Invalid user credentials.</span>";
}
}
Now how can i check to see if the type row is “admin” or “normal” and THEN set the session to either “normalUser” OR “adminUser” ?
I would really appreciate if anyone can help me out with this…
Thanks