[advanced search]
Results from the most recent live auction are here.
18 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 08-12-2005, 11:24 AM   · #1
thestudent
NamePros Regular
 
thestudent's Avatar
 
Name: nujaim
Location: pakistan
Trader Rating: (4)
Join Date: Aug 2005
Posts: 303
NP$: 219.95 (Donate)
thestudent is an unknown quantity at this point
Notice HTTP AUTH with PHP and mySQL

I saw that someone had posted a JavaScript login script topic on the forums, and quite a few people suggested that PHP and mySQL is a much better way to handle user logins. However, no-one posted anything about HOW to do it, so I figured I’d give it a bash.
So here’s a small tutorial on using HTTP AUTH with PHP and mySQL.
Get out your favourite text editor, fire up your local webserver, get a nice drink, and get ready to see just how simple it is.

First we need to set up the database with the info we’re going to need for the scripts, so log into your mySQL admin tool (phpMyAdmin, etc, etc) on your local server and set up a new database (doesn’t really matter what you call it. I usually use “test_db”).
Once that’s done, open up the SQL Query window and put in the following query
Code:
DROP TABLE IF EXISTS Users; CREATE TABLE Users (  ID int(11) NOT NULL auto_increment,  FullName varchar(255) NOT NULL default '',  Email varchar(255) NOT NULL default '',  Username varchar(8) NOT NULL default '',  Password varchar(20) NOT NULL default '',  LastLoginDateTime text NOT NULL,  LastLoginAddress varchar(255) NOT NULL default '',  LastLoginIP text NOT NULL,  PRIMARY KEY  (ID) ) TYPE=MyISAM;

I won’t be using all those variables in this tutorial, but I find it’s better to have extra fields in case you want to add functionality later on.
Right, now that’s done, we need to put user login information into the mySQL table so our script will work.
So open up the SQL Query window again and put in the following query.
Code:
INSERT INTO Users VALUES (1,'TestUser','test@localhost','test','test','1212 120','127.0.0.1', '127.0.0.1');

This will create a user with the username “test” and the password “test”.

OK, now for the scripts.
Make a new file and call it config.php (any name will do, just remember what it’s called).
In that file put the following
Code:
<?php $dbHost = "localhost"; &nbsp;//change this to the database host $dbUser = "root"; &nbsp; //change this to the database username $dbPass = "root"; &nbsp;//change this to the database password $dbName = "test_db"; &nbsp;//change this to the database name $userTable = "Users"; &nbsp; $userField = "Username"; $passField = "Password"; ?>

Remember to change anything that needs changing.
Save that file and then create another one called login.php.
In login.php put the following code
Code:
<?php &nbsp;include ("config.php"); &nbsp;function authenticate() { &nbsp; Header("WWW-Authenticate: Basic realm=\"secure login\""); &nbsp; echo ("Authentication Failed!\n"); &nbsp; exit(); &nbsp;} &nbsp;if(!isset($PHP_AUTH_USER)) { &nbsp; &nbsp;authenticate(); &nbsp; &nbsp;echo ("Authorization Failed!\n"); &nbsp; &nbsp;exit(); &nbsp;} else { &nbsp; &nbsp;$checkLogin = "SELECT ID FROM $userTable WHERE &nbsp; &nbsp;$userField='$PHP_AUTH_USER' AND &nbsp; &nbsp;$passField='$PHP_AUTH_PW'"; &nbsp; &nbsp;$db = mysql_pconnect($dbHost, $dbUser, $dbPass); &nbsp; &nbsp;mysql_select_db($dbName, $db); &nbsp; &nbsp;$result = mysql_query($checkLogin, $db); &nbsp; &nbsp;$numrows = mysql_num_rows($result); &nbsp; &nbsp;$myrow = mysql_fetch_array($result); &nbsp; &nbsp;if ($numrows == 0) { &nbsp; &nbsp; &nbsp;authenticate(); &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp;setcookie("UserID", $myrow["ID"]); &nbsp; &nbsp; &nbsp;$UserID = $myrow["ID"]; &nbsp; &nbsp;} &nbsp;} ?>

Save that file and create another one called login_test.php.
In login_test.php put the following code

Code:
<?php include ("login.php"); ?> <html> <head> <title>Login Test Page</title> </head> <body> <center> Login successful. </center> </body> </html>

And that’s it.
To test that it’s all working, crank up your browser and access the login_test.php page you just made. You should be prompted for a username and password. Just type in “test” and “test” and you should see a page that says “Login successful.”
For any PHP page that you want to protect, just add the following code at the very beginning of the file.
Code:
<?php include ("login.php"); ?>

Just in closing, the scripts are a little bit messy and could be cleaned up some.
And if you want to secure your pages even more you could encrypt the passwords in the database.
The basic script is from someone else's tutorial (not on B2L) but i can't for the life of me track it down, and i've added some bits and pieces as well. There are quite a few versions out there and they all pretty much use the same code, so don't be surprised if you see a similar script somewhere else.
Hope this comes in handy for anyone who wants to have user logins on their site.
I might do another tut on making the user add, edit and delete forms for this script, as well as adding the LastLoginIP, LastLoginAddress and LastLoginDateTime updating.


Please register or log-in into NamePros to hide ads
thestudent is offline   Reply With Quote
Old 08-15-2005, 08:26 PM   · #2
Porte
Template Designer
 
Porte's Avatar
 
Name: Ahmed
Location: Somewhere on earth!
Trader Rating: (29)
Join Date: May 2005
Posts: 3,533
NP$: 17.30 (Donate)
Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of
That's a nice effort. Keep it up!
Porte is offline   Reply With Quote
Old 08-23-2005, 06:16 PM   · #3
Red Eyes Arena
NamePros Regular
 
Trader Rating: (2)
Join Date: Aug 2005
Posts: 199
NP$: 50.00 (Donate)
Red Eyes Arena is an unknown quantity at this point
I remember reading that AES Encryption are like the most secure encryptions currently offered for passwords in database. Is that true?
Red Eyes Arena is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HOWTO: Install the Apache Web Server, Perl, PHP, and MySQL on Windows deadserious Webmaster Tutorials 96 05-27-2007 01:24 PM
Tutorial: How to Install Apache2 MySQL and PHP on Windows deadserious Webmaster Tutorials 35 09-21-2005 09:46 PM
WEB HOSTING - $4.99/MONTH For 1GB HD & 25GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet Web Hosting Offers 0 07-23-2005 09:40 AM
RESELLER WEB HOSTING - $9.99/MONTH For 4GB HD & 30GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet Web Hosting Offers 0 03-06-2005 08:16 PM

Site Sponsors
Hunting Moon Build your NameBrand Thousand Dollar Profits
Advertise your business at NamePros
All times are GMT -7. The time now is 02:47 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0