[RESOLVED] login validation oop
Results 1 to 5 of 5

Thread: [RESOLVED] login validation oop

  1. #1
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490

    resolved [RESOLVED] login validation oop

    Well I went back to the idea I had awhile ago, I thought I'd improved enough to try it now, apparently not. I'm trying to validate a login form however I can't get the failed validation to show, I've only attempted email so far

    PHP Code:
    class Login
    {    
        private 
        
    $email,
        
    $password,
        
    $database,
        
    $db null,
        
    $validEmail;

        public function 
    __construct()
        {
            
    $this->db = new Database;
        }
        
        public function 
    validEmail($email)
        {    
            return (
    filter_var($emailFILTER_VALIDATE_EMAIL) != self::$validEmail);
        }    

    PHP Code:
    require "loadclasses.php";
    $database = new Database;
    session_start();


        if (
    $_SERVER['REQUEST_METHOD'] == 'POST')
        {
            
    $email $pass "";
            
    $post filter_input_array(INPUT_POSTFILTER_SANITIZE_STRING);        
            
            
    $email $post['email-login'];
            
    $pass $post['password-login'];
            
    $errors = array();

            
    $fields = array( 
            
    'email' => array( 
                
    'validating' => 'validEmail'
                
    'failed'   => 'Please enter a valid email'
            )
            );  

            foreach(
    $post as $key => $value
            {
                  if(isset(
    $fields[$key])) 
                {
                    if(!
    Login::{$fields[$key]['validating']}($value)) 
                    {
                        
    $errors[] = ['name' => $key'error' => $fields[$key]['failed']];
                    }
                }
            }
            
            if(empty(
    $errors)) 
            {
                 
    $success = ['response' => 'true']; 
            }   
            
        }

        
        
    header('Content-Type: application/json');
    if (empty(
    $errors))
    {
        echo 
    json_encode($success);
    }
    else
    {
        echo 
    json_encode(["errors" => $errors]);


    PHP Code:
    $("#login").on("submit",
        function(
    event)
        {
            
    event.preventDefault();
            $(
    "span.error").empty()
            $(
    "span.success").empty() 
            $.
    post('loginForm.php', $(this).serialize(),
            function(
    data)
            {
                if (
    data.fail)
                {
                    $(
    ".fail").append(data.fail)
                }
                else
                if (!
    data.errors)
                {
                    $(
    ".success").append(data.message// deal with a no-error response ( all is good)
                
    }
                else
                {
                    $.
    each(data.errors,
                    function (
    idatum)
                    {
                        
    console.log(data);
                        $(
    "[name='" datum.name "']").next().html(datum.error)
                    })
                }
            }
            , 
    'json');
        }); 
    PHP Code:
    <ul class="indexList">
                <
    li class="logInList">
                    
    Login/Register
                
    </li>
                <
    li class="showLogin">
                    <
    ul>
                        <
    form method="POST" class="login-center" id="login">
                        <
    li>
                             <
    span class="fail"> </span>
                        </
    li>
                        
                        <
    li>
                        <
    div class="form-group">
                            <
    label for='email' class="registerLabel">E-mail</label>    
                            <
    input type="text" class="form-control" id="emailAddress" name="email-login" aria-describedby="emailHelp" placeholder="Enter email">
                            <
    span class="error"></span>
                        </
    div>
                        </
    li>
                        
                        <
    li>
                        <
    div class="form-group">
                            <
    label for='password' class="registerLabel">Password</label>
                            <
    input type="password" class="form-control" id="password" name="password-login" aria-describedby="password" placeholder="Password">
                            <
    span class="error"></span>
                        </
    div>
                        </
    li>
                        <
    li>
                        <
    div class="form-check">
                            <
    input type="submit" value="Submit">
                            <
    span class="success"> </span>    
                        </
    div>
                        </
    li>
                        </
    form>    
                        <
    li>
                        <
    div class="form-check">
                            <
    a href="register.php">Need an account?</a>
                        </
    div>
                        </
    li>
                    </
    ul>
                </
    li>
            </
    ul
    Last edited by cluelessPHP; 09-09-2017 at 03:56 PM.
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  2. #2
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490
    I guess I can do it like this...

    PHP Code:

    class Login
    {    
        private 
        
    $email,
        
    $password,
        
    $database,
        
    $db null;

        private static
        
    $validEmail;

        public function 
    __construct()
        {
            
    $this->db = new Database;
        }
        
        public static function 
    validEmail($email)
        {    
            return (
    filter_var($emailFILTER_VALIDATE_EMAIL) != self::$validEmail);
        }    

    It seems wrong some how but it works
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,881
    As far as I can see, since you do not initialize Login::$validEmail with any value, you're just doing a != comparison to NULL. So why not just cut out the middleman and do:
    PHP Code:
        return (filter_var($emailFILTER_VALIDATE_EMAIL) !== false); 
    ?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490
    Quote Originally Posted by NogDog View Post
    As far as I can see, since you do not initialize Login::$validEmail with any value, you're just doing a != comparison to NULL. So why not just cut out the middleman and do:
    PHP Code:
        return (filter_var($emailFILTER_VALIDATE_EMAIL) !== false); 
    ?
    Mostly because I was lying to myself , I kinda knew I would be tripped up a few minutes later. I remeber awhile ago you had written something along the lines of..

    PHP Code:
    $email $pass "";
            
    $post filter_input_array(INPUT_POSTFILTER_SANITIZE_STRING);        
            
            
    $email $post['email-login'];
            
    $pass $post['password-login'];
            
            
    $errors = array();

            
    $fields = array(
              
    'email-login' => array
               (
                 
    'validator' => 'Please enter a valid email',
                 
    'message'   => 'invalid username'
                  
    )
            );
            foreach(
    $_POST as $key => $value
            {
            if(isset(
    $fields[$key])) 
                {
                    if(!
    Login::{$fields[$key]['validator']}($value)) 
                    {
                        
    $errors[] = ['name' => $key'error' => $fields[$key]['message']];
                    }
                }
            } 
    But without using public static but I couldn't find it, just sitting down to search online for some ideas
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  5. #5
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490
    Well I "think" I've figured it out eventually

    PHP Code:
    <?php 
    require "loadclasses.php";

        if (
    $_SERVER['REQUEST_METHOD'] == 'POST')
        {
            
    $email $pass "";
            
    $post filter_input_array(INPUT_POSTFILTER_SANITIZE_STRING);        
            
            
    $email $post['email-login'];
            
    $pass $post['password-login'];
            
            
    $errors = array();

            
    $fields = array(
              
    'email-login' => array
               (
                 
    'message'   => 'Please enter a valid email'
                  
    )
            );

            foreach(
    $fields as $key => $value
            {
                
    $errors[] = ['name' => $key'error' => $fields[$key]['message']];
            }


        

            if(empty(
    $errors)) 
            {
                 
    $success = ['response' => 'true']; 
                 
    session_start();
            }   
            
        }

        
        
    header('Content-Type: application/json');
    if (empty(
    $errors))
    {
        echo 
    json_encode($success);
    }
    else
    {
        echo 
    json_encode(["errors" => $errors]);
    }
    PHP Code:
    <?php

    error_reporting
    (-1); // reports all errors    
    ini_set("display_errors""1"); // shows all errors    
    ini_set("log_errors"1);    
    ini_set("error_log""login.log");  
    require (
    "Database.class.php");

        
    class 
    Login
    {    
        private 
        
    $email,
        
    $password,
        
    $database,
        
    $db null;

        public function 
    __construct()
        {
            
    $this->db = new Database;
        }
        
        public function 
    validEmail($email)
        {    
            return (
    filter_var($emailFILTER_VALIDATE_EMAIL) !== false);  
        }    
    }
    I still need to add stuff to it but I got the idea from

    http://php.net/manual/en/language.oop5.iterations.php
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

Thread Information

Users Browsing this Thread

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

Posting Permissions

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