Explore, Learn & Share

Admin Panel with Login in CodeIgniter

[dfads params=’groups=-1′]

Today, I came up with a solution for how to make an inbuilt Admin Panel with already coded Login module in codeigniter. Just follow the steps below to setup into your project.

1. Download the adminPanelWithLogin.zip file. Extract it. Copy the respective admin folders to the Controllers, Models, & Views of your project.

Screenshot - Saturday 24 August 2013 - 05:23:59  IST

2. Create the MySQL table & populate data.

[sql]
CREATE TABLE IF NOT EXISTS `tbl_admin` (
`admin_id` int(4) NOT NULL AUTO_INCREMENT,
`admin_name` varchar(128) NOT NULL,
`admin_email` varchar(64) NOT NULL,
`admin_password` varchar(64) NOT NULL,
`admin_role` int(2) NOT NULL,
`admin_status` int(1) NOT NULL,
PRIMARY KEY (`admin_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

INSERT INTO `tbl_admin` (`admin_id`, `admin_name`, `admin_email`, `admin_password`, `admin_role`, `admin_status`) VALUES
(1, ‘admin’, ‘admin’, ‘admin_123’, 1, 0),
(13, ‘super’, ‘super’, ‘super_123’, 1, 1);
[/sql]

3.  Check whether the database & session are initiated by exploring to application/config/autoload.php file.

[php]$autoload[‘libraries’] = array(‘database’, ‘session’); [/php]

4. Browse the admin login from you browser: http://localhost/your_project/admin. A screen must appear like this:

Screenshot - Saturday 24 August 2013 - 05:40:09  IST

5. Use the default admin user as “admin ” and Password as “admin_123” then press Login. You will be redirected to a secure home page like this:

Screenshot - Saturday 24 August 2013 - 05:40:32  IST

You can further add modules of your choice & code your project as you like.

Feedbacks & suggestions are always welcome.

[dfads params=’groups=-1′]

69 responses to “Admin Panel with Login in CodeIgniter”

  1. Adi Avatar
    Adi

    Hey

    I try all your instructions but when i try admin page it give me error that page not find. So any further instruction for this. Thanks

    1. admin Avatar
      admin

      Deaer Adi, You might have been missing the clean URL part. So, please try this link to access the admin page: http://localhost/your_project/index.php/admin.
      Hope this may work.

      Regards
      Admin

      1. Paul Chater Avatar
        Paul Chater

        Will this work with a HMVC approach?

        1. admin Avatar
          admin

          Dear Paul, This is done for plain CI. You can further modify the code according to your needs.

      2. sush Avatar
        sush

        i am beginner in ci , i am facing similar problem, i have tried http://localhost/your_project/index.php/admin. also but there is no change….it would be very helpful for your further guidance

  2. arnezt Avatar
    arnezt

    where is the MY_Controller file? refers to “class Home extends MY_Controller”

    1. admin Avatar
      admin

      Dear arnezt,

      Pleae rename MY_Controller to CI_Controller, that will work.

      Thanx,
      Admin

  3. huseyn Avatar
    huseyn

    Thank you very much! I created that.
    And due to your example I increase my knowledge in mvc.

  4. syafiq Avatar
    syafiq

    I’m trying using your script and its goes page not found..i’ve tried http://localhost/app/index.php/admin
    page not found

    1. admin Avatar
      admin

      Dear Syafiq, something might have gone wrong with your code. Please recheck.

    2. manoj Avatar
      manoj

      plz make sure u’ve managed ur .httaccess file or route.php file inside config/ folder.
      same problem i’v faced but .htaccess worked for me…..
      if u dnt have .htacces file plz google once and create a .httaccess file

  5. nendi Avatar
    nendi

    thank you for share.. This really helped me

  6. didik Avatar
    didik

    I try to use the latest version is not successful ci, ci weve uses the version number?
    beg in reply to this comment ?

    1. admin Avatar
      admin

      I am not sure about the latest version of the CI. We use earlier versions. But I hope there might not be any kind of issues with the latest version. Please visit http://ellislab.com/codeigniter for further information.

  7. php training kolkata Avatar
    php training kolkata

    excellent information hope to get more in future.

  8. netcat Avatar
    netcat

    i try to go to index.php/admin page but i redirect to index of my page and noting show to me.

    so what can i do????

    1. admin Avatar
      admin

      Did you put the files at correct location?

  9. Fahad Avatar
    Fahad

    do you know the exact solution of this .httaccess in codeigniter because i test on wamp , i enable all of modules also add code in htaccess but its not working still, any suggestions you have.

    1. admin Avatar
      admin

      Did u put the script as below in your .htaccess file?

      RewriteEngine on
      RewriteBase /

      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule .* your_project_name/index.php/$1 [PT,L]

  10. tunde Avatar
    tunde

    The download link displays 404 error. The page cant be found. Kindly mail me the adminPanelWithLogin.zip file. Thanks in anticipation.

  11. admin Avatar
    admin

    Dear Tunde,

    You can Download the file from the same link.

    Thanx.

  12. parmar prahlad bhagvanji bhai Avatar
    parmar prahlad bhagvanji bhai

    Hello sir,

    I have extracted the files as per you said.
    I put different files as following
    controller/admin
    view/admin
    models/admin

    When I run using as this path : http://localhost/prahladandhari/avangatecms/admin
    I get following errors :
    Object not found. suggest solution.
    Thanks.

    1. admin Avatar
      admin

      Dear Parmar

      You can try this link: http://localhost/prahladandhari/avangatecms/index.php/admin/login

      I hope this works.

      Thanks and sorry for late reply.

  13. Val Avatar
    Val

    In order to use the Session class you are required to set an encryption key in your config file.

    1. admin Avatar
      admin

      Yes Val, Its for your convinience. Its a better option if you use an encryption key.

  14. hani Avatar
    hani

    error with style, exactly in VIEWBASE
    that in view files.

    how i can define it and where?

    1. admin Avatar
      admin

      VIEWBASE is defined in /application/config/config.php
      Enjoy coding.

      Thanx,
      Admin

    2. FR Avatar
      FR

      Class ‘MY_Model’ not found in your download folder

      1. admin Avatar
        admin

        Dear FR,
        Make MY_Model to CI_Model, this will definitely work.

        Thanks,

  15. sara Avatar
    sara

    how can define VIEWBASE in config ??
    $config[‘VIEWBASE’]=”http://localhost/example/” is it correct??

    1. admin Avatar
      admin

      1. Locate the file config.php in /application/config/ directory
      2. define(‘VIEWBASE’,’http://localhost/example/application/views/’); in that file.

      Thanx
      Dev

  16. naqqashrana Avatar
    naqqashrana

    Respected sir when i try to access admin/index page through this url http://localhost/codeigniter/admin.
    But there is showing welcome page and if i want to get http://localhost/codeigniter/index.php/admin still there is showing same welcome page.

  17. Jeffrey Avatar
    Jeffrey

    Howdy, i read your blog from time to time and i own a similar one and i was just curious
    if you get a lot of spam comments? If so how do you protect against it, any plugin or anything you can recommend?

    I get so much lately it’s driving me crazy so any support is very much appreciated.

  18. david Avatar
    david

    this is happened when login
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: data

    Filename: admin/home.php

    Line Number: 13
    plz help to solve this issue i am tryin but not getting proper way..

  19. Francois Avatar
    Francois

    David,
    You have to define the value of your $data, then pass it like this: $this->viewForm($data);

  20. Francois Avatar
    Francois

    David you can define your data value using
    $data = “”;
    if($this->session->userdata(‘xxxxxxxxx’)){
    $query = $this->session->all_userdata();
    $data[‘yyy’] = $query[‘xxxxx’];
    $data[‘yyy’] = $query[‘xxxxx’];
    $data[‘yyy’] = $query[‘xxxxx’];
    }
    $this->viewForm($data);

    Please replace the xxx and yyy with name define in your code. It works for me.

  21. Raj Kumar Bhardwaj Avatar
    Raj Kumar Bhardwaj

    dear sir,

    i extract your files and replace the all folder like controllers with your folder controllers and same as views and models , after that i run the change the routes file with the default controllers index , but this is giving the error with “The page was you requested was not found “please help me with all steps .

    1. admin Avatar
      admin

      you can try http://localhost/project_name/index.php/admin in the browsers URL.

      Thanks
      Admin

  22. Raj Kumar Bhardwaj Avatar
    Raj Kumar Bhardwaj

    what is code i can write for admin path and front end path in to the routs.php?

  23. Nicola Oda Avatar
    Nicola Oda

    Hi, i have succeed to run this trial apps, but the style won’t show eventhough i have defined the ‘viewbase’ code in config file. And i try to change the url src to be my link project refer to the css file something like this ‘http://localhost/project_name/application/views/admin/css/bootstrap.css’ but it still won’t show the style from the css files.

    Maybe you can kindly tell me what’s wrong with this?

    Thank you

    1. admin Avatar
      admin

      Did u check whether you can access this url http://localhost/project_name/application/views/admin/css/bootstrap.css from the browser? You might have Access Forbidden problem. If yes, check the permission of the directories.

      Thanks

  24. Mayank Avatar
    Mayank

    Hello,

    I downloaded the files and placed them on the specified locations. I used the RewriteRule provided by you earlier in this topic. I used “$route[‘admin/(:any)’] = “admin/$1″;” in routes.php, but still when I try to open the admin it gives me 404 error. Please let me know where I am lacking…?

    Thanks

  25. Mayank Avatar
    Mayank

    Hi,

    I cracked my last step. But now I stuck in next hurdle. When I input the login credentials (admin, admin_123) in Login Area.
    When I hit the login Button it remains on the same Login Window Page with redirected url in the bar to “admin/home” and it gives me the error msg: “Invalid username and/or password”.

    I would req. you to provide the solution ASAP.

    Thanks

    1. Dev Avatar
      Dev

      Dear Mayank,

      Please check whether you have created your mysql table / database. If yes, please check whether you are entering the right credentials.

      Thanks

      1. Mayank Avatar
        Mayank

        Dear Admin,

        What should I do to get it done, login part. I just followed the same code provided by you.

        Thanks

  26. Mayank Avatar
    Mayank

    Hi Dev,

    Thank you for the reply. I have created the database and credentials are correct. I used the same code provided above to create the database table. Do I need to create a session table…?

    Thanks

    1. admin Avatar
      admin

      Dear Mayank,

      There is no need to create any session table.

      Thanks
      Dev

      1. Mayank Avatar
        Mayank

        Dear Admin,

        What should I do to get it done, login part. I just followed the same code provided by you.

        Thanks

        1. admin Avatar
          admin

          Dear Mayank,

          Did you tried http://localhost/your_project/index.php/admin? If this is working you have to configure clean URL on your server.

          Thanks
          Dev

          1. Mayank Avatar
            Mayank

            Dear Admin,

            Yes I am working the same url pattern and yes it’s working, It is showing the login screens, but the problem occurs when I hit the login button.

            Thanks

  27. Mayank Avatar
    Mayank

    Dear Admin,

    Please suggest the solution to my above mentioned query.

    public function process(){
    // Load the model
    //$this->load->model(‘login_model’);
    $this->load->model(‘admin/admin_model’);
    // Validate the user can login
    $result = $this->admin_model->validate();
    // Now we verify the result
    if(! $result){
    // If user did not validate, then show them login page again
    $msg = ‘Invalid username and/or password.’;
    $this->index($msg);
    }else{
    echo “Loading…”;
    // If user did validate,
    // Send them to members area
    redirect(base_url().’index.php/admin/home/’);

    }
    }

    Both the conditions (If, else) are working from this code. I am using user as “admin ” and Password as “admin_123″

    1. Ram Avatar
      Ram

      Please check what $result is printing using print_r. I am not sure what mistake you are making at your code. You can debug the code instead. You are very near to your goal.

      Thanks
      Dev

      1. Mayank Avatar
        Mayank

        I tried using Print_r. Validation process is working fine. However I haven’t changed the code it’s same as I downloaded from here.

        Here both If and else condition is working like when I input user as “admin ” and Password as “admin_123″, it redirects to home in URL but it remains on the same page (login), as well it prints the message “Invalid username and/or password”.

        Thanks

        1. admin Avatar
          admin

          There is a redirect() function in the controller home.php. This function is missing at your side. Please replace it with any redirect code.

          Thanks
          Dev

          1. Mayank Avatar
            Mayank

            controller home.php

            check_isvalidated();
            }
            public function index()
            {
            $this->viewForm($data);
            }

            private function check_isvalidated(){
            if(! $this->session->userdata(‘validated’)){
            redirect(‘admin/login’);
            }
            }

            private function viewForm($data=”){
            $this->load->view(‘admin/header’);
            $this->load->view(‘admin/sidebar’);
            $this->load->view(‘admin/home’, $data);
            $this->load->view(‘admin/footer’);
            }

            }
            ?>

            where should i use the redirect function. I tried it at different locations but didn’t get the success.

            In my last post I provided the code of process() here I am not getting it how both the conditions are working.

  28. admin Avatar
    admin

    Please send me the result after print_r($result).

    Thanks

    1. Mayank Avatar
      Mayank

      It shows 1 when I use user as “admin ” and Password as “admin_123″ and blank if I input wrong credentials.

      I am using below mentioned routes, please let me know if I can do something here

      $route[‘admin’] = ‘admin/login’;
      $route[‘admin/(:any)’] = ‘admin/home/$1’;
      $route[‘admin/(:any)’] = ‘admin/login/process’;

  29. admin Avatar
    admin

    I have used the following routes in my routes.php

    $route[‘default_controller’] = “home”;
    $route[‘404_override’] = ”;

    Thanks
    Dev

    1. Mayank Avatar
      Mayank

      If I use this route

      $route[‘default_controller’] = “home”;
      $route[‘404_override’] = ”;

      It shows “404 Page Not Found”.

      But when I use

      $route[‘default_controller’] = ‘admin/home’;
      $route[‘404_override’] = ”;

      Its shows the home directly without login page, with the following error.

      A PHP Error was encountered

      Severity: Notice

      Message: Undefined variable: data

      Filename: admin/home.php

      Line Number: 13

      Thanks

  30. Mayank Avatar
    Mayank

    I am using $route[‘default_controller’] for frontend as well. Below is the route.php code which I am using for frontend and admin part.

    $route[‘admin’] = ‘admin/login’;
    $route[‘admin/(:any)’] = ‘admin/home/$1’;
    $route[‘admin/(:any)’] = ‘admin/login/process’;

    $route[‘news/create’] = ‘news/create’;
    $route[‘news/(:any)’] = ‘news/view/$1’;
    $route[‘news’] = ‘news’;
    $route[‘default_controller’] = ‘pages/view’;
    $route[‘(:any)’] = ‘pages/view/$1’;

    Thanks

  31. Mayank Avatar
    Mayank

    Dear Admin,

    Any solution of my query…?

    Thanks

  32. samer taha Avatar
    samer taha

    i have a suggestion for the developer to save support time for you what you dont offer the admin app for download already installed in a working codeigniter demo site plus the sql file and thats it.
    thanks.

  33. sathish Avatar
    sathish


    hi admin..
    i'm gettnig error in VIEWBASE. and home page it showing list footer and all in center of page it showing empty box. how to solve it??

    thank you.!

    1. admin Avatar
      admin

      Dear Satish,

      Sorry for late reply. Can you please send a screenshot of the output at nomeasdev@gmail.com? This will help me understanding your issue more clearly.

      Thanks

  34. shimii Avatar
    shimii

    i am a codeigniter beginner so what is the database name

  35. shimii Avatar
    shimii

    i am getting error viewbase how ca solve this

  36. Decatur il Wedding Venues Avatar
    Decatur il Wedding Venues

    Your method of describing everything in this piece of writing is actually nice, every one
    can effortlessly understand it, Thanks a lot.

  37. Tiffany Avatar
    Tiffany

    Oh my goodness! Incredible article dude! Thanks,
    However I am experiencing troubles with your RSS.
    I don’t know the reason why I cannot join it. Is there anyone
    else getting the same RSS issues? Anybody who knows the solution will you kindly respond?

    Thanks!!

  38. Peoria illinois wedding reception venues Avatar
    Peoria illinois wedding reception venues

    I got this web site from my pal who shared with me on the topic of this web page and now this time I am visiting this web page and reading very
    informative posts here.

Leave a Reply

Your email address will not be published. Required fields are marked *