Jump to content
Larry Ullman's Book Forums

sketchy

Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by sketchy

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

  2. 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?

×
×
  • Create New...