8
votes

i am new to code igniter and i am developing simple login system for that i am using xampp , i uploaded code igniter in folder code/ and the following are the codes in mvc

controller login.php

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

  class Login extends CI_Controller
     {

 public function __construct()
 {
      parent::__construct();
      $this->load->library('session');
      $this->load->helper('form');
      $this->load->helper('url');
      $this->load->helper('html');
      $this->load->database();
      $this->load->library('form_validation');
      //load the login model
      $this->load->model('login_model');
 }

 public function index()
 {
      //get the posted values
      $username = $this->input->post("txt_username");
      $password = $this->input->post("txt_password");

      //set validations
      $this->form_validation->set_rules("txt_username", "Username", "trim|required");
      $this->form_validation->set_rules("txt_password", "Password", "trim|required");

      if ($this->form_validation->run() == FALSE)
      {
           //validation fails
           $this->load->view('login_view');
      }
      else
      {
           //validation succeeds
           if ($this->input->post('btn_login') == "Login")
           {
                //check if username and password is correct
                $usr_result = $this->login_model->get_user($username, $password);
                if ($usr_result > 0) //active user record is present
                {
                     //set the session variables
                     $sessiondata = array(
                          'username' => $username,
                          'loginuser' => TRUE
                     );
                     $this->session->set_userdata($sessiondata);
                     redirect("index");
                }
                else
                {
                     $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
                     redirect('login/index');
                }
           }
           else
           {
                redirect('login/index');
           }
      }
 }
 }?>

MOdel is login_model.php

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

 class login_model extends CI_Model
      {
   function __construct()
        {
      // Call the Model constructor
      parent::__construct();
         }

 //get the username & password from tbl_usrs
   function get_user($usr, $pwd)
    {
        $sql = "select * from tbl_usrs where username = '" . $usr . "' and password = '" . md5($pwd) . "' and status = 'active'";
      $query = $this->db->query($sql);
      return $query->num_rows();
 }
  }?>

And View is login_view

   <!DOCTYPE html>
   <html>
     <head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Login Form</title>
 <!--link the bootstrap css file-->
 <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

 <style type="text/css">
 .colbox {
      margin-left: 0px;
      margin-right: 0px;
 }
 </style>
   </head>
           <body>
              <div class="container">
                      <div class="row">
                 <div class="col-lg-6 col-sm-6">
           <h1>COLORS</h1>
      </div>
      <div class="col-lg-6 col-sm-6">

           <ul class="nav nav-pills pull-right" style="margin-top:20px">
                <li class="active"><a href="#">Login</a></li>
                <li><a href="#">Signup</a></li>
           </ul>

           </div>
          </div>
       </div>
        <hr/>

              <div class="container">
               <div class="row">
      <div class="col-lg-4 col-sm-4 well">
      <?php 
      $attributes = array("class" => "form-horizontal", "id" => "loginform", "name" => "loginform");
      echo form_open("login/index", $attributes);?>
      <fieldset>
           <legend>Login</legend>
           <div class="form-group">
           <div class="row colbox">
           <div class="col-lg-4 col-sm-4">
                <label for="txt_username" class="control-label">Username</label>
           </div>
           <div class="col-lg-8 col-sm-8">
                <input class="form-control" id="txt_username" name="txt_username" placeholder="Username" type="text" value="<?php echo set_value('txt_username'); ?>" />
                <span class="text-danger"><?php echo form_error('txt_username'); ?></span>
           </div>
           </div>
           </div>

           <div class="form-group">
           <div class="row colbox">
           <div class="col-lg-4 col-sm-4">
           <label for="txt_password" class="control-label">Password</label>
           </div>
           <div class="col-lg-8 col-sm-8">
                <input class="form-control" id="txt_password" name="txt_password" placeholder="Password" type="password" value="<?php echo set_value('txt_password'); ?>" />
                <span class="text-danger"><?php echo form_error('txt_password'); ?></span>
           </div>
           </div>
           </div>

           <div class="form-group">
           <div class="col-lg-12 col-sm-12 text-center">
                <input id="btn_login" name="btn_login" type="submit" class="btn btn-default" value="Login" />
                <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-default" value="Cancel" />
           </div>
           </div>
      </fieldset>
      <?php echo form_close(); ?>
      <?php echo $this->session->flashdata('msg'); ?>
               </div>
             </div>
             </div>

          <!--load jQuery library-->
       <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
       <!--load bootstrap.js-->
     <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
     </body>
      </html>

IN config i used $config['index_page'] = ''; and in routes i used $route['default_controller'] = 'login'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; while accessing localhost/code/ it is working fine but when click login button it is going to url http://localhost/code/localhost/login/index and showing object not found ERROR 404

3
set your base_url in config fileManinderpreet Singh
i set base url as localhostGowri Sankar
add your project name alsoManinderpreet Singh
i added localhost/code but it showing localhost/code/localhost/code/login/index object not found and error 404Gowri Sankar
Your file and class names should have first letter upper case as per user guide if your using CI3 versions.Mr. ED

3 Answers

22
votes

Open application/config/config.php and set your base_url(). E.g: $config['base_url'] = 'http://localhost/code/';

Create .htaccess file under /code folder (Where application and system folder is) like below:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2
votes

create .htaccess file in \code\

as

RewriteEngine On
RewriteBase /code/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

and in config/config.php

$config['base_url'] = 'http://localhost/code';

Hope it will help you.

1
votes

I got same error as shown below. I am using php/codeigniter. I have defined $config['base_url'] = 'http://localhost/******'; in my config file. still it was showing same issue.

Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster.

How could I solve this.

Step 1: 1st i checked httpd.conf file. there mod_rewrite was imported.

Step 2: checked file is located at actual location or not.

Step 3: checked spelling mistake in redirection or anchor tag.

Step 4: checked $config['base_url'] variable in application/config.php file.

Step 5: checked $config['index_page'] variable in application/config.ph file. here was my actual problem. I set it to blank and there was something wrong with my mod_rewrite module. when I put $config['index_page']='index.php' it started working for me.