Jump to content
Larry Ullman's Book Forums

Ch18 Login Script, Slightly Modified, Not Working


Recommended Posts

I have made a few slight changes to the login script in Ch 18. One change is to have a last_login field updated when a user logs in.

 

The script works as I want it to. However, the last_login (type DATETIME) column in my db is not updating.

 

Could anyone suggest why this col is not updating? Thanks in advance. Code is below...

 

<?php
session_start();
ob_start();
require ('includes/config.inc.php');
$page_title = "Log-in";
include('includes/header.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array();
// Validate the email address:
if (!empty($_POST['email'])) {
 $e = mysqli_real_escape_string($dbc, $_POST['email']);
} else {
 $e = FALSE;
 $errors[] = 'Please enter a valid email address.';
}
// Validate the password:
if (!empty($_POST['pass'])) {
 $p = mysqli_real_escape_string($dbc, $_POST['pass']);
} else {
 $p = FALSE;
 $errors[] = 'Please enter a password.';
}
if ($e && $p) { // If everything's OK.
 // Query the database:
 $q = "SELECT id, first_name, level FROM users WHERE (email='$e' AND password=SHA1('$p')) AND active =''";
 $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 if (mysqli_num_rows($r) == 1) { // A match was made.
	 // Register the values:
	 $_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
	 $id = $_SESSION['id'];
	 // set last login date and time
	 $q = "UPDATE users SET last_login = NOW() WHERE id = $id";
	 $r = mysqli_query($dbc, $r) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	 mysqli_close($dbc);
	 // Redirect the user:
	 $url = BASE_URL . 'index.php'; // Define the URL.
ob_end_clean();
	 header("Location: $url");
	 exit(); // Quit the script.
 } else { // No match was made.
	 echo '<div id="lesson_wrap">
	 <h2 class="top error">Login failed.</h2>
	 <p class="error">No such username/password combination found.</p>
	 <p>Please <a href="login.php">try again</a>.</p>
	 </div>';
 }
} else { // If one of the data tests failed.
 echo "<div id='lesson_wrap'>
		 <div id='errors_holder'>
		 <h2>Oops, wait a minute...</h2>
		 <ul>";
 foreach ($errors as $error) {
	 echo "<li class='error'>$error</li>";
 }
?>
		 </ul>
		 </div>
	 <div id='form_holder'>
 <form name='signup' id='signup' method='post' action='' >
	 <label for='email'>Email address</label>
	 <input type='text' name='email' id='email' value='<?php if(isset($e)) { echo $e; }?>'/>
	 <label for='pass'>Password</label>
	 <input type='password' name='pass' id='pass' value=''/>
	 <input type='submit' name='submit' value='Log in' />
 </form>
</div>
<?php
 mysqli_close($dbc);
}
} else { // End of SUBMIT conditional.
?>
<div id='lesson_wrap'>
<h2 class='top'>Log in.</h2>
<p><em>(Note: your browser must allow cookies in order to log in.)</em></p>
<div id='form_holder'>
 <form name='signup' id='signup' method='post' action=''>
	 <label for='email'>Email address</label>
	 <input type='text' name='email' id='email' value=''/>
	 <label for='pass'>Password</label>
	 <input type='password' name='pass' id='pass' value=''/>
	 <input type='submit' name='submit' value='Log in'/>
 </form>
</div>
</div>
<?php include ('includes/footer.php');
}
?>

Link to comment
Share on other sites

$q = "UPDATE users SET last_login = NOW() WHERE id = $id";
$r = mysqli_query($dbc, $r)

 

ermm i think this one:

 

$r = mysqli_query($dbc, $r)

 

should be replaced with :

 

$r = mysqli_query($dbc, $q)

 

... duh!

That's what I get for staring at the same code for too long. Thanks Sendy!

Link to comment
Share on other sites

  • 2 months later...
 Share

×
×
  • Create New...