Jump to content
Larry Ullman's Book Forums

Ch4, Login Form Not Working


Recommended Posts

Hi Larry and all! I'm up to Chapter 4, User Accounts, creating a login form for the site. I can access it on index.php, but when I try to test it... nothing happens! No errors, not even a login confirmation. I've tried debugging it with the examples provided by this site, and I'm still having the same problem. Is it possible that one of my includes is causing this bug in some way? Thanks for anyone who helps me out!

 

(PS, I should note that I used form_functions.inc.php for site registration, and it worked fine! But I'm posting it anyway just in case.)

 

<?php # Script 1.7 - form_functions.inc.php

 

// Define the function

function create_form_input($name, $type, $errors){

 

// Check for and process the value:

$value = FALSE;

if (isset($_POST[$name])) $value=$_POST[$name];

 

// Strip the magic quotes:

if ($value && get_magic_quotes_gpc()) $value = stripslashes($value);

 

// Check the input type:

if (($type == 'text') || ($type == 'password')){

 

// Create the input:

echo '<input type="' . $type . '" name="' . $name . '" id="' . $name . '"';

 

// Add the input's value, if applicable:

if ($value) echo ' value="' . htmlspecialchars($value) . '"';

 

// Check for an error:

if (array_key_exists($name, $errors)){

echo ' class="error" /><span class="error">' . $errors[$name] . '</span>';

} else { // No error detected.

echo ' />';

}

 

// Check if the input type is a textarea:

} elseif ($type == 'textarea'){

 

// Display the error first:

if (array_key_exists($name, $errors)) echo '<span class="error">' . $errors[$name] . '</span>';

 

// Create the textarea:

echo '<textarea name="' . $name . '" id="' . $name . '" rows="5" cols="75"';

 

// Add the error class, if applicable:

if (array_key_exists($name, $errors)){

echo ' class="error">';

} else { // No error detected.

echo '>';

}

 

// Add the value to the text area:

if ($value) echo $value;

 

// Complete the textarea:

echo '</textarea>';

 

} // End of primary IF-ELSE.

} // End of the create_form_input() function.

 

---

 

<?php # Script 1.9 - login.inc.php

 

// Create an array for recording errors:

$login_errors = array();

 

// Validate the email address:

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){

$e = mysqli_real_escape_string($dbc, $_POST['email']);

} else {

$login_errors['email'] = 'Please enter a valid email address!';

}

 

// Validate the password:

if (!empty($_POST['pass'])){

$p = mysqli_real_escape_string($dbc, $_POST['pass']);

} else {

$login_errors['pass'] = 'Please enter your password!';

}

 

// If no errors occurred, query the database:

if (empty($login_errors)){

$q = "SELECT id, username, type, IF(date_expires >= NOW(), true, false) FROM t_users WHERE (email='$e' AND pass='" . get_password_hash($p) . "')";

$r = mysqli_query($dbc, $q);

 

// If everything went OK:

if (mysqli_num_rows($r) == 1){

$row = mysqli_fetch_array($r, MYSQLI_NUM);

$_SESSION['user_id'] = $row[0];

$_SESSION['username'] = $row[1];

if ($row[2] == 'admin') $_SESSION['user_admin'] = TRUE;

if ($row[3] == 1) $_SESSION['user_not_expired'] = TRUE;

} else { // Create an error message.

$login_errors['login'] = 'The email address and password did not match those on file.';

}

 

} // End of $login_errors IF.

 

---

 

<?php # Script 1.10 - login_form.inc.php

 

if (!isset($login_errors)) $login_errors = array();

require_once ('includes/form_functions.inc.php');

 

?>

<div class="title">

<h4>Login</h4>

</div>

<form action="index.php" method="post" accept-charset="utf-8">

<p><?php if (array_key_exists('login', $login_errors)){

echo '<span class="error">' . $login_errors['login'] . '</span><br />';

}?><label for="email"><strong>Email Address</strong></label><br /><?php create_form_input('email', 'text', $login_errors);?><br />

<label for="pass"><strong>Password</strong></label><br /><?php create_form_input('pass', 'password', $login_errors);?> <a href="forgot_password.php" align="right">Forgot?</a><br />

<input type="submit" value="Login →"></p>

</form>

Link to comment
Share on other sites

 Share

×
×
  • Create New...