Hi i m new to ci just working on the login & logout. When i try to logout it
shows url is not found /loginController/logout
What is the problem ? Is it because of the session or did i miss something?
Here is my controller
<?php
class loginController extends CI_Controller{ /**controller*/
public function index(){
$this->login();
}
public function login(){
$this->load->view('login');
}
public function home(){
if ($this->session->userdata('logged')){
$this->load->view('main');
} else {
redirect('loginController/denied');
}
}
public function denied(){
$this->load->view('denied_page');
}
public function login_validation() /**set rules*/
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username',
'Username', 'required|min_length[3]|max_length[12]');
$this->form_validation->set_rules('password',
'Password', 'required|sha1|callback_password_check');
if ($this->form_validation->run()){ /** form valdation*/
$data = array(
'username' => $this->input ->post('username'),
'logged' => 1
);
$this->session->set_userdata($data);
redirect('main');
} else {
$this->load->view('login');
}
}
public function logout(){ //login
$this->session->sess_destroy();
redirect('login');
}
public function password_check(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('users');
if($this->users->log_in($username, $password)){
return True;
}else{
$this->form_validation->set_message('password_check',
'Incorrect username or password.');
return False;
}
}
}
?>
change the code
public function logout(){ //login
$this->session->sess_destroy();
redirect('login');
}
to
public function logout(){
$this->session->sess_destroy();
redirect('loginController/login');
}
load $this->load->helper('url'); on the index function and change $this->load->helper(array('form', 'url')); to $this->load->helper(array('form')); on your validation function.
when redirecting use base_url()
on function logout change the second line redirect('login'); to redirect(base_url('/'.get_class($this)));
i hope it works and help you, just remember to read this and after logout the login page will be accessed via your index() function.
if that will not work just try to change base_url() to site_url() but you should know what is the differences.
Related
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');
}
How can I create logout button if my login controller with session is like this?
function login_user() {
$user_login = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$data=$this->Infoserbilis_model->login_user($user_login['username'],$user_login['password']);
if($data) {
$session_data['logged_in'] = TRUE;
$this->session->set_userdata($session_data);
//$this->session->set_userdata('logged_in', $session_data);
redirect('Infoserbilis/admin_page', 'refresh');
} else {
echo '<script>alert("Invalid Username or Password");</script>';
redirect('Infoserbilis/index', 'refresh');
}
}
I have tried $this->session->sess_destroy(); on my function logout but to no avail. Thanks in advance
public function logout() {
// Removing session data
$this->session->sess_destroy();
echo '<script>alert("Bye!");</script>';
redirect('Infoserbilis/index', 'refresh');
}
Ok, base on your message, $this->session->sess_destroy() just only destroy the current session, you need redirect page manually. Write the specific route you want to redirect to in redirect() function
/**
* logout
*/
public function logout()
{
$this->session->sess_destroy();
redirect('/');
}
function logout() {
$this->session->unset_userdata('is_searched');
redirect('CONTROLLER/login');
}
You are redirecting to index function in Infoserbilis controller. Check session is active or not in that function.
Here is an example :
public function index() {
if($this->session->userdata('is_logged_in') == true){
//load the required view
}
else {
redirect('controller/login');
}
}
or you can redirect to login from logout function
public function logout() {
$this->session->sess_destroy();
redirect('controller/login');
}
When I load the main page and try to leave the login details blank,I get page you requested is not found.This my code main and login files resp. kindly help
<?php
class Main extends CI_Controller
{
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('login');
}
public function members()
{
$this->load->view('members');
}
public function login_validation()
{
//do validation here,load validation library
$this->load->library('form_validation');
//setting rules for input data
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
}
This means your login.php view does not exist.
You should check your view folder to see if login.php exist there.
I have Controller called login.php that will take login credentials, if true user will be directed to a method profile() in another controller called page.php.
In that profile() method only contain a command to load the view of user's profile.
So the route is like this:
Home->login->profie
But when I try to bypass the login process via url like this
Home->profile
The system still accept that. How can I make a rule that a user can't open profile if they're not logged in?
Here's the Controller:
page.php
class page extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
}
function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}
function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}
function register() {
$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');
}
function profile(){
$this->load->view('page_header');
$this->load->view('content_profile');
$this->load->view('page_footer');
}
function success() {
$this->load->view('page_header');
$this->load->view('content_success');
$this->load->view('page_footer');
}
function logout()
{
//destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();
redirect('page/index');
}
}
?>
login.php
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url','html','security'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
//get form input
$username = $this->input->post('username');
$password = $this->input->post('password');
//form validation
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');
if($this->form_validation->run() == FALSE)
{
//validation fail
$this->load->view('content_login');
}
else
{
//check user credentials
$uresult = $this->user_model->get_user($username, $password);
if(count($uresult)>0)
{
//set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
$this->load->library('../controllers/page');
$this->page->profile();
}
else
{
$this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
$this->load->library('../controllers/page');
$this->page->login();
}
}
}
}
?>
Can anyone please help me how to fix this?
Thank you.
Add a constructor to every applicable controller.
Let the constructor check if the user is logged in, possibly by checking if a particular session exists. If it doesn't redirect to the login page.
Something like below
function __construct(){
parent::__construct();
if(!$this->session->userdata('userid')){
redirect('user/login');
}
}
use this at the top of the page you don't want it accessed when not logged in (supposing you've already set session data)
<?php if (!isset($_SESSION['username'])) {
redirect(base_url());
} ?>
Before showing that page, you should check whether that session exist or not, like if($this->session->userdata('username')). If exists, show that page, if not show any warning.
You can try
if ($this->session->userdata('login') == true) {
redirect('controller');
}
Login Controller
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url','html','security'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
if ($this->session->userdata('login') == true) {
redirect('controller');
}
//get form input
$username = $this->input->post('username');
$password = $this->input->post('password');
//form validation
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');
if($this->form_validation->run() == FALSE)
{
//validation fail
$this->load->view('content_login');
}
else
{
//check user credentials
$uresult = $this->user_model->get_user($username, $password);
if(count($uresult)>0)
{
//set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->username,'uid' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
$this->load->library('../controllers/page');
$this->page->profile();
}
else
{
$this->session->set_flashdata('msg','<div class = "alert alert-danger text-center">Wrong Email/Password</div>');
$this->load->library('../controllers/page');
$this->page->login();
}
}
}
}
Couple things I noticed also in Codeigniter 3 + versions the first letter must only be upper case on FILENAME and Controller Class
Also you don't need to close the controllers and model with ?> https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag
If for example you don't want the user to enter a function without login, you could check the session in the constructor of the class.
If it's only for a specific function, you could just also check the session in the function directly.
Create a helper file login_helper.php
function check_login( $session ) {
$CI =& get_instance();
$CI->load->helper('url');
if(!$session->userdata('login')){
redirect(base_url());
}
}
Now use this helper function in controllers where login is mandatory
class page extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
}
function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}
function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}
function register() {
$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');
}
function profile(){
check_login();
$this->load->view('page_header');
$this->load->view('content_profile');
$this->load->view('page_footer');
}
function success() {
$this->load->view('page_header');
$this->load->view('content_success');
$this->load->view('page_footer');
}
function logout()
{
//destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();
redirect('page/index');
}
}
?>
Note: add login_helper in autoload.php in config folder.
$autoload['helper'] = array('login','url','cookie');
if($this->session->userdata('logged_in') == FALSE) {
$this->session->set_flashdata('error','<p class="alert alert-danger">Please login to view this page.</p>');
redirect('login_c');
exit;
}
I want to connect from one controller in one module to another controller in another module. I want to go from my login module to my dashboard module and use a function inside the dashboard module. Just basically switch from my login module to my dashboard module. Here is my login controllers and my dashboard controller.
class Login extends MX_Controller {
function index()
{
$this->load->view('includes/header');
$this->load->view('login_form');
$this->load->view('includes/footer');
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
//$data['main_content'] = 'signup_form';
//$this->load->view('includes/template', $data);
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_if_email_exists');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[15]|callback_check_if_username_exists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE) // didn't validate
{
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['account created'] = 'Your account has been created. <br /> <br /> You may now login';
$this->load->view('includes/header');
$this->load->view('signup_successful', $data);
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
}
}
function check_if_username_exists($requested_username)
{
$this->load->model('membership_model');
$username_available = $this->membership_model->check_if_username_exists($requested_username);
if ($username_available)
{
return TRUE;
}else{
return FALSE;
}
}
function check_if_email_exists($requested_email)
{
$this->load->model('membership_model');
$email_available= $this->membership_model->check_if_email_exists($requested_email);
if ($email_available)
{
return TRUE;
}else{
return FALSE;
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
my second login controller
class Site extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->is_logged_in();
$this->lang->load('login/login/', 'english');
}
function members_area()
{
$this->load->view('logged_in_area');
//$this->load->module('dashboard/dashboard');
//$this->load->view('home');
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
//$this->load->view('login_form');
}
}
}
the controller in my dashboard module has a function called home I am trying to connect to and use. And the home function of the dashboard has a connection to another module but I cannot get the connection from login to dashboard to work. Also the way my login works I need to connect to the dashboard module through my members_area function. All help greatly appreciated.
Assuming you're using this library
From controller you can use Modules::load or $this->load-module
$moduleInstance = $this->load->module('yourmodule');
// or
$moduleInstance = Modules::load('yourmodule');
// Now you can call any public module controller method
$moduleInstance->somePublicMethod($arg1, $argn);
// you can also use $this->yourmodule as a model instance
Hope this helps