[eluser]Cassio Zen[/eluser]
Hi, sorry for the stupid question but i´m new to PHP and CodeIgniter.
I´m trying to include a simple user level in the Simple Login Library.
Here´s the Simplelogin.php code:
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Simplelogin
{
var $CI;
var $user_table = 'users';
function Simplelogin()
{
// get_instance does not work well in PHP 4
// you end up with two instances
// of the CI object and missing data
// when you call get_instance in the constructor
//$this->CI =& get_instance();
}
function create($user = '', $password = '', $usrlevel = '', $auto_login = true) {
//Put here for PHP 4 users
$this->CI =& get_instance();
//Make sure account info was sent
if($user == '' OR $password == '' OR $usrlevel == '') {
return false;
}
//Check against user table
$this->CI->db->where('username', $user);
$query = $this->CI->db->getwhere($this->user_table);
if ($query->num_rows() > 0) {
//username already exists
return false;
} else {
//Encrypt password
$password = md5($password);
//Insert account into the database
$data = array(
'username' => $user,
'password' => $password,
'usrlevel' => $usrlevel
);
$this->CI->db->set($data);
if(!$this->CI->db->insert($this->user_table)) {
//There was a problem!
return false;
}
$user_id = $this->CI->db->insert_id();
//Automatically login to created account
if($auto_login) {
//Destroy old session
$this->CI->session->sess_destroy();
//Create a fresh, brand new session
$this->CI->session->sess_create();
//Set session data
$this->CI->session->set_userdata(array('id' => $user_id,'username' => $user,'usrlevel' => $usrlevel));
//Set logged_in to true
$this->CI->session->set_userdata(array('logged_in' => true));
}
//Login was successful
return true;
}
}
function delete($user_id) {
//Put here for PHP 4 users
$this->CI =& get_instance();
if(!is_numeric($user_id)) {
//There was a problem
return false;
}
if($this->CI->db->delete($this->user_table, array('id' => $user_id))) {
//Database call was successful, user is deleted
return true;
} else {
//There was a problem
return false;
}
}
function login($user = '', $password = '') {
//Put here for PHP 4 users
$this->CI =& get_instance();
//Make sure login info was sent
if($user == '' OR $password == '') {
return false;
}
//Check if already logged in
if($this->CI->session->userdata('username') == $user) {
//User is already logged in.
return false;
}
//Check against user table
$this->CI->db->where('username', $user);
$query = $this->CI->db->getwhere($this->user_table);
if ($query->num_rows() > 0) {
$row = $query->row_array();
//Check against password
if(md5($password) != $row['password']) {
return false;
}
//Destroy old session
$this->CI->session->sess_destroy();
//Create a fresh, brand new session
$this->CI->session->sess_create();
//Remove the password field
unset($row['password']);
//Set session data
$this->CI->session->set_userdata($row);
//Set logged_in to true
$this->CI->session->set_userdata(array('logged_in' => true));
//Login was successful
return true;
} else {
//No database result found
return false;
}
}
function logout() {
//Put here for PHP 4 users
$this->CI =& get_instance();
//Destroy session
$this->CI->session->sess_destroy();
}
}
?>
[eluser]Cassio Zen[/eluser]
And here is the example.php controller:
Code: <?php
class Example extends Controller {
function Example()
{
parent::Controller();
}
function index()
{
//This assumes you used the sample MySQL table
$user_table = 'users';
//Load the URL helper
$this->load->helper('url');
//BOF Status Info
echo '<div id="status">';
echo '<h3>User Status</h3>';
if($this->session->userdata('logged_in')) {
echo 'User logged in as ' . $this->session->userdata('username');
} else {
echo 'User not logged in';
}
echo '</div>';
echo '<hr />';
//EOF Status Info
//BOF Create user
echo '<div id="create">';
echo '<h3>Create A User</h3>';
echo '<form action="' . site_url('/example/create/') . '" method="post">';
echo '<label for="create_username">Username:</label>';
echo '<input type="text" id="create_username" name="create_username" value="" /><br />';
echo '<label for="create_password">Password:</label>';
echo '<input type="password" id="create_password" name="create_password" value="" /><br />';
echo '<label for="create_usrlevel">User Level:</label>';
echo '<input type="text" id="create_usrlevel" name="create_usrlevel" value="" /><br />';
echo '<input type="submit" id="create" name="create" value="Create" />';
echo '</form>';
echo '</div>';
echo '<hr />';
//EOF Create user
//BOF Login user
if(!$this->session->userdata('logged_in')) {
echo '<div id="login">';
echo '<h3>Login</h3>';
echo '<form action="' . site_url('/example/login/') . '" method="post">';
echo '<label for="login_username">Username:</label>';
echo '<input type="text" id="login_username" name="login_username" value="" /><br />';
echo '<label for="login_password">Password:</label>';
echo '<input type="password" id="login_password" name="login_password" value="" /><br />';
echo '<input type="submit" id="login" name="login" value="Login" />';
echo '</form>';
echo '</div>';
echo '<hr />';
} else {
echo '<div id="logut">';
echo '<h3>Logut</h3>';
echo '<a href="' . site_url('/example/logout/') . '">Click here to logout.</a>';
echo '</div>';
echo '<hr />';
}
//EOF Login user
//BOF User table
......
//EOF User table
}
function create()
{
//Load
$this->load->helper('url');
$this->load->library('validation');
//Check incoming variables
$rules['create_username'] = "required|min_length[4]|max_length[32]|alpha_dash";
$rules['create_password'] = "required|min_length[4]|max_length[32]|alpha_dash";
$rules['create_usrlevel'] = "required|min_length[4]|max_length[32]|alpha_dash";
$this->validation->set_rules($rules);
$fields['create_username'] = 'Username';
$fields['create_password'] = 'Password';
$fields['create_usrlevel'] = 'Usrlevel';
$this->validation->set_fields($fields);
if ($this->validation->run() == false) {
redirect('/example/');
} else {
//Create account
if($this->simplelogin->create($this->input->post('create_username'), $this->input->post('create_password'), $this->input->post('create_usrlevel'))) {
redirect('/example/');
} else {
redirect('/example/');
}
}
}
function delete($user_id)
{
//Load
$this->load->helper('url');
if($this->simplelogin->delete($user_id)) {
redirect('/example/');
} else {
redirect('/example/');
}
}
function login()
{
//Load
$this->load->helper('url');
$this->load->library('validation');
//Check incoming variables
$rules['login_username'] = "required|min_length[4]|max_length[32]|alpha_dash";
$rules['login_password'] = "required|min_length[4]|max_length[32]|alpha_dash";
$this->validation->set_rules($rules);
$fields['login_username'] = 'Username';
$fields['login_password'] = 'Password';
$this->validation->set_fields($fields);
if ($this->validation->run() == false) {
redirect('/example/');
} else {
//Create account
if($this->simplelogin->login($this->input->post('login_username'), $this->input->post('login_password'))) {
redirect('/example/');
} else {
redirect('/example/');
}
}
}
function logout()
{
//Load
$this->load->helper('url');
//Logout
$this->simplelogin->logout();
redirect('/example/');
}
}
?>
But it´s not working! Could someone help me find out what´s wrong?
[eluser]Crimp[/eluser]
Not working is a little vague. To see what's going on with your session you can use session->all_userdata() to debug. That will tell you if the level is passed around correctly. Should not be too hard to find out where something is missing.
[eluser]Cassio Zen[/eluser]
Sorry for being vague, bu it doesen´t show any errors. It simply stops working.
All I did was putting the usrlevel information in the library.
I´ll try session->all_userdata(), thanks.
[eluser]jvittetoe[/eluser]
im building a similar application, but im not suing simple login, im trying to create my own login system. but im pretty new to ci and php in general. basically my app works as this,
home controller ->
submit login form ->
login model queries db ->
return to controller ->
if login true ->
CREATE SESSION HERE???? ->
then load view file
is this a proper structure or am i missing pretty bad?
[eluser]jvittetoe[/eluser]
n/m
[eluser]Cassio Zen[/eluser]
Solved, thanks. I wrote the whole thing again, made some modifications and it worked.
The Simplelogin.php code is now like this:
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Simplelogin Class
*
* Makes authentication simple
*
* Simplelogin is released to the public domain
* (use it however you want to)
*
* Simplelogin expects this database setup
* (if you are not using this setup you may
* need to do some tweaking)
*
#This is for a MySQL table
CREATE TABLE `users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 64 ) NOT NULL ,
`password` VARCHAR( 64 ) NOT NULL ,
UNIQUE (
`username`
)
);
*
*/
class Simplelogin
{
var $CI;
var $user_table = 'users';
function Simplelogin()
{
// get_instance does not work well in PHP 4
// you end up with two instances
// of the CI object and missing data
// when you call get_instance in the constructor
//$this->CI =& get_instance();
}
/**
* Create a user account
*
* @access public
* @param string
* @param string
* @param bool
* @return bool
*/
function create($user = '', $level='', $password = '', $auto_login = true) {
//Put here for PHP 4 users
$this->CI =& get_instance();
//Make sure account info was sent
if($user == '' OR $password == '') {
return false;
}
//Check against user table
$this->CI->db->where('username', $user);
$query = $this->CI->db->getwhere($this->user_table);
if ($query->num_rows() > 0) {
//username already exists
return false;
} else {
//Encrypt password
$password = md5($password);
//Insert account into the database
$data = array(
'username' => $user,
'level' => $level,
'password' => $password
);
$this->CI->db->set($data);
if(!$this->CI->db->insert($this->user_table)) {
//There was a problem!
return false;
}
$user_id = $this->CI->db->insert_id();
//Automatically login to created account
if($auto_login) {
//Destroy old session
$this->CI->session->sess_destroy();
//Create a fresh, brand new session
$this->CI->session->sess_create();
//Set session data
$this->CI->session->set_userdata(array('id' => $user_id,'username' => $user, 'level' => $level));
//Set logged_in to true
$this->CI->session->set_userdata(array('logged_in' => true));
}
//Login was successful
return true;
}
}
/**
* Delete user
*
* @access public
* @param integer
* @return bool
*/
function delete($user_id) {
//Put here for PHP 4 users
$this->CI =& get_instance();
if(!is_numeric($user_id)) {
//There was a problem
return false;
}
if($this->CI->db->delete($this->user_table, array('id' => $user_id))) {
//Database call was successful, user is deleted
return true;
} else {
//There was a problem
return false;
}
}
/**
* Login and sets session variables
*
* @access public
* @param string
* @param string
* @return bool
*/
function login($user = '', $password = '') {
//Put here for PHP 4 users
$this->CI =& get_instance();
//Make sure login info was sent
if($user == '' OR $password == '') {
return false;
}
//Check if already logged in
if($this->CI->session->userdata('username') == $user) {
//User is already logged in.
return false;
}
//Check against user table
$this->CI->db->where('username', $user);
$query = $this->CI->db->getwhere($this->user_table);
if ($query->num_rows() > 0) {
$row = $query->row_array();
//Check against password
if(md5($password) != $row['password']) {
return false;
}
//Destroy old session
$this->CI->session->sess_destroy();
//Create a fresh, brand new session
$this->CI->session->sess_create();
//Remove the password field
unset($row['password']);
//Set session data
$this->CI->session->set_userdata($row);
//Set logged_in to true
$this->CI->session->set_userdata(array('logged_in' => true));
//Login was successful
return true;
} else {
//No database result found
return false;
}
}
/**
* Logout user
*
* @access public
* @return void
*/
function logout() {
//Put here for PHP 4 users
$this->CI =& get_instance();
//Destroy session
$this->CI->session->sess_destroy();
}
}
?>
[eluser]Cassio Zen[/eluser]
Example.php Controller
Code: <?php
class Example extends Controller {
function Example()
{
parent::Controller();
}
function index()
{
$user_table = 'users';
$this->load->helper('url');
//BOF Status Info
echo '<div id="status">';
echo '<h3>User Status</h3>';
if($this->session->userdata('logged_in')) {
echo 'User logged in as ' . $this->session->userdata('username');
echo 'Access Level: ' . $this->session->userdata('level');
} else {
echo 'User not logged in';
}
echo '</div>';
echo '<hr />';
//EOF Status Info
//BOF Create user
echo '<div id="create">';
echo '<h3>Create A User</h3>';
echo '<form action="' . site_url('/example/create/') . '" method="post">';
echo '<label for="create_username">Username:</label>';
echo '<input type="text" id="create_username" name="create_username" value="" /><br />';
echo '<label for="create_level">User level:</label>';
echo '<input type="text" id="create_level" name="create_level" value="" /><br />';
echo '<label for="create_password">Password:</label>';
echo '<input type="password" id="create_password" name="create_password" value="" /><br />';
echo '<input type="submit" id="create" name="create" value="Create" />';
echo '</form>';
echo '</div>';
echo '<hr />';
//EOF Create user
//BOF Login user
if(!$this->session->userdata('logged_in')) {
echo '<div id="login">';
echo '<h3>Login</h3>';
echo '<form action="' . site_url('/example/login/') . '" method="post">';
echo '<label for="login_username">Username:</label>';
echo '<input type="text" id="login_username" name="login_username" value="" /><br />';
echo '<label for="login_password">Password:</label>';
echo '<input type="password" id="login_password" name="login_password" value="" /><br />';
echo '<input type="submit" id="login" name="login" value="Login" />';
echo '</form>';
echo '</div>';
echo '<hr />';
} else {
echo '<div id="logut">';
echo '<h3>Logut</h3>';
echo '<a href="' . site_url('/example/logout/') . '">Click here to logout.</a>';
echo '</div>';
echo '<hr />';
}
//EOF Login user
}
function create()
{
$this->load->helper('url');
$this->load->library('validation');
$rules['create_username'] = "required|min_length[4]|max_length[32]|alpha_dash";
$rules['create_level'] = "required";
$rules['create_password'] = "required|min_length[4]|max_length[32]|alpha_dash";
$this->validation->set_rules($rules);
$fields['create_username'] = 'Username';
$fields['create_level'] = 'Level';
$fields['create_password'] = 'Password';
$this->validation->set_fields($fields);
if ($this->validation->run() == false) {
redirect('/merda/');
} else {
if($this->simplelogin->create($this->input->post('create_username'), $this->input->post('create_level'), $this->input->post('create_password'))) {
redirect('/example/');
} else {
redirect('/example/');
}
}
}
function delete($user_id)
{
$this->load->helper('url');
if($this->simplelogin->delete($user_id)) {
redirect('/example/');
} else {
redirect('/example/');
}
}
function login()
{
//Load
$this->load->helper('url');
$this->load->library('validation');
$rules['login_username'] = "required|min_length[4]|max_length[32]|alpha_dash";
$rules['login_password'] = "required|min_length[4]|max_length[32]|alpha_dash";
$this->validation->set_rules($rules);
$fields['login_username'] = 'Username';
$fields['login_password'] = 'Password';
$this->validation->set_fields($fields);
if ($this->validation->run() == false) {
redirect('/example/');
} else {
if($this->simplelogin->login($this->input->post('login_username'), $this->input->post('login_password'))) {
redirect('/example/');
} else {
redirect('/example/');
}
}
}
function logout()
{
//Load
$this->load->helper('url');
//Logout
$this->simplelogin->logout();
redirect('/example/');
}
}
?>
[eluser]jvittetoe[/eluser]
can i ask why you are echoing everything out from your controller instead of loading a view file? is there a benefit to the way you are doing things?
[eluser]Cassio Zen[/eluser]
Oh, this is not the way i'm using it. As the original author said, It's simply controller examples of how to work with the library (you should never use controllers like this in a real project) - these files are examples and are not necessary.
|