/    Sign up×
Community /Pin to ProfileBookmark

Why If Not Statement Fetch Condition Fails ?

Folks,

Look at this login.php.
It works if I remove the following:

[code]
if (!mysqli_stmt_fetch($stmt_1))
{
echo “Incorrect log-in details1!<br>”;
mysqli_stmt_close($stmt_1);
exit();
}

[/code]

And remove the following:

[code]
if (!mysqli_stmt_fetch($stmt_2))
{
echo “Incorrect log-in details2!<br>”;
mysqli_stmt_close($stmt_2);
exit();
}
[/code]

Else, I get following error, even if login inputs are correct:
Incorrect log-in details1!

Why is that ?
Full code:

[code]
<?php

//Required PHP Files.
include ‘config.php’; //Required on all webpages of the site.

//Check if User is already logged-in or not. Get the login_check() FUNCTION to check.
if (login_check() === TRUE)
{
//Redirect User to Log-in Page immediately.
//header(“refresh:0; url=home.php”);
header(“location:home.php?user=$user”);
exit();
}

if (isset($_POST[“login_username_or_email_or_domain”]) && isset($_POST[“login_password”]))
{
$login_username_or_email_or_domain = trim($_POST[“login_username_or_email_or_domain”]);
$login_password = $_POST[“login_password”];

//Check if User inputted Username/Email exist in db. Registered or not.
//Select Username or Email to check against Mysql DB if they are already registered or not.
if(strpos(“login_username_or_email_or_domain”, “@”))
{
$querying_column = “primary_website_email”;
}
elseif(strpos(“login_username_or_email_or_domain”, “.”))
{
$querying_column = “primary_website_domain”;
}
else
{
$querying_column = “username”;
}
//Make sure the users table has atleast these 3 columns: username,primary_website_domain and primary_website_email due to the $querying_column variable which could hold any of these 3 values.
$query_1 = “SELECT id,account_activation_status,id_video_verification_status,sponsor_username,username,password,primary_website_domain,primary_website_email,first_name,middle_name,surname,gender,age_range,country FROM users WHERE $querying_column = ?”;
$stmt_1 = mysqli_prepare($conn, $query_1);
mysqli_stmt_bind_param($stmt_1, ‘s’, $login_username_or_email_or_domain);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$db_id,$db_account_activation_status,$db_id_video_verification_status,$db_sponsor_username,$db_username,$db_password,$db_primary_website_domain,$db_primary_website_email,$db_first_name,$db_middle_name,$db_surname,$db_gender,$db_age_range,$db_country);
mysqli_stmt_fetch($stmt_1);
if (!mysqli_stmt_fetch($stmt_1))
{
echo “Incorrect log-in details1!<br>”;
mysqli_stmt_close($stmt_1);
exit();
}
mysqli_stmt_close($stmt_1);
//Free Result_1 Set
mysqli_stmt_free_result($stmt_1);

$query_2 = “SELECT blog,personal_email,personal_mobile,home_zip,home_town,home_borough,home_city,home_county,home_region,home_state,home_country,business_name,business_email,business_phone,business_zip,business_town,business_borough,business_city,business_county,business_region,business_state,business_country FROM contact_details WHERE id = ?”;
$stmt_2 = mysqli_prepare($conn, $query_2);
mysqli_stmt_bind_param($stmt_2, ‘s’, $db_id);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_bind_result($stmt_2,$db_blog,$db_personal_email,$db_personal_mobile,$db_home_zip,$db_home_town,$db_home_borough,$db_home_city,$db_home_county,$db_home_region,$db_home_state,$db_home_country,$db_business_name,$db_business_email,$db_business_phone,$db_business_zip,$db_business_town,$db_business_borough,$db_business_city,$db_business_county,$db_business_region,$db_business_state,$db_business_country);
mysqli_stmt_fetch($stmt_2);
if (!mysqli_stmt_fetch($stmt_2))
{
echo “Incorrect log-in details2!<br>”;
mysqli_stmt_close($stmt_2);
exit();
}
mysqli_stmt_close($stmt_2);
//Free Result_2 Set
mysqli_stmt_free_result($stmt_2);

if (!password_verify($login_password, $db_password))
{
echo “Incorrect log-in details3<br>”;
exit();
}
else
{
$user = $db_username;

$_SESSION[“user”] = $user;
$_SESSION[“id”] = $db_id;
$_SESSION[“account_activation_status”] = $db_account_activation_status;
$_SESSION[“id_video_verification_status”] = $db_id_video_verification_status;
$_SESSION[“id_video_verification_url”] = $db_id_video_verification_url;
$_SESSION[“sponsor_username”] = $db_sponsor_username;
$_SESSION[“recruits_number”] = $db_recruits_number;
$_SESSION[“primary_website_domain”] = $db_primary_website_domain;
$_SESSION[“primary_website_email”] = $db_primary_website_email;
$_SESSION[“username”] = $db_username;
$_SESSION[“first_name”] = $db_first_name;
$_SESSION[“middle_name”] = $db_middle_name;
$_SESSION[“surname”] = $db_surname;
$_SESSION[“gender”] = $db_gender;
$_SESSION[“date_of_birth”] = $db_date_of_birth;
$_SESSION[“age_range”] = $db_age_range;
$_SESSION[“religion”] = $db_religion;
$_SESSION[“education”] = $db_education;
$_SESSION[“profession”] = $db_profession;
$_SESSION[“marital_status”] = $db_marital_status;
$_SESSION[“working_status”] = $db_working_status;
$_SESSION[“home_town”] = $db_home_town;
$_SESSION[“home_borough”] = $db_home_borough;
$_SESSION[“home_city”] = $db_home_city;
$_SESSION[“home_county”] = $db_home_county;
$_SESSION[“home_region”] = $db_home_region;
$_SESSION[“home_state”] = $db_home_state;
$_SESSION[“home_country”] = $db_home_country;
include ‘sessions.php’;

header(“location:home.php?user=$user”);
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title><?php $site_name ?> Member Login Page</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
</head>
<body>
<form name = “login_form” method = “post” action=”” enctype = “multipart/form-data”>
<h3><?php echo “$site_name”;?> Member Login Form</h3>
<fieldset>
<label for=”login_name”>Username/Email:</label>
<input type=”text” name=”login_username_or_email_or_domain” id=”login_name” required [A-Za-z0-9] autocorrect=off value=””><br>
<label for=”login_pass”>Password:</label>
<input type=”password” name=”login_password” id=”login_pass” required [A-Za-z0-9] autocorrect=off value=””>
</fieldset>
<div class=”SubmitsAndHiddens”>
<label for=”login_remember”>Remember Log-in Details:</label>
<input type=”checkbox” name=”login_remember” id=”login_remember” />
<br>
<p align=”left”><input type=”submit” class=”btn btn-default” name=”submit” value=”Log-in!”></p>
<br>
<a href=”login_password_reset.php”>Forgot your password ? Reset it here!</a>
<br>
<a href=”register.php”>Don’t yet have an account ? Register here!</a>
</div>
</form>
</body>
</html>

<?php

include ‘footer.php’;

?>
[/code]

One of the Included files define the $user.

Guys,

Looking at my login.php, can someone be kind enough to add the cookie code so I can learn from your way of coding ? Your style.

Thanks!

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 15.2018 — That's because when you do the mysqli_stmt_fetch() right before it, it fetches one result row (if there is one), and then moves the result pointer **to the next result row** (if there is one). If there is only one result row, then the second call will return false, since there's no second result. Depending on whether or not you want to actually do anything with the result data, maybe just assign the result of the first call to a variable, then test that variable to see if it's false?
[code=php]
$result_1 = mysqli_stmt_fetch($stmt_1);
if($result_1 == false) {
// handle no-match-found condition
}
else {
// query results now in array $result if you need it
}
[/code]
Copy linkTweet thisAlerts:
@ginerjmOct 15.2018 — As has been pointed out - one has to assign the contents of the fetch call to SOMETHING. What were you going to do with your current code when you made the fetch call and had no data to work with?
Copy linkTweet thisAlerts:
@rootOct 16.2018 — Erm... TBH honest here...

A session that contains this many elements $_SESSION["user"] = $user;
$_SESSION["id"] = $db_id;
$_SESSION["account_activation_status"] = $db_account_activation_status;
$_SESSION["id_video_verification_status"] = $db_id_video_verification_status;
$_SESSION["id_video_verification_url"] = $db_id_video_verification_url;
$_SESSION["sponsor_username"] = $db_sponsor_username;
$_SESSION["recruits_number"] = $db_recruits_number;
$_SESSION["primary_website_domain"] = $db_primary_website_domain;
$_SESSION["primary_website_email"] = $db_primary_website_email;
$_SESSION["username"] = $db_username;
$_SESSION["first_name"] = $db_first_name;
$_SESSION["middle_name"] = $db_middle_name;
$_SESSION["surname"] = $db_surname;
$_SESSION["gender"] = $db_gender;
$_SESSION["date_of_birth"] = $db_date_of_birth;
$_SESSION["age_range"] = $db_age_range;
$_SESSION["religion"] = $db_religion;
$_SESSION["education"] = $db_education;
$_SESSION["profession"] = $db_profession;
$_SESSION["marital_status"] = $db_marital_status;
$_SESSION["working_status"] = $db_working_status;
$_SESSION["home_town"] = $db_home_town;
$_SESSION["home_borough"] = $db_home_borough;
$_SESSION["home_city"] = $db_home_city;
$_SESSION["home_county"] = $db_home_county;
$_SESSION["home_region"] = $db_home_region;
$_SESSION["home_state"] = $db_home_state;
$_SESSION["home_country"] = $db_home_country;
include 'sessions.php';


SHOULD be data pushed to a database record and not as a huge chunk of data, Sessions really are meant to be passing small amounts of information page to page, you would likely be best pushing data to a data storage area, get a reference to that passed as the $_SESSION value so that it can be retrieved on next page rendering.

So instead of all this ping-pong with the server, you are sending data in parts to complete a database record that can be more reliably dealt with.

Also, when you are using sessions, you have to on the first available line to declare the session session_start() before you use any $_SESSION variables.

You have no indication of a session starting or being checked if a session is running or not...
Copy linkTweet thisAlerts:
@ginerjmOct 16.2018 — I heartily agree with Root. Placing this much data into the SESSION array tells me that something is not that well-designed. You currently have a huge number of items being saved. Perhaps a re-think is in order?

Oh - I just realized who I am writing to - He-Who-Will-Not-Learn.
Copy linkTweet thisAlerts:
@site-developerauthorOct 26.2018 — @root#1596840

I have it in one of the included files like this:
<i>
</i>&lt;?php

//login_check() FUNCTION File.
/*
Check if User is logged-in or not by checking if the session names "user" is set (isset) or not. Return "TRUE" if it is; Else "FALSE".
*/

//Have to initiate the "session_start" global variable, regardless of whether User is logged-in or not, in order to deal with session stuffs in php.
if(!session_start())
{
session_start();
}

//Function for checking if User is logged-in or not.
function login_check()
{
if(isset($_SESSION["user"]) &amp;&amp; !empty($_SESSION["user"]))
{
//If Session "user" is set and not empty then return TRUE.
return TRUE;
}
else
{
//If Session "user" is NOT set or if session is empty then return FALSE.
return FALSE;
}
}

?&gt;
Copy linkTweet thisAlerts:
@rootOct 27.2018 — if(!session_start())
{
session_start();
}
is not how you test for an active session.

if( session_status() != PHP_SESSION_ACTIVE ) session_start();
Copy linkTweet thisAlerts:
@ginerjmOct 28.2018 — Since I don't see any code that runs if the session is not started, why do you believe you need to do this test? If the session is habitually begun at the beginning of each newly executed script you will have no problem. And if it is already begun for some odd reason, it won't matter since PHP recognizes it and (I think) puts out a notice only.

Simply get into the habit of starting your scripts (where possible) with a "session_start();" line. Nuff said.
Copy linkTweet thisAlerts:
@site-developerauthorNov 06.2018 — @root#1597190

Thanks!

I amended it like this:

login_check.php

<i>
</i>&lt;?php

//login_check() FUNCTION File.
/*
Check if User is logged-in or not by checking if the session names "user" is set (isset) or not. Return "TRUE" if it is; Else "FALSE".
*/

//Have to initiate the "session_start" global variable, regardless of whether User is logged-in or not, in order to deal with session stuffs in php.

/* Replacing following chunk:

if(!session_start())
{
session_start();
}

*/

//Replacing to this insstead:
if( session_status() != PHP_SESSION_ACTIVE ) session_start();

//Function for checking if User is logged-in or not.
function login_check()
{
if(isset($_SESSION["user"]) &amp;&amp; !empty($_SESSION["user"]))
{
//If Session "user" is set and not empty then return TRUE.
return TRUE;
}
else
{
//If Session "user" is NOT set or if session is empty then return FALSE.
return FALSE;
}
}

?&gt;
Copy linkTweet thisAlerts:
@site-developerauthorNov 06.2018 — @ginerjm#1597207

So, you are saying that, in my membership script, I should not bother checking if session has been started or not and should just start it on every page ?

I should add this at the top of all pages ?
<i>
</i>session_start();


Pages such as: home.php, settings.php, profile,php, search.php, etc. ? All pages that a user can access once logged-into his account.

EDITed a minute later.

All pages such as home.php, settings.php, etc. (pages inside member account) have the following line at the top:
<i>
</i>//Check if User is already logged-in or not. Get the login_check() FUNCTION to check.
if (login_check() === FALSE)
{
//Redirect User to Log-in Page immediately.
//header("refresh:0; url=home.php");
header("location:login.php");
exit();
}
Copy linkTweet thisAlerts:
@ginerjmNov 06.2018 — What does this line do for you?

if( session_status() != PHP_SESSION_ACTIVE ) session_start();

You can save a bit of needless typing/coding/thinking by simply starting the session. If it already exists you get a warning/notice which means nothing. Plus this condition will only happen if you have the session_start call in a 'sub-script' that is included inside your main script that began this executing session. That you should not do.
Copy linkTweet thisAlerts:
@ginerjmNov 06.2018 — if the settings/profile/search scripts are actually (what I call) modules that are included inside a main script then NO - you don't have to (nor should) use the session_start call in them. Remember what are your main scripts - the ones usually called via a url - and which are your sub-scripts.
Copy linkTweet thisAlerts:
@site-developerauthorNov 06.2018 — @ginerjm#1597461

You know what. How I did things (actually another php expert like you did it like this by building the login_check function) or how you suggest I do things is really based on us checking-out all my pages to get to grip with the structure of my script. Else, we'd get confused. Hence, before I do as you suggest, I reckon best you check all my files out first so that at the end you do not say I was doing it right originally and you got me to do things differently because you did not see all my files (did not see the big picture).

Stay tuned. I will release my work in this forum. Checkout any new thread based on the "membership script" as I would open such a thread to show all you guys my 21 mnths work for your feed-backs on my learnings and script.

Thanks! ?
Copy linkTweet thisAlerts:
@site-developerauthorNov 06.2018 — @ginerjm#1597462

In other words. At the end, you'd say I was doing things the right way and you thought otherwise because you did not have access to my other php files.

Best you see all my files and then conclude. Lookout for my upcoming "Membership Script" thread.
Copy linkTweet thisAlerts:
@ginerjmNov 06.2018 — I really think that your proposal would be even more of a waster of our time and yours. Nobody here is sitting around waiting to review any/all of your code just because you can't seem to take good advice and use it properly.

If you had used the things that you and your fellow pseudonyms have been given over the last few months/years things would have probably improved for you.

Focus on resolving the problem posed in this topic. Take what you then learn and ON YOUR TIME make any changes you think would benefit from your new-found knowledge.
Copy linkTweet thisAlerts:
@korolkov_kosNov 07.2018 — HELLO MY BROOO HEOL ME PLEASE IMMEDIATELY
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...