URL problem |
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. |
Messages In This Thread |
URL problem - by subhajitmaity - 02-26-2015, 05:22 AM
RE: URL problem - by massaki - 02-26-2015, 06:15 AM
RE: URL problem - by aurelien - 02-26-2015, 06:18 AM
RE: URL problem - by subhajitmaity - 02-26-2015, 09:45 AM
RE: URL problem - by james - 02-26-2015, 08:55 AM
RE: URL problem - by Narf - 02-26-2015, 09:45 AM
RE: URL problem - by subhajitmaity - 02-26-2015, 09:53 AM
RE: URL problem - by Rufnex - 02-26-2015, 10:57 AM
RE: URL problem - by subhajitmaity - 02-26-2015, 11:40 AM
RE: URL problem - by Rufnex - 02-26-2015, 11:46 AM
RE: URL problem - by Hobbes - 02-26-2015, 01:13 PM
|