Hi All, Having completed chpt 4, i have one minor bug which i can't seem to get around. I have created my login.inc.php and included a connection to mysqli.inc.php on the second line as i have created the mysqli file in the includes folder. When i load the index page into my browser and attempt to log in a get the following error message:
Fatal error: Cannot redeclare escape_data() in /Applications/MAMP/htdocs/html/includes/mysql.inc.php on line 8
my code for the mysql.inc.php seems correct and reads like this:
<?php
DEFINE ('DB_USER','............');
DEFINE ('DB_PASSWORD', '..............');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'ecommerces');
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($dbc, 'utf8');
function escape_data ($data) {
global $dbc; // Database connection.
if (get_magic_quotes_gpc()) $data = stripslashes($data);
return mysqli_real_escape_string ($dbc, trim ($data));
} // End of the escape_data() function.
function get_password_hash($password) {
global $dbc;
return mysqli_real_escape_string ($dbc, hash_hmac('sha256', $password, 'c#haRl891', true));
}
my code on the login.inc,php is as follows:
<?php
require ('mysql.inc.php');
$login_errors = array();
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!';
}
if (!empty($_POST['pass'])) {
$p = mysqli_real_escape_string($dbc, $_POST['pass']);
} else {
$login_errors['pass'] = 'Please enter your password!';
}
if (empty($login_errors)) {
$q = "SELECT id, username, type, IF(date_expires >= NOW(), true, false) FROM users WHERE (email='$e' AND pass='" . get_password_hash($p) . "')";
//in book no = sign after NOW() but on files IF(date_expires >= NOW()
$r = mysqli_query ($dbc,$q);
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 {
$login_errors['login'] = 'The email address and password do not match those on file.';
}
}// end of $login_errors IF.
Do you have any insights as to what may be causing this fatal error?