Topic: PHP/MySql Login Verification

Hey guys,  I'm a new web developer doing things kinda out of my reach so if I sound stupid I'm sorry.  I am trying to create a mysql database with usernames and passwords which php will connect to and verify, then in turn redirect the user accordingly.  I found a script that does just that but I have some questions about it.
I created the Mysql database as such...

CREATE TABLE tbl_auth_user (
user_id VARCHAR(10) NOT NULL,
user_password CHAR(32) NOT NULL,

PRIMARY KEY (user_id)
);

INSERT INTO tbl_auth_user (user_id, user_password) VALUES ('theadmin', PASSWORD('chumbawamba'));
INSERT INTO tbl_auth_user (user_id, user_password) VALUES ('webmaster', PASSWORD('webmistress'));


//Now that that is created I looked at the PHP code and this is what I have questions on.

<?php
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
   include 'library/config.php';
   include 'library/opendb.php';

   $userId = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];

   // check if the user id and password combination exist in database
   $sql = "SELECT user_id
           FROM tbl_auth_user
           WHERE user_id = '$userId'
                 AND user_password = PASSWORD('$password')";

   $result = mysql_query($sql)
             or die('Query failed. ' . mysql_error());

   if (mysql_num_rows($result) == 1) {
      // the user id and password match,
      // set the session
      $_SESSION['db_is_logged_in'] = true;

      // after login we move to the main page
      header('Location: main.php');
      exit;
   } else {
      $errorMessage = 'Sorry, wrong user id / password';
   }

   include 'library/closedb.php';
}
?>

The part that confuses me is the library directory.  Do I actually have to create a library directory and the files called closedb.php and opendb.php or is this some apache built in server feature.  If i do need to create the directory... are these files already created?  The reason I ask is because in the tutorial he gave no source to these files, so I am wondering if they are a standard web script that can be obtained someplace.

Thank you for any input you may have in advance,
Squeaker2

Thumbs up

Re: PHP/MySql Login Verification

Hi .. Yes You have to create these two files

include 'library/config.php';
Inside the above file you can put code some thing like this

<?php
$host = "localhost";//Your mysql host name
$user = "root";  //Username to access the database;
$pass = "password";//Password to access the database;
$db = "_db_name";//The database where you created the tables
?>

include 'library/opendb.php';

then in opendb.php add the following code

<?php
mysql_connect(  $host, $user, $pass  ) or die( mysql_error() );
mysql_select_db(  $db );
?>

Try this .. have a nice time ..

Thumbs up

Re: PHP/MySql Login Verification

if you dont want to create these file than you need to add the above mentioned code in the same file and all the files makes task a little bit complex so better you must create the two files

Thumbs up