Zubrag.com
June 23, 2018, 05:15:53 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: using MySQL DB to handle User name and Password  (Read 22247 times)
barry
Newbie
*
Posts: 4


« on: December 18, 2007, 03:17:39 AM »

This script is great! Works very well... many thanks for developing it.

I am trying to use Mysql database to handle username & password.

I got it to work; however only the last entry from the db works.  Huh 
This is the code so far...

include "dbconnect.php";  // connection file

$query = 'SELECT * FROM user_table';
$result = mysql_query($query);
{
   while($row = mysql_fetch_array($result))

$LOGIN_INFORMATION[$row['user_name']] = $row['password']; // calling user_name & password from db

}
mysql_close($db);                      // close db connection

The above script works... it connects to the db and inserts the user name and password from the row...

However, if I have 3 rows of data, only the last row user_name and password works (also the manual entry works).

If I add another row of user_name & password to the db the previous one stops working!!!

How do I make all of them work! or is it not possible?

Thanks for your time!

Logged
zubrag
Administrator
Hero Member
*****
Posts: 786


WWW
« Reply #1 on: December 18, 2007, 03:48:57 AM »

Hi. Updated your code for LOGIN_INFORMATION.

LOGIN_INFORMATION should be populated like this: LOGIN_INFORMATION['username'] = 'password';
Logged
barry
Newbie
*
Posts: 4


« Reply #2 on: December 18, 2007, 04:23:34 AM »

WOWOWOW Zubrag

You blew me away! Thank you so very much for answering my post within minutes and it works 100%  Grin   Roll Eyes  Grin

I am pasting the script again for others to copy, knowing it works 100%

include "dbconnect.php";  // connection file

$query = 'SELECT * FROM user_table';
$result = mysql_query($query);

 {
   while($row = mysql_fetch_array($result))

// calling user_name & password fields from db table

$LOGIN_INFORMATION[$row['user_name']] = $row['password'];

 }

mysql_close($db);                      // close db connection

Thanks again, your help made my night meaningful!
Logged
mysmallbizu
Newbie
*
Posts: 4


« Reply #3 on: December 27, 2007, 12:44:36 AM »

I'm not a coder but I can cut and paste... Smiley

Where specifcially is the code to access the mysql db that you posted added?  and what part of the existing code is deleted?

Also instead of a dedicated db I need to query an existing db.

I'd like to use the user tables from my billing software "http://www.whmcs.com" - The username would be their email address and the password their existing password from the users table.

The other condition would be that they also have an active order.  this information is stored in a differnet table from the same db musing their userid to connect the two tables

So basically the query would allow members who have purchased product A would be allowed to login to the protected page using the email address and password that they have on file.

Is this possible?  Is there a easier way to do this?
 
I know the table names and fields I need to query, but not the php code

Any help would be much appreciated.
Logged
zubrag
Administrator
Hero Member
*****
Posts: 786


WWW
« Reply #4 on: December 27, 2007, 10:40:21 AM »

Hi.

Instead of
$LOGIN_INFORMATION = array(
  'zubrag' => 'root',
  'test' => 'testpass',
  'admin' => 'passwd'
);

you'll need to put this, replacing all the marked with red fields with correct values

MYSQL_HOST = 'localhost'; // usually localhost
MYSQL_USER = 'db-username'; // mysql username
MYSQL_PASSWORD = 'db-password'; // password
MYSQL_DB_NAME = 'db-name'; // mysql database name (on cPanel it is usually prefixed with username and underscore, like username_dbname)
USERNAME_FIELD = 'user_name'; // name of the field which holds username
PASSWORD_FIELD = 'user_password'; // name of the field which holds password

// Establish DB connection
$db = @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die("Could not connect to database");
@mysql_select_db(MYSQL_DB_NAME, $db) or die("Could not select database");

// query to get users list from db. Replace user_table with the table name which holds users.
$query = "SELECT * FROM user_table";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
  $LOGIN_INFORMATION[$row[USERNAME_FIELD]] = $row[PASSWORD_FIELD];
}
mysql_close($db);  // close db connection
Logged
zubrag
Administrator
Hero Member
*****
Posts: 786


WWW
« Reply #5 on: December 27, 2007, 10:48:16 AM »

mysmallbizu, you'll need to change SQL query marked with red above to something like this:

select *
from USER_TABLE, PURCHASE_TABLE
where USER_TABLE.ID_FIELD_NAME = PURCHASE_TABLE.USER_ID_FIELD
and PURCHASE_TABLE.ACTIVE = 1

Not sure about line marked with green. If you have some order "active" flag then use that line, otherwise remove.
Logged
mysmallbizu
Newbie
*
Posts: 4


« Reply #6 on: December 27, 2007, 12:23:25 PM »

The password is md5 encoded,

almost there.....

this is what i've been unsuccessfully trying...

USERNAME_FIELD = 'email'; // name of the field which holds username
PASSWORD_FIELD = 'md5($password)'; // name of the field which holds password
Logged
rambro
Newbie
*
Posts: 2


« Reply #7 on: April 01, 2008, 09:24:57 PM »

Hi all,

I used the above code, but made a few changes and it seems to be working for me.

$MYSQL_HOST = 'localhost'; // usually localhost
$MYSQL_USER = 'db-username'; // mysql username
$MYSQL_PASSWORD = 'db-password'; // password

$MYSQL_DB_NAME = 'db-name'; // mysql database name (on cPanel it is usually prefixed with username     // and underscore, like username_dbname)

$USERNAME_FIELD = 'user_name'; // name of the field which holds username
$PASSWORD_FIELD = 'user_password'; // name of the field which holds password

// Establish DB connection
$db = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or die("Could not connect to database");
@mysql_select_db($MYSQL_DB_NAME, $db) or die("Could not select database");

// query to get users list from db. Replace user_table with the table name which holds users.
$query = "SELECT * FROM user_table";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
  $LOGIN_INFORMATION[$row[$USERNAME_FIELD]] = $row[$PASSWORD_FIELD];
}
mysql_close($db);  // close db connection

Hope this helps.... Smiley
Logged
SuprSpy79
Newbie
*
Posts: 3


« Reply #8 on: May 20, 2008, 08:38:13 AM »

How can I add a 3rd field, I want to add a group field so that I can reference the users group to give access to certain areas based on user name. So I have a 3rd field in my db called pricegroup.

also after editing the script to use the db i am just getting a white screen?
EDIT: apparently i needed a $ sign in front of the variable names DUH lol
Logged
Surao
Newbie
*
Posts: 7


« Reply #9 on: January 12, 2009, 04:24:05 AM »

How to set up the manager.php to bring up data from the mysql DB?
Logged
mmarwaha
Newbie
*
Posts: 1


« Reply #10 on: November 03, 2011, 04:39:14 AM »

hi,
i am using the password protect script and it works great. thanks. one problem i am facing is that when i want to echo out the username, it only does when i log in for the first time, after refreshing the same page the logged in username is not echoed.
i want to use this because based on the logged in username, i want to run specific queries on that page from mysql.
please help
Logged
farish.kty
Newbie
*
Posts: 2


« Reply #11 on: November 08, 2011, 06:33:31 AM »

Can any one please tell me, where I paste/replace and what all details should I change from the below code for using MySQL DB to handle User name and Password ;
Code:
$MYSQL_HOST = 'localhost'; // usually localhost
$MYSQL_USER = 'db-username'; // mysql username
$MYSQL_PASSWORD = 'db-password'; // password

$MYSQL_DB_NAME = 'db-name'; // mysql database name (on cPanel it is usually prefixed with username     // and underscore, like username_dbname)

$USERNAME_FIELD = 'user_name'; // name of the field which holds username
$PASSWORD_FIELD = 'user_password'; // name of the field which holds password

// Establish DB connection
$db = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or die("Could not connect to database");
@mysql_select_db($MYSQL_DB_NAME, $db) or die("Could not select database");

// query to get users list from db. Replace user_table with the table name which holds users.
$query = "SELECT * FROM user_table";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
  $LOGIN_INFORMATION[$row[$USERNAME_FIELD]] = $row[$PASSWORD_FIELD];
}
mysql_close($db);  // close db connection

Logged
intrepid
Newbie
*
Posts: 4


« Reply #12 on: May 27, 2012, 11:56:34 PM »

That all works fine from the mySQL database,
but it fauls to set the cookie,
I need to login for each page I open,
unlike the simple code with the username => password array.

Any ideas
Logged
ragtech
[url=http://www.uswebsitecreations.com/]U.S. Website Creations[/url]
Newbie
*
Posts: 3


« Reply #13 on: August 13, 2012, 04:19:43 PM »

I also had the same cookie issue, but was able to use a third party cookie I found at codecanyon.com.

You may want to try it; it is the only one there at present so it should not be to hard to find.


Regards,
~ragtech
U.S. Website Creations
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC