user.class.php
Code: Select all
<?php
// User class
class user{
private $id = 0;
private $pass = "";
private $table;
private $id_field;
private $password_field;
private $db;
function __construct($db, $table = "user_table", $id_field = "id", $password_field = "password")
{
$this->table = $table;
$this->id_field = $id_field;
$this->password_field = $password_field;
$this->db = $db;
}
// SET USER ID AND PASS
function load_user($id, $pass)
{
$this->id = $id;
$this->pass = $pass;
}
// USER LOGIN
function user_login($data, $stay_logged = false)
{
//global $this->db;
$last = end($data);
$query = "SELECT `{$this->id_field}`, `{$this->password_field}` FROM `{$this->table}` WHERE ";
foreach($data as $k => $v)
{
$query .= "`{$k}`='{$v}'";
if($v != $last)
{
$query .= " AND ";
}
}
$query .= " LIMIT 1";
$sql = $this->db->query($query);
if($sql->num_rows > 0)
{
$row = $sql->fetch_object();
$this->load_user($row->{$this->id_field}, $row->{$this->password_field});
return true;
}
else
{
return false;
}
}
//FETCH SPECIFIED USER DATA
function fetch_user_data($data)
{
//global $this->db;
$last = end($data);
$query = "SELECT ";
foreach($data as $v)
{
$query .= "`{$v}`";
if($v != $last)
{
$query .= ", ";
}
}
$query .= " FROM `{$this->table}` WHERE `{$this->id_field}`={$this->id} AND `{$this->password_field}`='{$this->pass}' LIMIT 1";
$sql = $this->db->query($query);
return $sql->fetch_object();
}
// AUTHENTICATE USER WITH ID AND PASSWORD
function auth()
{
//global $this->db;
$query = "SELECT `{$this->id_field}` FROM `{$this->table}` WHERE `{$this->id_field}`={$this->id} AND `{$this->password_field}`='{$this->pass}' LIMIT 1";
$sql = $this->db->query($query);
if($sql->num_rows > 0)
{
return true;
}
else
{
return false;
}
}
// OUTPUT LOGIN FORM
function load_form($template)
{
echo file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/templates/{$template}");
}
// REGISTER NEW USER
function register_user($data)
{
//global $this->db;
$query = "INSERT INTO `{$this->table}` SET ";
$last = end($data);
foreach($data as $k => $v)
{
$query .= "`{$k}`='{$v}'";
if($v != $last)
{
$query .= ", ";
}
}
$this->db->query($query);
}
// AUTHENTICATE DATA
function auth_data($data)
{
//global $this->db;
$query = "SELECT `{$this->id_field}` FROM `{$this->table}` WHERE ";
$last = end($data);
foreach($data as $k => $v)
{
$query .= "`{$k}`='{$v}'";
if($v != $last)
{
$query .= " AND ";
}
}
$query .= " LIMIT 1";
$sql = $this->db->query($query);
if($sql->num_rows > 0)
{
return false;
}
else
{
return true;
}
}
}
?>
the class is designed to work with mysqli objects (so knowing how to initiate a mysqli object instance helps).
The construct has 1 required attribute and 3 optional. The 1st one is the mysqli database connection variable. This is needed
The second is the name of the user table which defaults to user_table.
The third is the user id field. This defaults to id.
the fourth is the password field, which defaults to password.
once the new class instance is initiated, you will need to load certain information into the class. The information required is the user id and the user password. You can keep these set in sessions.
Code: Select all
$user->load_user($_SESSION['uid'], $_SESSION['upass']);
Code: Select all
$auth = $user->auth();
// or
if($user->auth() === true)
But it doesn't stop there!
You can also select certain information from the user database by using an array and the fetch_user_data function
Code: Select all
$data = array('name', 'age', 'job');
$info = $user->fetch_user_data($data);
//call information like so
echo $info->name;
if($info->age > 30)
echo 'My job is ', $info->job;
So it loads a user, authenticates a user and calls user information... What about logging in a user? Yes it does that too...
Code: Select all
if(isset($_POST['submit']))
{
$data = array(
"email" => $_POST['email'],
"password" => md5($_POST['password'])
);
$user->user_login($data);
}
it even registers users.....
in the same fashion as logging in, you set the fields and data into an array which you want to put into the database.
Code: Select all
$data = array(
"username" => $_POST['username'],
"password" => $_POST['password'],
"age" => $_POST['age']
);
$user->register_user($data);
And finally, it checks to see if data is already input into the table.
Code: Select all
$data = array(
"username" => $_POST['username']
);
if($user->auth_data($data) === true)
{
// Data already exists in the table!
}
Well i think that is all lol. Hope its of some use to someone