Hi
I’m learning PHP out of a book, and I’ve come across an error that I’m not sure how to solve.
Error:
Parse error: parse error, expecting ','' or‘;’’ in /Users/nick/Sites/Head First/PHP/examples/ch07/initial/mismatch/login.php on line 71
The code for the login.php file can be found below:
[php]<?php
require_once(‘connectvars.php’);
// Clear the error message
$error_msg = “”;
// If the user isn’t logged in, try to log them in
if (!isset($_COOKIE[‘user_id’])) {
if (!isset($_POST[‘submit’])) {
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Grab the user-entered log-in data
$user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));
if (!empty($user_username) && !empty($user_password)) {
// Look up the username and password in the database
$query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND password = SHA('$user_password')";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 1) {
// The log-in is OK so set the user ID and username variables, and redirect to the home page
$row = mysqli_fetch_array($data);
setcookie('user_id', $row['user_id']);
setcookie('username', $row['username']);
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
header('Location: ' . $home_url);
}
else {
// The username/password are incorrect so set an error message
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}
else {
// The username/password weren’t entered so set an error message
$error_msg = ‘Sorry, you must enter your username and password to log in.’;
}
}
}
?>
Mismatch - Log In
<?php // If the cookie is empty, show any error message and the log-in form; otherwise confirm the log-in if (empty($_COOKIE['user_id'])) { echo '' . $error_msg . '
'; ?><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend> Log In</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php if (!empty($user_username)) echo $username'?>" /><br />
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
</fieldset>
<input type="submit" value="Log In" name="submit" />
</form>
<?php
}
else {
// Confirm the successful log in
echo('You are logged in as ' . $_COOKIE['username'] . '.
'); } ?> [/php] [i]Line 71 is 6 lines from the bottom.[/i]Does anybody have any suggestions to fix the error?
Thanks a lot
Nick