Hi everyone!
I know this problem has already been discussed but I use MYSQLI so I have different issue here.
My problem is:
I have coded a login page (my testing page) för a forum that I am creating.
But it doesn't work.
I have spent 4 days going through everything and have now scratched off all of my hair from my head ;)
Could anyone take a look and se what the problem is
Thank you very much.
<?php
// This section processes submissions from the login form
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//connect to database
require('dbconnection.php');
// Validate the email address
if (!empty($_POST['email'])) {
$e = mysqli_real_escape_string($dbcon, $_POST['email']);
} else {
$e = FALSE;
echo '<p class="error">You forgot to enter your email address.</p>';
}
// Validate the password
if (!empty($_POST['psword'])) {
$p = mysqli_real_escape_string($dbcon, $_POST['psword']);
} else {
$p = FALSE;
echo '<p class="error">You forgot to enter your password.</p>';
}
if ($e && $p){//if no problems
// Retrieve the user_id, first_name and user_level for that email/password combination
$q = "SELECT user_id, fname, user_level FROM members WHERE (email='$e' AND psword=SHA1('$p'))";
// Run the query and assign it to the variable $result
$result = mysqli_query ($dbcon, $q);
// Count the number of rows that match the email/password combination
if (@mysqli_num_rows($result) == 1) {//if one database row (record) matches the input:-
// Start the session, fetch the record and insert the three values in an array
session_start();
$_SESSION = mysqli_fetch_array ($result, MYSQLI_ASSOC);
// Ensure that the user level is an integer.
$_SESSION['user_level'] = (int) $_SESSION['user_level'];
// Use a ternary operation to set the URL
$url = ($_SESSION['user_level'] === 1) ? 'home.php' : 'member.php';
header('Location: ' . $url); // Make the browser load either the members’ or the admin page
exit(); // Cancel the rest of the script
mysqli_free_result($result);
mysqli_close($dbcon);
} else { // No match was made.
echo '<p class="error">The e-mail address and password entered do not match our records
<br>Perhaps you need to register, just click the Register button on the header menu</p>';
}
} else { // If there was a problem.
echo '<p class="error">Please try again.</p>';
}
mysqli_close($dbcon);
} // End of SUBMIT conditional.
?>
And here is the login fields' table
<?php require ('dbconnection.php'); ?>
<form action="index.php" method="post">
<table cellpadding="1" cellspacing="1">
<tr>
<td>
<label class="under_title">
Your e-mail address
</label>
</td>
<td>
<label class="under_title">
Password
</label>
</td>
</tr>
<tr>
<td>
<input class="login_fields" type="text" id="email" name="email" value="<?php if(isset($_SESSION['email'])); echo $_POST['email']; ?>"
</td>
<td>
<input class="login_fields" type="password" name="psword" value=""
</td>
</tr>
<tr>
<td colspan="2">
<input class="login_button" name="login" id="login" type="submit" value="Login now">
</td>
</tr>
<tr>
<td>
<a href="index.php" target="_self"> Forgot password?</a>
</td>
<td>
<a href="register.php" target="_self"> Create account.</a>
</td>
</tr>
</table>
</form>