Folks,
My 3+ pages are all in mysqli procedural. I cannot just switch to pdo and oop and throw 6 mnths of work down the drain! And so, let's try converting the following code suggestion by Death Shadow to mysqli procedural.
I need your help.
Remember, the script is a login page and the user is given a choice to either type his email or username. And then finally the password.
The html form looks like this:
The following is regex to check if the user typed email or not.
Newbies, if you were after a regex that checks if the input is email or not. Then, here it is:
I need help adding the above regex in the appropriate place in the script. Appropriate integrationing.
:thumbsup:
My 3+ pages are all in mysqli procedural. I cannot just switch to pdo and oop and throw 6 mnths of work down the drain! And so, let's try converting the following code suggestion by Death Shadow to mysqli procedural.
Code:
if (
array_key_exists('login_username_or_email', $_POST) &&
array_key_exists('login_password'], $_POST)
) {
// don't bother trimming, they can't enter it right, don't let them log in!
$stmt = $conn->prepare('
SELECT ids, usernames, passwords, emails, accounts_activations_statuses
FROM users
WHERE ' . (
strpos($usernameOrEmail, '@') === false) ? 'usernames' : 'emails'
) . ' = ?
');
$stmt->bind_param('s', $_POST['login_username_or_email']);
$stmt->execute();
$stmt->bind_result(
$db_id, $db_username, $db_password, $db_email,
$db_account_activation_status
);
if (
$stmt->fetch() &&
password_verify($_POST['login_password'], $db_password)
) {
echo '
<p>Login Successful</p>
<dl>
<dt>User Id</dt>
<dd>', $db_id, '</dd>
<dt>E-Mail</dt>
<dd>', $db_email, '</dd>
<dt>Username</dt>
<dd>', $db_username, '</dd>
<dt>Activation Stats</dt>
<dd>', $db_account_activation_status, '</dd>
</dl>
';
} else echo '<p>Invalid username or password</p>';
$stmt->close();
} else echo '<p>Missing username or password</p>';
Remember, the script is a login page and the user is given a choice to either type his email or username. And then finally the password.
The html form looks like this:
Code:
<!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"]; ?>"
<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"]; ?>">
</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>
The following is regex to check if the user typed email or not.
Newbies, if you were after a regex that checks if the input is email or not. Then, here it is:
Code:
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;
}
}
:thumbsup: