Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Login With Username or Email And Password - Cleaning Up The Reg Page

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.52k
    Answer it

    Php Folks!

    You know others in other forums suggest I use regex but yet again they never provide sample or I reckon many don't know the regex.
    I will mention your sample on other forum and link back to this thread. Hopefully, a lot of newbies would flock this way to learn more regex.
    In the meanwhile since I am a little weak on php, do you mind adding the following regex code on my next following code so that I start getting an idea (and any future newbies too that drop-in here) how regex fits into my code or anywhere atall ? hug:

     

    function valid_email($email) { 
        if(preg_match('/^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$/',$email)) { 
            return TRUE; 
        } else { 
            return FALSE; 
        } 
    }  

     

    You will notice that, I have a few questions on my comments where I show confusion on how to proceed forward.
    At one place, in the comments, I will ask which 1 of the following 2 I should use which will suit the context of my code well.
    I commented-out the one that I personally thought did not fit into my codes' context. But, I need your professional opinion.

    //$result = mysqli_stmt_get_result($stmt); //Use either this line (if you need to get all data of the array without associating them to variables like you do with mysqli_stmt_bind_result), or ...
    
    $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // ... this line. But not both.

     

    At one point, I was not sure which 1 of the following 3 to use. I commented-out the 2 that I personally thought did not fit in to my codes' context. But I need your professional opinion here too.

    $row = mysqli_stmt_fetch($stmt);
    //$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
    //$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

     

     

    login.php

    <?php
     
    /*
    ERROR HANDLING
    */
    declare(strict_types=1);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
     
    include 'config.php';
     
    // check if user is already logged in
    if (is_logged() === true) 
    {
        //Redirect user to homepage page after 5 seconds.
        header("refresh:2;url=home.php");
        exit; //
    }
    
    
    if ($_SERVER['REQUEST_METHOD'] == "POST")
    { 
        if (isset($_POST["login_username_or_email"]) && isset($_POST["login_password"]))
        {
            $username_or_email = trim($_POST["login_username_or_email"]); //
            $password = $_POST["login_password"];        
             
            //Select Username or Email to check against Mysql DB if they are already registered or not.
            $stmt = mysqli_stmt_init($conn);
            
            if(strpos("$username_or_email", "@") == true)
            {
                $email = $username_or_email;
                $username = "";
                
                $query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE emails = ?";
                $stmt = mysqli_prepare($conn, $query);            
                mysqli_stmt_bind_param($stmt, 's', $email);
                mysqli_stmt_execute($stmt);
                //$result = mysqli_stmt_get_result($stmt); //Use either this line (if you need to get all data of the array without associating them to variables like you do with mysqli_stmt_bind_result), or ...
                //Note from line below that the variables "$db_username", "$db_account_activation_status" are related to the tbl columns selected on $query ("SELECT ids, usernames, accounts_activations_statuses From users .. WHERE).
                $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // ... this line. But not both.
            }
            else
            {
                $username = $username_or_email;
                $email = "";
                $query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE usernames = ?";
                $stmt = mysqli_prepare($conn, $query);
                mysqli_stmt_bind_param($stmt, 's', $username);
                mysqli_stmt_execute($stmt);
                //$result = mysqli_stmt_get_result($stmt); //Use either this line (if you need to get all data of the array without associating them to variables like you do with mysqli_stmt_bind_result), or ...
                //Note from line below that the variables "$db_email", "$db_account_activation_status" are related to the tbl columns selected on $query ("SELECT ids, emails, accounts_activations_statuses From users .. WHERE).
                $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // ... this line. But not both.
            }           
            
            //Which of the following to do and why that one over others ?
            $row = mysqli_stmt_fetch($stmt);
            //$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
            //$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
            
            mysqli_stmt_close($stmt);
            
            printf("%s (%s)\n",$row["usernames"],$row["passwords"]);
            
            if ($result == false)
            {
                echo "'$result == false' on line 73! IF got triggered on line 73! !<br>";// For debugging purpose!
                exit();
            }
            elseif ($row['accounts_activations_statuses'] == '0')
            {
                {
                    echo "You have not activated your account yet! Check your email for instructions on how to activate it. 
                    Check your spam folder if you don't find an email from us.";
                    exit();
                }
            }
            else
            {
                echo "'$result == true' on line 73! Else got triggered on line 86! <br>";// For debugging purpose!
            }
            
            if (password_verify($password, $db_password))        
            {
                echo "IF triggered for password_verify! password_verify ok";
                //If 'Remember Me' check box is checked then set the cookie. 
                //if(!empty($_POST["login_remember"])) // Either use this line ....
                if (isset($_POST['login_remember']) && $_post['login_remember'] == "on") // ...or this line. But not both!
                {
                    setcookie("login_username", $username, time()+ (10*365*24*60*60));
                }
                else
                {
                    //If Cookie is available then use it to auto log user into his/her account!
                    if (isset($_COOKIE['login_username']))
                    {
                        setcookie("login_username","","");
                    }
                }
                $_SESSION["user"] = $username;
                header("location:home.php?user=$username");                
            }
            else
            {
                echo "Else got triggered on line 111: Incorrect User Credentials ! 'password_verify = FALSE';<br>";// For debugging purpose!
                exit();
            }
        }
    }
        
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    <title><?php $site_name?> Member Login Page</title>
      <meta charset="utf-8">
    </head>
    <body>
    <form method="post" action="">
        <h3><?= $site_name ?> Member Login Form</h3>
        <fieldset>
            <label for="login_name">Username/Email:</label>
            <input type="text" name="login_username_or_email" id="login_name" value="<?php if(isset($_COOKIE["login_username_or_email"])) echo $_COOKIE["login_username_or_email"]; ?>"</center>
            <br>
            <label for="login_pass">Password:</label>
            <input type="password" name="login_password" id="login_pass" value="<?php if(isset($_COOKIE["login_password"])) echo $_COOKIE["login_password"]; ?>"></center>
        </fieldset>
        <div class="submitsAndHiddens">
            <label for="login_remember">Remember Login Details:</label>
            <input type="checkbox" name="login_remember" />
            <br>
            <button type="submit">Login</button>
            <br>
            <a href="login_password_reset.php">Forgot your Password ? Reset it here!</a>
            <br>
            <a href="register.php">Register here!</a>
        </div>
    </form>
    
    </body>
    </html>
    
    [/php]

     

    Finally, do you or anyone mind adding the cookie section so that when the user checks the "Remember Me" checkbox then the cookie gets set to auto login the user when next time they visit the login.php page or the page this php code is running on.
    Even though I have read on how to use the following, I still get confused a little and when you got tonnes of lines of codes you tend to get a little puzzled. You guys' inputs would be helpful and would remove any doubts from my mind.

    [B]mysqli_fetch_array
    mysqli_stmt_get_result($stmt)
    mysqli_stmt_bind_result($stmt)
    cookies[/B]


    You know how it is. Reading & learning is one thing and seeing in practice how it is done in a project, is another thing. And it is called: Work Experience.

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: