Www Codefactorycr Com

  • Upload
    don-sur

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 Www Codefactorycr Com

    1/13

  • 8/13/2019 Www Codefactorycr Com

    2/13

    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 controller s name. Well call our landing 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

    Encryption Key

    When you use the sessionlibrary, you need to set the encryption_keyin the file application/config/config.php.

    The Code

    Here are the actual Views, Controllers and Model we are using for the login functionality.

    User Model (application/models/user.php)

    Login Controller (application/controllers/login.php)

    41 $route['default_controller'] = "login";

    55 $autoload['libraries'] = array('database','session');

    67 $autoload['helper'] = array('url');

    227 $config['encryption_key'] = 'REALLY_LONG_NUMBER';

    1

    1

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDF
  • 8/13/2019 Www Codefactorycr Com

    3/13

    Login View (application/views/login_view.php)

    VerifyLogin Controller (application/controllers/verifylogin.php)

    This controller does the actual validation of the fields and checks the credentials against the database.

    1

    2 3 4 Simple Login with CodeIgniter5 6 7 Simple Login with CodeIgniter8

    9 10 Username:11 12
    13 Password:14 15
    16 17 18 19

    1

  • 8/13/2019 Www Codefactorycr Com

    4/13

    Setting up Glassfish behind Apache Custom Rules for JQuery Validation in Forms

    Home Controller (application/controllers/home.php)

    This is the private page (only authenticated users can access it).

    Home Page View (application/views/home_view.php)

    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

    Validationdocs at their site.

    Cheers!

    Posted in Blog, PHP

    49 comments on Simple Login with CodeIgniter in PHP

    59 }60 ?>

    1

  • 8/13/2019 Www Codefactorycr Com

    5/13

    govarthanansays:

    January 11, 2013 at 7:52 am

    its good one

    Joe P.says:

    January 13, 2013 at 11:16 pm

    Nice and concise. Thanks

    alex mbirusays:

    January 14, 2013 at 2:08 am

    this script help me a lot, keep the good work.

    flashbagsays:

    January 15, 2013 at 7:30 am

    I was just searching for simple login example. And Ive founded it!

    Thank you!

    Ebsays:

    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?

    Andres Ariassays:

    January 15, 2013 at 9:48 pm

    Fixed! Thanks for the heads up!

    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.

    In case it possessed much more pics this would be even

    much better. Regards ,Gilda

    Preethamsays:

    January 18, 2013 at 1:27 am

    thanks a lot for posting this.

    Tonysays:

    January 20, 2013 at 6:24 am

    Thanks for this! It helped me a great deal.

    It was posted back in 2011 from the looks of it, but still works line for line with the current version in 2013.

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://tinyurl.com/0577horn32250http://flashbag.if.ua/http://www.technologykenya.com/http://joeparente.com/
  • 8/13/2019 Www Codefactorycr Com

    6/13

    Shahzeb Babarsays:

    January 22, 2013 at 2:47 am

    It is open for SQL injection. tried it myself.

    kapsays:

    January 22, 2013 at 4:24 am

    This is fantastic work..

    and also thanks

    kapsays:

    January 22, 2013 at 4:25 am

    (dap)

    SHWETAsays:

    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

    Taukil Alisays:

    January 24, 2013 at 5:46 am

    Great post .I like it.

    Andres Ariassays:

    January 24, 2013 at 8:51 am

    Hey! Can you post the error you are getting? Or send me an email (aarias_at_codefactorycr.com)

    juliansays:

    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?

    Andres Ariassays:January 24, 2013 at 9:54 am

    Good catch! It is not required in the controller as you say. Ill fix the post.

    Thanks!

    Austinsays:

    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 problem?

    Andres Ariassays:

    January 27, 2013 at 4:28 pm

    Good catch! I updated the code in user.php to use a more proper version of $this->db->where(), instead of concatenating strings. More

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://no/http://no/
  • 8/13/2019 Www Codefactorycr Com

    7/13

    info here: http://ellislab.com/codeigniter/user-guide/database/active_record.html

    Andres Ariassays:

    January 27, 2013 at 4:29 pm

    Hey Austin! Well, it could be an .htaccess problem, since by using form_open(verifylogin); in the

    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?

    andressays:

    January 28, 2013 at 9:44 pm

    hi, sorry for my english.

    how do I validate that a user is logged in, if I have multiple controllers.

    because when I click it back in my browser, and I logged out, I can see my application. Im trying to apply your example to my application

    but I could not get it right. Thank you.

    Andres Ariassays:

    January 28, 2013 at 10:53 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);

    }

    And to log out you can do this: $this->session->unset_userdata(logged_in);

    Cheers

    andressays:

    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.

    zulsays:

    January 28, 2013 at 11:28 pm

    Why i get this Error :

    An Error Was Encountered

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

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://ellislab.com/codeigniter/user-guide/database/active_record.html
  • 8/13/2019 Www Codefactorycr Com

    8/13

    I already follow your instruction. anybody can help me?

    Jeffsays:

    January 29, 2013 at 1:12 am

    Hi! I get this after following your tutorial:

    An Error Was Encountered

    Unable to load the requested class: database, session

    Can anyone help me? I was wondering what happened?

    Manoj Damkondwarsays:

    January 29, 2013 at 1:55 am

    Thanks man now I am starting with codeiniter its very useful for me..

    Prashanth.Maddursays:

    February 6, 2013 at 3:35 am

    its good for beginners.

    Morrissays:

    February 7, 2013 at 1:41 pm

    Thanks!!! this really help me a lot

    Christiaan Janssensays:

    February 8, 2013 at 1:24 am

    Awesome tutorial. Works like a charm!

    petersays:

    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 inchanges to bob.

    thanks in advance

    Andres Ariassays:

    February 10, 2013 at 9:23 pm

    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

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.shubhbandh.com/
  • 8/13/2019 Www Codefactorycr Com

    9/13

    Sebastiansays:

    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

    Vladimirsays:February 11, 2013 at 7:14 am

    I dont understand one row.

    This is in function check_database($password)

    $username = $this->input->post(username);

    Why you dont put too variable $password?

    fe. $password = $this->input->post(username);

    Thanks.

    Vladimirsays:

    February 11, 2013 at 7:16 am

    I made mistake. Sorry.

    I dont understand one row.

    This is in function check_database($password)

    $username = $this->input->post(username);

    Why you dont put too variable $password?

    fe. $password = $this->input->post(password);

    Thanks.

    jithu.majinusays:

    February 12, 2013 at 4:59 am

    realy it is good tutorial for beginners

    thank you

    pantassays:

    February 12, 2013 at 8:20 pm

    help me an error

    i try it on appserv 2.5.9, but i get this error..

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, [email protected] 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 ma be available in the server error lo .

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFmailto:[email protected]://[email protected]/
  • 8/13/2019 Www Codefactorycr Com

    10/13

    Apache/2.2.4 (Win32) PHP/5.2.3 Server at localhost Port 80

    please help me..

    angelsays:

    February 13, 2013 at 7:12 am

    thanks!

    Andres Ariassays:

    February 13, 2013 at 7:21 am

    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

    Andres Ariassays:

    February 13, 2013 at 7:23 am

    Hi Sebastian

    The url should be something like http://localhost/simple_login/index.php/login(or remove index.php if you have changed the config for this)

    Cheers

    Andres Ariassays:February 13, 2013 at 7:24 am

    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

    Gopikasays:

    February 13, 2013 at 11:13 pm

    Hi Andres

    Thank you for the tutorial. As a beginner with codeigniter, it was very helpful.

    I wanted to create this in both chinese and english.

    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.

    Any help will be appreciated.

    Thanks a lot.

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttps://github.com/EllisLab/CodeIgniter/wiki/CodeIgniter-2.1-internationalization-i18nhttp://localhost/simple_login/index.php/login
  • 8/13/2019 Www Codefactorycr Com

    11/13

    Barsays:

    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.

    funtisays:

    February 15, 2013 at 1:59 pm

    once i have comment this line

    redirect(home, refresh);

    than it works without any error message.

    but after login it will lead to index.php/verifylogin

    is it fine ?

    and what to do to redirect it to another page !

    arunasays:

    February 16, 2013 at 6:32 am

    Really good one . searching for simple sample application to learn codeigniter and found it here

    Scavanza Laziendosays:

    February 18, 2013 at 3:07 am

    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

    Jacksays:

    February 18, 2013 at 9:58 am

    Hi,

    Your tutorial was very helpful! However when I attempt to log in I get this error:

    The requested URL /LoginTut/login/VerifyLogin was not found on this server.

    Do you know what might be the problem?

    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!

    Nitishsays:

    February 19, 2013 at 4:25 am

    Thanks for the tutorial. I am getting the error

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: Verifylogin::$form_validation

    Filename: controllers/verifylogin.php

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://stackoverflow.com/questions/14940118/simple-login-system-using-codeigniter-returning-404-on-login#comment20967412_14940118http://www.jdiadt.com/http://vegaatechnologies.com/http://www.barisgunduz.com/
  • 8/13/2019 Www Codefactorycr Com

    12/13

    Line Number: 18

    line number 18 is

    $this->form_validation->set_rules(username,'Username,'trim|required|xss_clean);

    Andres Ariassays:

    February 19, 2013 at 7:36 am

    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

    Andres Ariassays:

    February 19, 2013 at 7:38 am

    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

    1 Pings/Trackbacks for "Simple Login with CodeIgniter in PHP"

    Simple Login using CodeIgniter & Database | imron02says:

    June 1, 2013 at 9:15 am

    [...] Very easy if you understand this. Full Code on Gist https://gist.github.com/Imron02/5690519Source: http://www.codefactorycr.com[...]

    Leave a Reply

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

    Name *

    E-mail*

    Website

    Comment

    Post CommentPost Comment

    converted by Web2PDFConvert.com

    http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.codefactorycr.com/https://gist.github.com/Imron02/5690519http://imron02.wordpress.com/2013/06/01/simple-login-using-codeigniter-database/
  • 8/13/2019 Www Codefactorycr Com

    13/13

    2013Code Factory Responsive Themepowered by WordPress

    Latest Blog Entries

    Create a test project with Selenium2, TestNG and Maven

    Custom Rules for JQuery Validation in Forms

    Simple Login with CodeIgniter in PHP

    Setting up Glassfish behind Apache

    Apache Tomcat and APR in a Linux Server

    http://wordpress.org/http://themeid.com/responsive-theme/http://www.codefactorycr.com/http://cr.linkedin.com/in/aarias/http://www.facebook.com/pages/Codefactorycr/153581951341581http://twitter.com/codefactorycrhttp://www.dreamhost.com/r.cgi?493459http://www.codefactorycr.com/apache-tomcat-and-apr-in-a-linux-server.htmlhttp://www.codefactorycr.com/glassfish-behind-apache.htmlhttp://www.codefactorycr.com/custom-rules-jquery-validation.htmlhttp://www.codefactorycr.com/test-project-selenium2-testng-maven.html