Here i'm creating a login system. Once the user is verified the controller redirects the page to home.php.
There are 2 pages here login_registration.php and home.php
login_registration.php consists login and registration form from here the user is redirected to home.php. both the files are in path C:\wamp\www\CI-Project_1\application\views\login
The issue is it show 404 Page Not Found when redirected.
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
//LOAD MODELS HERE
$this->load->model('Login_model');
}
public function index(){
$this->load->view('login/login_registration.php');
}
function login_user(){
$login_btn = $this->input->post('login_btn');
if($login_btn == TRUE){
$arr['login'] = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$query = $this->Login_model->users_login($arr);
if($query->num_rows() > 0 && $query->num_rows() == 1){
$this->load->library('session');
$this->session->set_userdata($arr['login']);
redirect('login/home.php', 'location');
}
else
{
$this->index();
}
}
else
{
$this->index();
}
}
}
THANKS IN ADVANCE
According to codeigniter views documentation
The .php file extension does not need to be specified unless you use something other than .php.
So, the correct code is as follow:
$this->load->view('login/login_registration');
Also you are not using the redirect function correctly, as i don't see any home function inside login controller. If you somehow have home function inside login controller then why are you using .php after function name?
Check the codeigniter redirect documentation for more info.
You have to mention the name of your controller and method name where you want to redirect something like this
redirect('controllername/methodname');
Try this,
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
//LOAD MODELS HERE
$this->load->model('Login_model');
}
public function index(){
$this->load->view('login/login_registration.php');
}
public function home(){
$this->load->view('login/home.php');
}
function login_user(){
$login_btn = $this->input->post('login_btn');
if($login_btn == TRUE){
$arr['login'] = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$query = $this->Login_model->users_login($arr);
if($query->num_rows() > 0 && $query->num_rows() == 1){
$this->load->library('session');
$this->session->set_userdata($arr['login']);
redirect('login/home', 'location');
}
else
{
$this->index();
}
}
else
{
$this->index();
}
}
}
Why you going to redirect if you want to call any method from a controller then you can redirect like that:
public function home(){
$this->load->view('login/home.php');
}
redirect('login/home', 'location');
But I think you don't need to redirect you can simply load view file just like you already load your registration view. So you just load view Instead of redirect..
$this->load->view('login/home.php');
To redirect on any function in CI can use redirect('controller n_name/function_name','refresh');
For redirect and $this->load->view, don't use .php.
Use something like this:
redirect('login/home', 'location');
Related
at the moment I have to create a project with the PHP framework CodeIgniter. Everything is working properly, except for redirect(). If I call a redirect() after I inserted something to the database the project doesn't redirect to the index page. I already set the URL Helper to autoload and I configured the Base Path.
Controller:
<?php
class Modules extends CI_Controller {
public function __construct() {
parent::__construct();
$this->session->set_userdata('site_language', 'german');
}
public function index($search = '') {
$data['modules'] = $this->module_model->get_modules($search);
$this->load->view('templates/header.php');
$this->load->view('modules/index.php', $data);
$this->load->view('templates/footer.php');
}
public function create() {
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header.php');
$this->load->view('modules/new.php');
$this->load->view('templates/footer.php');
}
else {
$this->module_model->create_module();
redirect('modules/index');
}
}
}
Model:
<?php
class Module_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_modules($search) {
// TODO ~> Suche implementieren
$this->db->order_by('name', 'ASC');
$query = $this->db->get('modules');
return $query->result_array();
}
public function create_module() {
$active = $this->input->post('active');
if (!empty($active)) {
$active = 1;
}
else {
$active = 0;
}
$data = array('name' => $this->input->post('name'), 'active' => $active);
}
}
Base Path:
$config['base_url'] = 'http://localhost/projects/PROJECTNAME/PROJECTNAME_004/';
I'd be very happy if someone could help me. :)
With a bit of help from a friend we solved the problem with passing another parameter with the redirect method. The working redirect looks like this: redirect('modules', 'refresh');
Sadly I still don't know why a normal redirect doesn't work.
well if you still courious why normal redirect doesnt work...try put ob_start(); at the very first line of your code after <?php tag...maybe it can work...
Mate. Define your link in routes.php in the application config folder. That’s why the redirect won’t work.
I'm trying to implement basic section that only a logged-in user can access. I overrided CI_Controller, as follow:
//file created in application/core/MY_Controller.php
class Auth_Controller extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->library('session');
if ($this->session->userdata('user_logged') !== null){
redirect(base_url() . 'dashboard');
die();
} else {
redirect(base_url() . 'auth/login');
die();
}
}
And then I extend from Auth_Controller all the other controllers that are only available for the logged-in user, as follow:
class Dashboard extends Auth_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('dashboardView');
}
}
But after the login is successful, it doesn't redirect to dashboardView.
Anyone knows what is really happening?
You are redirecting infinitely to the Dashboard controller in this part of the code:
if ($this->session->userdata('user_logged') !== null){
redirect(base_url() . 'dashboard');
die();
}
Use this instead (redirect to the login form if the user is not logged in):
class Auth_controller extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->library('session');
// If the user is not logged in
if ($this->session->userdata('user_logged') === null){
// Redirect to http://yoursite/auth/login
// No need for the base_url function, redirect does it for you
redirect('auth/login');
// You don't have to exit/die, redirect() already does that
}
}
}
Opinions
If I check the null, will use like this != null. BUT in CI its a best option to check empty() rather null.
If I use redirect() will use it like this redirect('dashboard');
Code will be
if (!empty($this->session->userdata('user_logged'))) {
redirect('dashboard');
else {
redirect('auth/login');
}
Make sure your site will load without index.php in URL. If no search for .htaccess to your site.
In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}
I am using this code for user which can't access page without login ,but it is not working to prevent the user .
if(! $this->session->userdata('id'))
return redirect('Login');
$this->load->view('public/dashboard');
Try this for start. But generally i use another strategy, i make the if statements in Views.
If this is not works for you, i will check further.
if(! $this->session->userdata('id')){
$this->load->view('Login'); }
else {
$this->load->view('public/dashboard'); }
What you could do is something like this
application / core / MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$route = $this->router->directory . $this->router->fetch_class();
// echo $route;
// Ignore any controllers not to be effected
$ignore = array(
'dirname/controllername',
);
// If the user has not logged in will redirect back to login
if (!$this->session->userdata('id') && !in_array($route, $ignore)) {
$this->session->unset_userdata('id');
redirect($this->config->item('base_url') . 'login');
}
}
}
On the controller change CI_Controller to MY_Controller
class Example extends MY_Controller {
public function index() {
}
}
if( $this->session->userdata('id') == "")
{
redirect('Login');
}
else
{
redirect('dashboard');
}
here Login and dashboard both are controller names.
I am new to codeigniter. I have just made a login feature in CI with the help of google, but here I am redirecting to URL on login but It is not working.
Here is the details
after redirecting the link is like this http://localhost/xpensepedia/index.php/home
which is giving the 404 error
404 Page Not Found
The page you requested was not found.
and my controller is
public function index() {
$this->load->view('header');
if (($this->session->userdata('user_id') != "")) {
redirect(site_url('home'));
} else {
$this->load->view("register");
}
$this->load->view('footer');
}
My files are in the image
Kindly check name of class you given to home.php Controller
Try this...
public function index() {
if (($this->session->userdata('user_id') != "")) {
redirect(site_url('home'));
} else {
$this->load->view('header');
$this->load->view("register");
$this->load->view('footer');
}
}
Home Controller :
class Home extends CI_Controller {
Public function index {
$this->load->view('header');
$this->load->view("home");
$this->load->view('footer');
}
}
you can add url path on constraints ....
you define the url like this on constants :
define('ROUTE_STIE_PATH','localhost/xxx');
define('ROUTE_STIE_PATH_LOGIN',ROUTE_STIE_PATH.'login/');
define('ROUTE_STIE_PATH_HOME',ROUTE_STIE_PATH.'home/');
and then you can echo by simply
echo ROUTE_STIE_PATH_LOGIN
if you want to call any methods with in home then
echo ROUTE_STIE_PATH_HOME.'methodname';
I solve similar case when I change my config file located in application>config>config.php to the following value:
$config['base_url'] = '';
$config['uri_protocol'] = 'REQUEST_URI';