www.webdeveloper.com
Results 1 to 12 of 12

Thread: Help with basic login form

  1. #1
    Join Date
    Dec 2013
    Posts
    57

    Help with basic login form

    I have created this basic login form and I'm using this login script from https://github.com/panique/php-login...sses/Login.php on my localhost
    HTML Code:
    <?php
    require 'classLogin.php';
    if (isset($_POST['submit'])) {
    loginWithPostData($_POST['user_name'], $_POST['user_password']);
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
      <head>
    <title>Login</title>
      </head>
    
      <body>
    
        <div class="container">
    
          <form method="post">
            <h2>Please sign in</h2>
            <input type="text" class="form-control" name="user_name" placeholder="Email address" value="<?php if(isset($_POST['user_name'])) { echo ($_POST['user_name']);} ?>" >
            <input type="password" class="form-control" name="user_password" placeholder="Password" autofocus>
            <button name="submit" type="submit">Sign in</button>
          </form>
    
        </div>
    
    
    
    
      </body>
    </html>
    I receive this error message upon hitting the submit button "Fatal error: Call to undefined function loginWithPostData() in /form.php on line 4

    I am sort of new to this so my question is how do I make calls to the functions so the credentials can be validated?

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    The function is a method of a class. You will need to initialize the class in order to use the function (method).
    To initialize a class you do so by using the new operator and calling the class like a function.
    PHP Code:
    <?php
    class Foo
    {
        function 
    __construct()
        {
            
    // the __construct() or constructor is called when initializing a class (if it has one).
        
    }
    }
    $foo = new Foo(); // initialize Foo
    // now we can access any public properties (variables) and/or methods (functions)
    ?>

  3. #3
    Join Date
    Apr 2008
    Location
    Earth
    Posts
    31
    Piggybacking off of the previous post... You have the function name wrong as well. And you are not calling the function correctly.

    Initialize the class as explained above.Your code should look something like this:

    PHP Code:

    require 'classLogin.php';
    $classlogin = New Login;

    $classlogin->dologinWithPostData(); 

    Also, looking at that function does not require any parameters... Not sure why you are passing the username and password. Perhaps I am looking at the wrong class?

  4. #4
    Join Date
    Dec 2013
    Posts
    57
    Quote Originally Posted by dclamp View Post
    Also, looking at that function does not require any parameters... Not sure why you are passing the username and password. Perhaps I am looking at the wrong class?
    What do you mean by that?

  5. #5
    Join Date
    Dec 2013
    Posts
    57
    Here is the updated code I used but I still receive an error.

    HTML Code:
    <?php
    require 'classLogin.php';
    $classLogin = new Login;
    
    
    if (isset($_POST['submit'])) {
    $classLogin->doLoginWithPostData();
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
      <head>
    <title>Login</title>
      </head>
    
      <body>
    
        <div class="container">
    
          <form method="post">
            <h2>Please sign in</h2>
            <input type="text" class="form-control" name="user_name" placeholder="Email address" value="<?php if(isset($_POST['user_name'])) { echo ($_POST['user_name']);} ?>" >
            <input type="password" class="form-control" name="user_password" placeholder="Password" autofocus>
            <button name="submit" type="submit">Sign in</button>
          </form>
    
        </div>
    
    
    
    
      </body>
    </html>

    This is the error message I get "Fatal error: Call to undefined method Login::doLoginWithPostData() in /form.php on line 7"

    I left the __construct() exactly how it was on the github link. Was I suppose to pass some arguments between the ( )?

  6. #6
    Join Date
    Dec 2013
    Posts
    57
    The reason I was getting an undefined method was because the the method was loginWithPostData() instead of doLoginWithPostData().

    I corrected it but when I hit the submit button without filling out any of the fields I get the same screen appearing and the errors do not show up. I also try entering a correct user and password but the same screen reappears. Can someone take a look at this code and let me know where I might have messed up at.
    Code:
         public function loginWithPostData()
         {
             // if POST data (from login form) contains non-empty user_name and non-empty user_password
             if (!empty($_POST['user_name']) && !empty($_POST['user_password'])) {
     
                 // create a database connection, using the constants from config/db.php (which we loaded in index.php)
                 $this->db_connection = new mysqli('localhost', 'root', '1', 'test');
     
                 // change character set to utf8 and check it
                 if (!$this->db_connection->set_charset("utf8")) {
                     printf("Error loading character set utf8: %s\n", $this->db_connection->error);
                 }
     
                 // if no connection errors (= working database connection)
                 if (!$this->db_connection->connect_errno) {
     
                     // escape the POST stuff
                     $this->user_name = $this->db_connection->real_escape_string($_POST['user_name']);
     
                     // database query, getting all the info of the selected user
                     $sql = "SELECT user_name, user_password
                             FROM users
                             WHERE user_name = '" . $this->user_name . "';";
                     $checklogin = $this->db_connection->query($sql);
     
                     // if this user exists
                     if ($checklogin->num_rows == 1) {
     
                         // get result row (as an object)
                         $result_row = $checklogin->fetch_object();
     
                         // using PHP 5.5's password_verify() function to check if the provided passwords fits to the hash of that user's password
                         if ($_POST['user_password'] === $result_row->user_password) {
     
                             // write user data into PHP SESSION [a file on your server]
                             $_SESSION['user_name'] = $result_row->user_name;
                             $_SESSION['user_email'] = $result_row->user_email;
                             $_SESSION['user_logged_in'] = 1;
     
                             // set the login status to true
                             $this->user_is_logged_in = true;
     
                         } else {
                             $this->errors[] = "Wrong password. Try again.";
                         }
                     } else {
                        $this->errors[] = "This user does not exist.";
                     }
                 } else {
                     $this->errors[] = "Database connection problem.";
                 }
             } elseif (empty($_POST['user_name'])) {
                $this->errors[] = "Username field was empty.";
            } elseif (empty($_POST['user_password'])) {
                 $this->errors[] = "Password field was empty.";
             }
             echo $this->errors;
         }
    It is almost the same as the original code except that I added the last line "echo $this->errors;" I also removed the passwordverify() function from the code.

  7. #7
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    I looked at the github files more closely. Did you go thru the process of installing it (e.g. setting up the config file and installing the MySQL tables)?
    In my honest opinion, the script itself is garbage. It uses the built-in functions for session managements (in conjunction with a database). But uses PHP version checking (which should not be there, due to low-level functionality of the script). unnecessary version enforcing + low-level functionality = bad portability = bad programming.

  8. #8
    Join Date
    Dec 2013
    Posts
    57
    Quote Originally Posted by ShrineDesigns View Post
    I looked at the github files more closely. Did you go thru the process of installing it (e.g. setting up the config file and installing the MySQL tables)?
    In my honest opinion, the script itself is garbage. It uses the built-in functions for session managements (in conjunction with a database). But uses PHP version checking (which should not be there, due to low-level functionality of the script). unnecessary version enforcing + low-level functionality = bad portability = bad programming.
    I didnt set up any config files. I just copied the login script from Github then connected to a database table I created through phpmyadmin.

    Do you have any recommendations for a different script or would it be best if I just created through scratch?

  9. #9
    Join Date
    Apr 2008
    Location
    Earth
    Posts
    31
    I never use any 3rd party scripts (besides well known classes such as FPDF and the like).

    I prefer to write all my code myself because I know exactly how it works and how to trouble shoot it. I know what every line means and the purpose behind it. Using someone elses code you cant always ensure they are programming with your best interest in mind. Security is the most important thing. If you use someone elses script, you cannot confirm that it is secure and is handling data correctly.

    I suggest you come up with your own login script.

  10. #10
    Join Date
    Dec 2013
    Posts
    57
    Quote Originally Posted by dclamp View Post
    I never use any 3rd party scripts (besides well known classes such as FPDF and the like).

    I prefer to write all my code myself because I know exactly how it works and how to trouble shoot it. I know what every line means and the purpose behind it. Using someone elses code you cant always ensure they are programming with your best interest in mind. Security is the most important thing. If you use someone elses script, you cannot confirm that it is secure and is handling data correctly.

    I suggest you come up with your own login script.
    Okay thanks, I am new to oop in php so could you recommend a website that can teach me about writing scripts and best practices? I just need something to get me started so I can have an idea on where to begin.

  11. #11
    Join Date
    Apr 2008
    Location
    Earth
    Posts
    31
    Google found this: http://code.tutsplus.com/tutorials/o...ers--net-12762

    That tutorial looks like a good place to start.

  12. #12
    Join Date
    Feb 2013
    Posts
    105
    You don't have to write a login script in OOP, you still can do it the procedural way. I wrote a basic login/registration tutorial and even have a demo page: http://www.pepster.com/PD0_demo/register.php. I feel it's pretty secure and I'm pretty sure you could modify it to fit your needs, if you choose to do so.
    Last edited by Strider64; 05-09-2014 at 03:34 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles