Jump to content

Why Html Form Plays Hide & Seek with Php ?


phpsane

Recommended Posts

Php Gurus,

 

This is my login.php.

All this time it was working when I place the php code at the top and the html form at the bottom. I made a few changes to the code and suddenly the code does not work unless I drag the html form above the php code. Why is that ?

On the other hand, on the registration.php, I got the php code above the html form and it is working. No problem there. What am I missing here ?

 

This forum has messed up both of my post's code indentations. So, best you check the attachments.

 

1. Html Form above Php Code (working):

 

<?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; //Added it so script runs no further if user is logged-in. Mac_Guyver's suggestion.
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
  <meta charset="utf-8">
</head>
<body>
<div class = "container">
<form method="post" action="">
<center><h3><?php $site_name ?> Member Login Form</h3></center>
<div class="text-danger">
<div class="form-group">
<center><label>Username/Email:</label>
<input type="text" placeholder="Enter Username" name="login_username_or_email" value="<?php if(isset($_COOKIE["login_username_or_email"])) echo $_COOKIE["login_username_or_email"]; ?>"</center>
</div>
<div class="form-group">
<center><label>Password:</label>
<input type="password" placeholder="Enter password" name="login_password" value="<?php if(isset($_COOKIE["login_password"])) echo $_COOKIE["login_password"]; ?>"></center>
</div>
<div class="form-group">
<center><label>Remember Login Details:</label>
<input type="checkbox" name="login_remember" /></center>
</div>
<div class="form-group">
<center><input type="submit" name="login_submit" value="Login" class="button button-success" /></center>
</div>
<div class="form-group">
<center><font color="red" size="3"><b>Forgot your password ?</b><br><a href="login_password_reset.php">Reset it here!</a></font></center>
<center><font color="red" size="3"><b>Not registered ?</b><br><a href="register.php">Register here!</a></font></center>
</form>
</div>
</body>
</html>
 
<?php
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"]); // I rid the mysqli_real_escape_string based on Mac_Guyver's suggestion.
$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($stmt = mysqli_prepare($conn, "SELECT accounts_activations, usernames, emails, passwords FROM users WHERE usernames = ?"))
{
mysqli_stmt_bind_param($stmt, 's', $username_or_email);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt); //Use either this line, or ...
//$result = mysqli_stmt_bind_result($stmt, $db_account_activation_state, $db_username, $db_email, $db_password); // ... this line. But not both.
 
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
 
// Check if inputted Username or Email is registered or not.
//Either type following paragraph or the next one but not both. Ask in forum which one is best.
 
// PARAGRAPH 1 
if ($username_or_email == $row['usernames'] || $username_or_email == $row['emails'] && password_verify($password, $row['passwords'])) // either this paragraph or ...
{
echo "Paragraph 1: Please wait while we check your account details ...";
if($row['accounts_activations'] == '0')
{
echo "Paragraph 1: 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 "Paragraph 1: That Username or Email is not registered!"; 
exit;
} 
 
 
/* PARAGRAPH 2
if ($username_or_email == $db_username || $username_or_email == $row['emails'] && password_verify($password, $db_password)) // ..... this paragraph. But not both.
{
echo "Paragraph 2: Please wait while we check your account details ...";
if($row['accounts_activations'] == '0')
{
echo "Paragraph 2: 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 "Paragraph 2: That Username or Email is not registered!"; 
exit;
} 
*/
 
//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","","");
}
if (isset($_COOKIE['login_password'])) //I will remove this later on as it is not safe to save passwords on cookies. Currently, got this on for debugging purpoises.
{
setcookie("login_password", "", "");
}
}
$_SESSION["user"] = $username_or_email;
header("location:home.php?user=$username");
}
else
{
echo "Your account details are incorrect!";
exit;
}
}
else
{
echo "isset else triggered!";
exit;
}
}
else
{
echo "SERVER REQUEST_METHOD is not getting triggered !";
exit;
}
 
echo "Do not forget to click the 'Login' button!";
exit;
?>

login_2.php

login_2b.php

Link to comment
Share on other sites

Message continued from original post as this forum deleted the lower part of my original post ....

 

 

2.
Php Code Above Html Form (Does not work! Html form does not appear!):
 

<?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; //Added it so script runs no further if user is logged-in. Mac_Guyver's suggestion.
}
 
 
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"]); // I rid the mysqli_real_escape_string based on Mac_Guyver's suggestion.
$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($stmt = mysqli_prepare($conn, "SELECT accounts_activations, usernames, emails, passwords FROM users WHERE usernames = ?"))
{
mysqli_stmt_bind_param($stmt, 's', $username_or_email);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt); //Use either this line, or ...
//$result = mysqli_stmt_bind_result($stmt, $db_account_activation_state, $db_username, $db_email, $db_password); // ... this line. But not both.
 
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
 
// Check if inputted Username or Email is registered or not.
//Either type following paragraph or the next one but not both. Ask in forum which one is best.
 
// PARAGRAPH 1 
if ($username_or_email == $row['usernames'] || $username_or_email == $row['emails'] && password_verify($password, $row['passwords'])) // either this paragraph or ...
{
echo "Paragraph 1: Please wait while we check your account details ...";
if($row['accounts_activations'] == '0')
{
echo "Paragraph 1: 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 "Paragraph 1: That Username or Email is not registered!"; 
exit;
} 
 
 
/* PARAGRAPH 2
if ($username_or_email == $db_username || $username_or_email == $row['emails'] && password_verify($password, $db_password)) // ..... this paragraph. But not both.
{
echo "Paragraph 2: Please wait while we check your account details ...";
if($row['accounts_activations'] == '0')
{
echo "Paragraph 2: 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 "Paragraph 2: That Username or Email is not registered!"; 
exit;
} 
*/
 
//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","","");
}
if (isset($_COOKIE['login_password'])) //I will remove this later on as it is not safe to save passwords on cookies. Currently, got this on for debugging purpoises.
{
setcookie("login_password", "", "");
}
}
$_SESSION["user"] = $username_or_email;
header("location:home.php?user=$username");
}
else
{
echo "Your account details are incorrect!";
exit;
}
}
else
{
echo "isset else triggered!";
exit;
}
}
else
{
echo "SERVER REQUEST_METHOD is not getting triggered !";
exit;
}
 
echo "Do not forget to click the 'Login' button!";
exit;
?>
 
<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
  <meta charset="utf-8">
</head>
<body>
<div class = "container">
<form method="post" action="">
<center><h3><?php $site_name ?> Member Login Form</h3></center>
<div class="text-danger">
<div class="form-group">
<center><label>Username/Email:</label>
<input type="text" placeholder="Enter Username" name="login_username_or_email" value="<?php if(isset($_COOKIE["login_username_or_email"])) echo $_COOKIE["login_username_or_email"]; ?>"</center>
</div>
<div class="form-group">
<center><label>Password:</label>
<input type="password" placeholder="Enter password" name="login_password" value="<?php if(isset($_COOKIE["login_password"])) echo $_COOKIE["login_password"]; ?>"></center>
</div>
<div class="form-group">
<center><label>Remember Login Details:</label>
<input type="checkbox" name="login_remember" /></center>
</div>
<div class="form-group">
<center><input type="submit" name="login_submit" value="Login" class="button button-success" /></center>
</div>
<div class="form-group">
<center><font color="red" size="3"><b>Forgot your password ?</b><br><a href="login_password_reset.php">Reset it here!</a></font></center>
<center><font color="red" size="3"><b>Not registered ?</b><br><a href="register.php">Register here!</a></font></center>
</form>
</div>
</body>
</html>

This forum has messed up both of my post's code indentations. So, best you check the attachments.
Link to comment
Share on other sites

I got rid of the two "exit;" on 114 & 118 and no it is showing the html form. :)

 

Working:

else
{
echo "SERVER REQUEST_METHOD is not getting triggered !";
 
}
 
echo "Do not forget to click the 'Login' button!";

Was not working:

else
{
echo "SERVER REQUEST_METHOD is not getting triggered !";
exit;
}
 
echo "Do not forget to click the 'Login' button!";
exit;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.