Académique Documents
Professionnel Documents
Culture Documents
html
Code Factory
Web Development for You
1 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
Being a Java developer for almost 10 years now, when I had to move to PHP I chose CodeIgniter
for the following reasons:
Easy to install and configure (being a newbie in PHP this was crucial)
Clean and elegant MVC implementation
Uses Active Record pattern for database access
Overall small footprint and good performance
Usually when you are building a program, the login/logout functionality is a must we always have
to go through, so this quick tutorial will focus on this functionality, taking advantage of the benefits
of using CodeIgniter instead of doing it from scratch in PHP.
Requirements
CodeIgniter framework. By the time this tutorial was done, the latest version was 2.0.2
Any Apache/PHP/MySQL stack. You can install the applications independently, or install
one of those packages that have all of them bundled together.
Installing CodeIgniter
To install CodeIgniter, you only need to uncompress the Zip file you download from the site into
your htdocs directory and youre good to go. Well configure the database access later.
Remember also to add at least one user. Well add one user called bob with password
supersecret.
Configure CodeIgniter
Database Access
Update the file application/config/database.php in your CodeIgniter installation with your
database info:
44 $db['default']['hostname'] = 'localhost';
45 $db['default']['username'] = 'yourdbusername';
46 $db['default']['password'] = 'yourdbpassword';
47 $db['default']['database'] = 'yourdbname';
Default Controller
We need to tell CodeIgniter to land into our login page instead of the default welcome page.
Update the file application/config/routes.php in your CodeIgniter installation with you
controllers name. Well call our landing controller login.
41 $route['default_controller'] = "login";
Default Libraries
In the file application/config/autoload.php you can configure the default libraries you want to
load in all your controllers. For our case, well load the database and session libraries, since we
want to handle user sessions, and also the URL helper for internal link generation
55 $autoload['libraries'] = array('database','session');
2 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
67 $autoload['helper'] = array('url');
Encryption Key
When you use the session library, you need to set the encryption_key in the file
application/config/config.php.
The Code
Here are the actual Views, Controllers and Model we are using for the login functionality.
3 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
4 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>Simple Login with CodeIgniter - Private Area</title>
5 </head>
6 <body>
7 <h1>Home</h1>
8 <h2>Welcome <?php echo $username; ?>!</h2>
9 <a href="home/logout">Logout</a>
10 </body>
11 </html>
The code is pretty easy to follow and understand. Also, you can download the code from here, so
you can install it and test it in your location. Youll only need a full installation of CodeIgniter 2.0.2
and the table in your MySQL database. If you need any help, feel free to leave us a comment or
shoot us an email.
Also, this code uses a pretty basic form validation from CodeIgniter. If you need a more complex
validation process, check CodeIgniters Form Validation docs at their site.
Cheers!
Setting up Glassfish behind Apache Custom Rules for JQuery Validation in Forms
govarthanan says:
January 11, 2013 at 7:52 am
Joe P. says:
January 13, 2013 at 11:16 pm
flashbag says:
January 15, 2013 at 7:30 am
I was just searching for simple login example. And Ive founded it!
Thank you!
Eb says:
January 15, 2013 at 8:45 pm
thanks for the great tutorial. Im just getting started with code igniter. Just an observation, User
Model (application/models/user/php) is supposed to be User Model (application/models
/user.php). Wrong?
http://tinyurl.com/0577horn32250 says:
January 16, 2013 at 12:01 pm
Simple Login with CodeIgniter in PHP Code Factory was in fact a great posting.
5 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
Preetham says:
January 18, 2013 at 1:27 am
Tony says:
January 20, 2013 at 6:24 am
kap says:
January 22, 2013 at 4:24 am
kap says:
January 22, 2013 at 4:25 am
(dap)
SHWETA says:
January 22, 2013 at 5:07 am
i have tried with your code and created database and tables but not getting how to run
this,please help me
Hey! Can you post the error you are getting? Or send me an email
(aarias_at_codefactorycr.com)
julian says:
January 24, 2013 at 9:37 am
you already put the url helper in the autoload.php so why did you still load it in the login
controller?
Good catch! It is not required in the controller as you say. Ill fix the post.
Thanks!
Austin says:
January 27, 2013 at 5:53 am
Im getting a 404 on verifylogin, yet I followed your guide to the teeth. Could it be an htaccess
6 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
problem?
Good catch! I updated the code in user.php to use a more proper version of
$this->db->where(), instead of concatenating strings. More info here: http://ellislab.com
/codeigniter/user-guide/database/active_record.html
tag, youre using CodeIgniters helper to create an absolute path for the form action, instead of
building the url yourself. Have you tried this without the .htaccess to see how it goes?
andres says:
January 28, 2013 at 9:44 pm
In verifylogin.php (line 49) you add the user info to a key called logged_in in the session, so
in the private pages you need to check that this logged_in key exists in the session, like this:
if(!$this->session->userdata(logged_in)){
//Not logged in, redirect to a public page
redirect(home, refresh);
}
Cheers
andres says:
January 28, 2013 at 11:11 pm
if it is exactly what Im doing pass your example to my application, but I have more drivers also
I have to validate these and did what you commented:
if (!$this->session->userdata(logged_in)) {
redirect(home, refresh);
}
I log off, I get redirected to the login form, but when clicking back on my browser allows me to
see all my application. : (
Curiously works fine in Internet Explorer but not in Firefox, Chrome and Opera.
Do you speak Spanish?.
Greetings from Cali, Colombia.
zul says:
January 28, 2013 at 11:28 pm
In order to use the Session class you are required to set an encryption key in your config file.
Jeff says:
7 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
Thanks man now I am starting with codeiniter its very useful for me..
Prashanth.Maddur says:
February 6, 2013 at 3:35 am
Morris says:
February 7, 2013 at 1:41 pm
peter says:
February 10, 2013 at 3:04 pm
Thank you very much, very useful. Ive got only one problem. I think it uses only 1 session for
multiple users !?
If i log in as for example bob and in a new tab mike, then when i have a shopping cart in the
member area and want to shop, then i add something and go to the other tab where mike is
logged in and put something into the shopping cart, the tab where mike is logged in changes to
bob.
thanks in advance
Hi Peter
I think the problem youre having is in your browser. The same happens to me in Firefox and
Chrome, since the session is shared among tabs (e.g. youre in amazon and open multiple
tabs and add them to your cart, keeping the logged in session). You can try this with a different
browser (two different browsers in the same machine) or maybe with Chrome in incognito
mode, since its a separate window and process from the non-incognito window.
Cheers
Sebastian says:
February 11, 2013 at 6:43 am
After doing exactly as requested in the whole login tutorial, what url should I run in my localhost
given my local root is localhost/simple_login?
pliz help
Vladimir says:
February 11, 2013 at 7:14 am
8 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
$username = $this->input->post(username);
Thanks.
Vladimir says:
February 11, 2013 at 7:16 am
$username = $this->input->post(username);
Thanks.
jithu.majinu says:
February 12, 2013 at 4:59 am
thank you
pantas says:
February 12, 2013 at 8:20 pm
help me an error
The server encountered an internal error or misconfiguration and was unable to complete your
request.
Please contact the server administrator, psinaga24@gmail.com and inform them of the time
the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Apache/2.2.4 (Win32) PHP/5.2.3 Server at localhost Port 80
angel says:
February 13, 2013 at 7:12 am
thanks!
Hi pantas
Is there a way you could check Apaches logfiles? It seems something went really wrong and
threw out that 500 error. You can check for the error_log or php_error_log (depending on the
lamp server youre using).
Cheers
Hi Sebastian
9 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
Cheers
Hi Vladimir
As far as I understand, the form_validation methods accept only one field since theyre
validating a field in a form. The $this->input->post(username) is a trick to have both params
when checking the database.
Cheers
Gopika says:
February 13, 2013 at 11:13 pm
Hi Andres
Thank you for the tutorial. As a beginner with codeigniter, it was very helpful.
https://github.com/EllisLab/CodeIgniter/wiki/CodeIgniter-2.1-internationalization-i18n
I had followed this steps and was able to create the language change. But then the login is not
working, it is showing Call to a member function select() on a non-object.
Thanks a lot.
Bar says:
February 14, 2013 at 7:03 am
That was really useful. I am learning the codeigniter framework and i can say it is the best
sample ive ever seen. Thank you.
funti says:
February 15, 2013 at 1:59 pm
aruna says:
February 16, 2013 at 6:32 am
Really good one . searching for simple sample application to learn codeigniter and found it
here
what is the action in the tag form in the login_view?? i dont get it,,because if you hit the
submit button nothing will happen,,oh and by the way thanks for the tutorial i love it
Jack says:
February 18, 2013 at 9:58 am
Hi,
Your tutorial was very helpful! However when I attempt to log in I get this error:
10 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
I posted a question of my problem on StackOverflow. It describes in detail what I did and the
problem I am now having.
http://stackoverflow.com/questions/14940118/simple-login-system-using-codeigniter-returning-
404-on-login#comment20967412_14940118
Id appreciate any help on the topic but once again fantastic tutorial!
Nitish says:
February 19, 2013 at 4:25 am
Severity: Notice
Filename: controllers/verifylogin.php
Line Number: 18
line number 18 is
$this->form_validation->set_rules(username,'Username,'trim|required|xss_clean);
Hi Nitish
Do you have the line $this->load->library(form_validation); before line 18? You need this line
to load the validation library.
Cheers
Hi Scavanza
The action in the form is added here => < ?php echo form_open('verifylogin'); ?>
(login_view.php, line 9). This is the same as creating the full tag
Thanks
11 of 12 5/18/2013 12:43 AM
Simple Login with CodeIgniter in PHP - Code Factory http://www.codefactorycr.com/login-with-codeigniter-php.html
12 of 12 5/18/2013 12:43 AM