PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Dec 11, 2017 12:47 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri Sep 29, 2017 5:07 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 178
Php Folks,

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

//Which of the following to use and why that one over the other 2 ?
$row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 2 of the next 2 ?
//$row = mysqli_fetch_array($query, MYSQLI_ASSOC); //Which line to use ? This line or the one above this lone or the one below this line ?
//$row = mysqli_fetch_array($result, MYSQLI_ASSOC);



It is a Log In Page script where the users are given a choice to either login to their accounts by typing their "username" or "email".

Syntax: [ Download ] [ Hide ]
<?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 (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", "@"))
                {
                        $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); //Which line to use ? This line or the next ?
                        $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // Which line to use ? This line or the one above ?
                }
                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_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status);
                }              
               
                //Which of the following 3 to use and why that one over the other 2 ?
                $row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 2 of the next 2 ?
                //$row = mysqli_fetch_array($query, MYSQLI_ASSOC); //Which line to use ? This line or the one above this lone or the one below this line ?
                //$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
               
                mysqli_stmt_close($stmt);
               
                printf("%s (%s)\n",$row["usernames"],$row["passwords"]);
               
                if ($result == false)
                {
                        echo "No result!";// 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
                {
               
                        if (password_verify($password, $db_password))          
                        {
                                echo "IF triggered for password_verify! password_verify ok"; // For debugging purpose!
                       
                        $_SESSION["user"] = $username;
                        header("location:home.php?user=$username");                            
                }
                else
                {
                        echo "Incorrect User Credentials !';<br>";
                        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="">
                <br>
                <label for="login_pass">Password:</label>
                <input type="password" name="login_password" id="login_pass" value="">
        </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>
 

 


Top
 Profile  
 
PostPosted: Sat Sep 30, 2017 8:40 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
You're using prepared statements and bound variables, so you'll want to use mysql_stmt_fetch

http://php.net/manual/en/mysqli-result.fetch-array.php
http://php.net/manual/en/mysqli-stmt.fetch.php

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sat Sep 30, 2017 8:55 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 178
Celauran wrote:
You're using prepared statements and bound variables, so you'll want to use mysql_stmt_fetch

http://php.net/manual/en/mysqli-result.fetch-array.php
http://php.net/manual/en/mysqli-stmt.fetch.php


Thank you!


Top
 Profile  
 
PostPosted: Sat Sep 30, 2017 8:56 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 178
Folks,

Do you mind reviewing my code in my original post and edit wherever you deem editing is necessary ?
I prefer 2 versions where on one you use the mysqli_stmt_fetch and on the other mysqli_fetch_array.
That way, we newbies learn both from you with your examples.


Top
 Profile  
 
PostPosted: Sun Oct 01, 2017 8:25 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
Here's a thought. Rather than having us write up two versions, why don't you do it? Exercise for yourself and we can comment on the finished product if you like. Keep in mind, I have already reviewed two separate versions of this code and you ignored all my suggestions, so I'm not in a hurry to invest more time in that.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Oct 04, 2017 8:48 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 178
Celauran wrote:
Here's a thought. Rather than having us write up two versions, why don't you do it? Exercise for yourself and we can comment on the finished product if you like. Keep in mind, I have already reviewed two separate versions of this code and you ignored all my suggestions, so I'm not in a hurry to invest more time in that.


Can you remind me where you did that ? No offense, but I've been busy lately and you know how it is. You forget things. Things slip by.


Top
 Profile  
 
PostPosted: Wed Oct 04, 2017 6:51 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
UniqueIdeaMan wrote:
Celauran wrote:
Here's a thought. Rather than having us write up two versions, why don't you do it? Exercise for yourself and we can comment on the finished product if you like. Keep in mind, I have already reviewed two separate versions of this code and you ignored all my suggestions, so I'm not in a hurry to invest more time in that.


Can you remind me where you did that ?

viewtopic.php?f=1&t=144266

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sat Oct 14, 2017 5:38 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 178
Thanks for reminding.
I have finished my login.php but I will review it again cross referencing it with your suggestions again for the final time before I finish the project.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group