Welcome Guest, Not a member yet? Register   Sign In
redirect()
#1

[eluser]davy_yg[/eluser]
Hello, I am following this tutorial: http://www.codefactorycr.com/login-with-...r-php.html

What's redirect means ? In the tutorial seems there are several controllers. So is redirect means to switch to another controller ?

This is my codes:

controllers/admin/login.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
class Login extends CI_Controller {
  
    function __construct()
    {
    parent::__construct();
    }
  
  function index()
    {
    $this->load->helper(array('form'));
    $this->load->view('admin/login_view');
       }
  
}
  
?>

controllers/admin/verifylogin.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
class VerifyLogin extends CI_Controller {
  
  function __construct()
  {
    parent::__construct();
    $this->load->model('user','',TRUE);
  }
  
  function index()
  {
    //This method will have the credentials validation
    $this->load->library('form_validation');
  
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
  
    if($this->form_validation->run() == FALSE)
    {
      //Field validation failed.  User redirected to login page
      $this->load->view('login_view');
    }
    else
    {
      //Go to private area
      $this->load->view('admin/admin');
    }
  
  }
  
  function check_database($password)
  {
    //Field validation succeeded.  Validate against database
    $username = $this->input->post('username');
  
    //query the database
    $result = $this->user->login($username, $password);
  
     if($result)
    {
      $sess_array = array();
      foreach($result as $row)
      {
        $sess_array = array(
          'id' => $row->id,
          'username' => $row->username
        );
        $this->session->set_userdata('logged_in', $sess_array);
      }
      return TRUE;
    }
    else
    {
      $this->form_validation->set_message('check_database', 'Invalid username or password');
      return false;
    }
  }
}

?>

controllers/admin/home.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

session_start(); //we need to call PHP's session object to access it through CI

class Home extends CI_Controller {
  
  function __construct()
  {
    parent::__construct();
  }
  
  function index()
  {
    if($this->session->userdata('logged_in'))
    {
      $session_data = $this->session->userdata('logged_in');
      $data['username'] = $session_data['username'];
      $this->load->view('admin/admin', $data);
    }
    else
    {
      //If no session, redirect to login page
      redirect('login', 'refresh');
    }
  }
  
  function logout()
  {
    $this->session->unset_userdata('logged_in');
    session_destroy();
    redirect('admin/admin', 'refresh');
  }
  
}
  
?>

views/admin/login_view.php

[code]


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

&lt;html &gt;
  &lt;head&gt;
    &lt;title&gt;Simple Login with CodeIgniter&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    <h1>Simple Login with CodeIgniter</h1>
    &lt;?php echo validation_errors(); ?&gt;
    &lt;?php echo form_open('verifylogin'); ?&gt;
      <label for="username">Username:</label>
      &lt;input type="text" size="20" id="username" name="username"/&gt;
      <br/>
      <label for="password">Password:</label>
      &lt;input type="password" size="20" id="password" name="password"/&gt;
      <br/>
      &lt;input type="submit" value="Login"/&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

models/user.php

Code:
&lt;?php

Class User extends CI_Model
{

    function login($username, $password)
    {
    $this -> db -> select('id, username, password');
    $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;
      }
    }
   }
?&gt;

routes.php

Code:
$route['admin/login_view'] = 'admin/login';

My file structure:

controllers/admin/ home.php
controllers/admin/ login.php
controllers/admin/ verifylogin.php

application/views/admin.php
application/views/adminnav.php
application/views/login_view.php


That's my codes. I wonder why after I login to login page it brings me to : http://localhost/IndonusaCI/index.php?verifylogin (this page instead remaining in the login page or go to the admin page)

I wonder why ?

Please help. Thanks before.
#2

[eluser]Tpojka[/eluser]
Use same file structure as shown in tutorial. If you achive it works, start with changing code regarding your needs.
Have you tried exact code if worked as well?
If so, when you change particular line of code and it doesn't work, say what line is that.
But first you have to be sure you are dealling with something that works besides comments in article.
Make sure it works as is in very your system.
#3

[eluser]davy_yg[/eluser]
The problem is there is no error message appears, just the url changes as I said and I cannot figure out which line of codes cause the problems.

I wonder why after I login to login page it brings me to : http://localhost/IndonusaCI/index.php?verifylogin (this page instead remaining in the login page or go to the admin page)

Can anyone tells me which line of code do I need to change ?
#4

[eluser]davy_yg[/eluser]
I try the exact code and it does works. I wonder why the modified code does not work. I only change the address of the file after the login and trying to match it with my system.

I wonder what causes the error.
#5

[eluser]Tpojka[/eluser]
Most people here don't have so much time to read pages and pages and files and files.
Write down two codes that are changed with pointing on those lines. For example:
works
Code:
&lt;?=isset($example_var) ? 1 : 0;?&gt;
doesn't
Code:
&lt;?=isset($my_var) ? 1 : 0;?&gt;
Locate the problem, meaning read all code you use (addon for login etc).

Maybe you should change your routes.php line. You don't put there location of view file, but something you want to be typed in browser URL.
#6

[eluser]davy_yg[/eluser]
Strange, I keep getting the same result.

This is my routes:

Code:
$route['admin/login_view'] = 'admin/login';
$route['admin/admin'] = 'admin/home';
#7

[eluser]Tpojka[/eluser]
Seems you are "making house from roof" here.
It is irrelevant folder organization and routing until you have valid code that make the job done.
For start, remove session_start(); and put
Code:
$this->load->library('session');
in constructor if not autoloaded. If it is application that is using sessions, maybe better idea would be autoloading that library.




Theme © iAndrew 2016 - Forum software by © MyBB