Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
sketchy

Fatal Error: When Attempting To Log In

Recommended Posts

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?

Share this post


Link to post
Share on other sites

Yes, you've defined the same function twice. Is that the full error message or is there more to it?

 

Also, just to be clear, the book does assume complete comfort with PHP and MySQL.

Share this post


Link to post
Share on other sites

Hi Larry

 

Thanks great book btw! Would you please advise me how to get around this please? The full error is:

Fatal error: Cannot redeclare escape_data() in /Applications/MAMP/htdocs/html/includes/mysql.inc.php on line 8

Share this post


Link to post
Share on other sites

Thanks for the nice words. I'm glad you like it. To get around this issue, you need to figure out where you're defining the escape_data() function twice. Or see if you're not including the database connection script twice.

Share this post


Link to post
Share on other sites

Thanks Larry - I have to admit I am a little stumped on this issue. Perhaps I am approaching this the wrong way, the only difference that I have done between what written in your book and my code is that I have included the mysql inside the 'includes folder', therefore if I don't add a require('mysqli.inc.php); command at the beginning of the login.inc.php code as shown before i get an error stating that:

 

An error occurred in script '/Applications/MAMP/htdocs/html/includes/login.inc.php' on line 4:

Undefined variable: dbc

 

i understood this error meant that it wasn't making a connection to the database and that is why i added that additional line, when i do add connection to mysqli using require command i get the error that I am redeclaring twice which as far as i can see I am not nor am I making a connection twice to the database as far as i can tell.

Share this post


Link to post
Share on other sites

It's not a question of making a connection twice, it's a question of declaring that same function twice. You either do that in one script or you include the same script (that defines that function) twice.

Share this post


Link to post
Share on other sites

The way you have this set up, you can't have "login.inc.php" and "mysql.inc.php" included in the same file without generating this error. To get around this, try using "require_once" instead of "require" any time you need to include the "mysql.inc.php" file. This is a pretty decent practice to follow anyway, although you should probably be more conscious about when you are connecting to the database.

 

To take it a bit further, however, it's arguably good design to not allow the login file to do the connecting, and instead just have it check and see if you are connected before it tries to execute. This way you are keeping the database connection functionality separate from the login functionality, since the login is obviously not the only thing using the database connection. But then again it's not necessarily bad design either, just some food for thought.

  • Upvote 1

Share this post


Link to post
Share on other sites

Hi Everyone,

Not sure if the problem of the 'dbc' variable not being declared has been resolved yet, but I was experiencing the same issue. I finally got to a point where I saw that the index.php file had the line 'require(MYSQL)' AFTER the 'if($_SERVER['REQUEST_METHOD']=='POST'){ include('includes/login.inc.php');}'. This gave the dbc not defined error in the login.inc.php script.

 

Adding 'require(MYSQL)' to the login.inc.php script caused the error that one can't redeclare a function.

 

I believe I solved this by moving the 'require(MYSQL)' above the 'if($_SERVER['REQUEST_METHOD']=='POST'){ include('includes/login.inc.php');}' line in the index.php file.

 

I hope that helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×