Welcome Guest, Not a member yet? Register   Sign In
URL problem
#1

I create a simple login system.The code is the following

Create yourcodeigniter/application/models/m_login.php:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class M_login extends CI_Model {

   function __construct() {
       parent::__construct();
       $this->load->database();
   }

   function login($username, $password) {
       //create query to connect user login database
       $this->db->select('id_admin, name_admin, password');
       $this->db->from('admin');
       $this->db->where('name_admin', $username);
       $this->db->where('password', MD5($password));
       $this->db->limit(1);
       
       //get query and processing
       $query = $this->db->get();
       if($query->num_rows() == 1) {
           return $query->result(); //if data is true
       } else {
           return false; //if data is wrong
       }
   }
}
 
/* End of file m_login.php */
/* Location: ./application/models/m_login.php */



Create yourcodeigniter/application/controllers/c_login.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_login extends CI_Controller {
   function index() {
       $this->load->helper(array('form','html'));
       $this->load->view('v_login'); //load view for login
   }
}
/* End of file c_login.php */
/* Location: ./application/controllers/c_login.php */


Create yourcodeigniter/application/views/v_login.php:
<!DOCTYPE html>
<html>
<head>
  <title>Simple Login with CodeIgniter</title>
</head>
<body>
  <h1>Simple Login with CodeIgniter</h1>
  <?php echo validation_errors(); ?>
  <?php echo form_open('c_verifylogin');
  echo form_label("Username: ");
  echo form_input("username");
  echo br();
  echo form_label("Password: ");
  echo form_password("password");
  echo br();
  echo form_submit("","Login");
  echo form_close();
  ?>
</body>
</html>



Create Verify login on yourcodeigniter/application/controllers/c_verifylogin.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_verifyLogin extends CI_Controller {
   function __construct() {
       parent::__construct();
       //load session and connect to database
       $this->load->model('m_login','login',TRUE);
       $this->load->helper(array('form', 'url','html'));
       $this->load->library(array('form_validation','session'));
   }

   function index() {
       $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) {
           $this->load->view('v_login',$data);
           } else {
               //Go to private area
               redirect(base_url('c_home'), 'refresh');
           }      
    }

    function check_database($password) {
        //Field validation succeeded.  Validate against database
        $username = $this->input->post('username');
        //query the database
        $result = $this->login->login($username, $password);
        if($result) {
            $sess_array = array();
            foreach($result as $row) {
                //create the session
                $sess_array = array('id_admin' => $row->id_admin,
                    'name_admin' => $row->name_admin);
                //set session with value from database
                $this->session->set_userdata('logged_in', $sess_array);
                }
         return TRUE;
         } else {
             //if form validate false
             $this->form_validation->set_message('check_database', 'Invalid username or password');
             return FALSE;
         }
     }
}
/* End of file c_verifylogin.php */
/* Location: ./application/controllers/c_verifylogin.php */

Create yourcodeigniter/application/controllers/c_home.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_home extends CI_Controller {
   function __construct() {
       parent::__construct();
       $this->load->model('m_login','',TRUE);
       $this->load->helper('url');
       $this->load->library(array('form_validation','session'));
   }

   function index() {
       if($this->session->userdata('logged_in'))
       {
           $session_data = $this->session->userdata('logged_in');
           $data['username'] = $session_data['name_admin'];
           $data['id'] = $session_data['id_admin'];
           $this->load->view('v_home', $data);
       } else {
       //If no session, redirect to login page
           redirect('c_login', 'refresh');
       }
   }

   function logout() {
        //remove all session data
        $this->session->unset_userdata('logged_in');
        $this->session->sess_destroy();
        redirect(base_url('c_login'), 'refresh');
    }

}
/* End of file c_home.php */
/* Location: ./application/controllers/c_home.php */



Create yourcodeigniter/application/views/v_home.php:
<!DOCTYPE html>
<head>
  <title>Simple Login with CodeIgniter - Private Area</title>
</head>
<body>
  <h1>Home</h1>
  <h2>Welcome <?php echo $username; ?>!</h2>
  <a href="c_home/logout">Logout</a>
</body>
</html>




And my base_url is::$config['base_url'] = 'http://localhost/codeignator/index.php';


The problem is that when I click on Login button the url looks like "http://localhost/codeignator/index.php/index.php/c_verifylogin".. There is two index.php in url.but it should be one.If there is one index.php then the programme run correctly..what is the problem...??please help me.
Reply
#2

The config.php file (application/config) has tho variables:
- $config['base_url']
- $config['index_page']

I think you defined base_url with "index.php". Just remove it.
Reply
#3

Hi,

I think you should be remove "index.php" from the ::$config['base_url'].
Reply
#4
Thumbs Up 

Hello,

You do'nt need to set the whole URI in $config['base_url']

set only

Code:
$config['base_url'] = '//localhost/codeignator/'

Because your $config['index_page'] should look like this  

Code:
$config['index_page'] = 'index.php'
Reply
#5

DO NOT USE MD5!

Use password_hash() and password_verify().
Reply
#6

(02-26-2015, 06:18 AM)aurelien Wrote: Hi,

I think you should be remove "index.php" from the ::$config['base_url'].


if i remove index.php from base url then after clicking log in button the url looks like http://localhost/codeignator/c_home..which should be http://localhost/codeignator/index.php/c_home.....if the url looks like http://localhost/codeignator/index.php/c_home then the progrm run correctly..
Reply
#7

(02-26-2015, 09:45 AM)Narf Wrote: DO NOT USE MD5!

Use password_hash() and password_verify().


ok ok...but please solve my issue...
Reply
#8

Have you a .htaccess file in the document root?

PHP Code:
RewriteEngine on
RewriteBase 
/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ index.php?/$[L

Reply
#9

(02-26-2015, 10:57 AM)Rufnex Wrote: Have you a .htaccess file in the document root?


PHP Code:
RewriteEngine on
RewriteBase 
/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ index.php?/$[L

yes i have
Reply
#10

Try to this config setting:

$config['base_url'] = '//localhost/codeignator/'
$config['index_page'] = '';

Reply




Theme © iAndrew 2016 - Forum software by © MyBB