Codeigniter username and access level echo - php

How do I output the username and database value "access" in the view?
It should be on the main page display user name and id of the band, how to implement? The database has a table "Access" at the base of users, you need to get the value and just bring it. Help
Here is my controller and model:
Model:
Class User extends CI_Model
{
function login($username, $password)
{
$this->db->select('id', 'username', 'password', 'access');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}
Controller:
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('Layout');
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->layout->content('home_view', $data);
}
else
{
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}

The problem is that Users should be a Model and Home should be a Controller.
when you get the user name from the table, make a $this->session->set_userdata('username', $username) where $username is the query result.
There is another problem. CI does not use $_SESSION so session_destroy() will not work, use
$this->session->sess_destroy() instead.

Related

How to stop redirecting logged-in user to login page when pressing back button in php codeigniter

I have a login for with username and password , after the user logged in , the user redirect to dashboard , and if the user press the back button of browser it will redirect the user to login page , while i want to redirect the user to dashboard as is already logged-in.i would appreciate if anyone can help me to implement that.
Here is my login. php controller
<?php
class Login extends CI_controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->library('encrypt');
$this->load->model('login_model');
}
function index()
{
//echo CI_VERSION; die;
//echo $this->encrypt->encode('123456');
//echo "<br>".$this->encrypt->decode('ADxTYFI1ATFSYwFn');
//die;
$this->load->view('login');
}
function validation()
{
// print_r($_REQUEST);
// die;
$this->form_validation->set_rules('username', 'Username', 'required|trim' );
$this->form_validation->set_rules('password' , 'Password' , 'required');
if ($this->form_validation->run())
{
$res = $this->login_model->can_login($this->input->post('username'), $this->input->post('password'));
if(is_array($res)){
$stored_password = $res['stored_password'];
$row = $res['row'];
if(password_verify($this->input->post('password'),$stored_password)){
$userID = $row->id;//die;
$this->session->set_userdata('admin',$userID);
redirect('admin/dashboard');
}else{
$this->session->set_flashdata ('message','Invalid Credentils');
redirect('admin/login');
}
}else{
$this->session->set_flashdata ('message','Invalid Credentils');
redirect('admin/login');
}
}
else
{
$this->index();
}
}
}
And here is my login_model.php
<?php
class Login_model extends CI_Model
{
function can_login($username, $password)
{
$this->db->where('username', $username);
$query = $this->db->get('user_login');
if($query->num_rows() > 0)
{
$result = $query->row();
//print_r($result);die;
$stored_password = $result->password;
$return = array('stored_password'=> $stored_password , 'row' => $result);
return $return;
}
else
{
return FALSE;
}
}
And here is my dashboard.php controller
<?php
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!$this->session->userdata('admin'))
redirect('admin');
// else{
// redirect('admin/dashboard');
// }
}
function index()
{
$this->load->view('admin/dashboard');
}
function logout()
{
$this->session->sess_destroy();
redirect('admin');
}
}
Solved,
I solved the problem by adding session in my login.php controller
public function __construct()
{
parent::__construct();
if($this->session->userdata('admin'))
redirect('dashboard');
$this->load->library('form_validation');
$this->load->library('encrypt');
$this->load->model('login_model');
}

UserId is not displaying after successfull login in codeigniter

I am new to codeigniter I Have create a login form and i am able to login succesfully fine and after login i am getting the login username also fine.but when i try to get the userid it is showing empty.I am displaying userid just like username displaying .But it is displaying empty why.Please any help would be appreciated thanks in Advance.
This is my model
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$this->db->select('id','username','password');
$this->db->get('login');
$this->db->where('username',$uname);
$this->db->where('password',$pass);
$query=$this->db->get('login');
if($query->num_rows()==1){
return true;
}else{
return false;
}
}
This is mycontroller
public function verifyuser(){
$name=$this->input->post('username');
$password=$this->input->post('password');
$this->load->model('Login_Model');
if($this->Login_Model->checklogin($name,$password)){
$this->Login_Model->checklogin($name,$password);
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $id);
return true;
}else{
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
}
This view
$id= $this->session->userdata('id');
echo "login id is".$id;
in View page i am accessing data like this but it giving empty for id but it is username fine.
It looks like you are just checking if the username and password match in the database in the checklogin function.
You are not returning the id and storing it in $id variable.
Hence since $id is undefined, no value is printed.
Changes required
Model
Fetch the id from the database and return it instead of a boolean.
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$this->db->select('id','username','password');
$this->db->get('login');
$this->db->where('username',$uname);
$this->db->where('password',$pass);
$query=$this->db->get('login');
if($query->num_rows()==1){
$res = $query->row_array();
return $res['id'];
} else {
return false;
}
}
}
Controller
Fetch the id and use it to set the userdata.
public function verifyuser(){
$name=$this->input->post('username');
$password=$this->input->post('password');
$this->load->model('Login_Model');
$id = $this->Login_Model->checklogin($name,$password);
if($id){
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $id);
return true;
} else {
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
}
Your are getting userid but not storing it in session so you will not get this id
make model code like this
public function checklogin($uname,$password)
{
$this->db->select('user_id','user_email','user_password');
$this->db->from('users');
$this->db->where('user_email',$uname);
$this->db->where('user_password',$password);
$res = $this->db->get()->result_array();
if(!empty($res))
{
return $res[0]['user_id'];
}
else
{
return 0;
}
}
and controller code like this so you will get id
$name=$this->input->post('username');
$password=$this->input->post('password');
$user_id=$this->Login_model->checklogin($name,$password);
if($user_id > 0)
{
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $user_id);
return true;
}
else
{
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
You can make the following Changes in your Model.
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$query = $this->db->select('id','username','password');
->where(['username'=>$uname,'password'=>$pass]);
->get('login');
return $query->row()->id;
}

Display the profile information of a logged in user in Codeigniter?

I want to create a profile view for the user that is currently logged in. I created a session_data array for when you log in that only contains their username. How would I retrieve the other values from the row depending on who is logged in? Do I have to add all that information to the session_data array?
I have the values first_name, last_name, & date_joined that I want to display alongside username.
Login Controller:
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('templates/header');
$this->load->view('login');
$this->load->view('templates/footer');
}
function login_validation()
{
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run())
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('login_model');
if ($this->login_model->can_login($username, $password))
{
$session_data = array(
'username' => $username
);
$this->session->set_userdata($session_data);
redirect('home');
}
else {
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect('login');
}
}
else {
$this->index();
}
}
function enter()
{
if ($this->session->userdata('username') != '') {
echo "Welcome " . $this->session->userdata('username');
echo 'Logout';
}
else {
redirect('login');
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect('login');
}
}
User View:
<h4><?php echo $_SESSION['username']; ?></h4>
You'll need a method in login_model that retrieves the username info based on the username you send to it.
Something like this:
function get_user($username)
{
$this->db->select(*);
$this->db->from('users');
$this->db->where('username', $username);
$query = $this->db->get();
if ($this->db->affected_rows())
{
return $query->row();
}
}
On the other hand, I recommend you to use an existing authentication library.
Ion Auth is pretty good.
Good luck!

Codeigniter can not redirect

i am trying to learn codeigniter, and i start with basic login function. Everything works well, until i want to redirect member to their homepage.
i seems like codeigniter can not redirect to it. It just always stops at this url:
I want this redirect to the site/members_area like mycode. This is my controller code:
class login extends ci_controller{
function index(){
$data['main_content'] = 'login_form';
$this->load->view('includes/template.php', $data);
}
function validate_credentials(){
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query)//user's credentails validated
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else
{
$this->index();
}
}
}
And this is my model code:
class Membership_model extends ci_model{
function validate(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1){
return true;
}
}
}
I already input helper url and form in my autoload, but it still does not work.
Can someone help?
num_rows is a function and should have parentheses like this: $query->num_rows()
You can eliminate the if and simply use
return $query->num_rows() > 0;

CodeIgniter, Call to a member function post() on a non-object

i am new in CodeIgniter (v.2.1.4). I have a problem with getting post from model.
I have a contoller:
class Login extends CI_Controller{
public function index(){
$data['main_view'] = 'login_form';
$this->load->view('login/include/template', $data);
}
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
if($this->users_model->validate_login($username, $password)){
//valid user
}
else{
//not vlaid user
}
}
else{
$this->index();
}
}
}
and a model:
class Users_model extends CI_Model{
function __construct() {
parent::__construct();
}
function validate_login(){
$username = $this->input->post('user');
$password = md5($this->input->post('pass'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
else return FALSE;
}
}
When send via form (post) valid pair (username and password) nothing happens, but in apache2 log appears this:
PHP Fatal error: Call to a member function where() on a non-object in /var/www/ci/application/models/users_model.php
What is wrong?
Add a constructor to the Login class
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
Do you have database loaded? You need to load the database first.
You can either add it to /config/autoload.php to autoload database function:
$autoload['libraries'] = array('database');
Or call on demand like this:
$this->load->database();
More details here
Do Not Use post in model instead use this in controller like this
controller login.php
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
$post = $this->input->post();
$data['username'] = $post['username'];
$data['password'] = md5($post['password']);
if($this->users_model->validate_login($data)){
//valid user
}else{
//not vlaid user
}
}
else{
$this->index();
}
}
Model
function validate_login($where){
$this->db->where($where);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
return FALSE;
}
This should work. Remember calling post somehow doesn't work in model. I dont exactly know they reason.

Categories