Jump to content



Photo

Fatal Error: When Attempting To Log In


  • Please log in to reply
7 replies to this topic

#1 sketchy

sketchy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 22 February 2012 - 7:37 PM

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?
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,855 posts
  • LocationState College, PA (USA)

Posted 22 February 2012 - 8:06 PM

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.
  • 0

#3 sketchy

sketchy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 22 February 2012 - 8:20 PM

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
  • 0

#4 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,855 posts
  • LocationState College, PA (USA)

Posted 22 February 2012 - 9:16 PM

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.
  • 0

#5 sketchy

sketchy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 23 February 2012 - 3:37 PM

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.
  • 0

#6 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,855 posts
  • LocationState College, PA (USA)

Posted 24 February 2012 - 10:10 PM

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.
  • 0

#7 theaceofthespade

theaceofthespade

    Member

  • Members
  • PipPip
  • 24 posts
  • LocationKentucky

Posted 31 March 2012 - 9:32 PM

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.
  • 1

#8 Andre

Andre

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 10 June 2012 - 10:15 AM

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.
  • 0