NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > Webmaster Tutorials
Reload this Page HTTP AUTH with PHP and mySQL

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search
0 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 08-12-2005, 11:24 AM THREAD STARTER               #1 (permalink)
Account Suspended
 
thestudent's Avatar
Join Date: Aug 2005
Location: Canada
Posts: 491
thestudent is just really nicethestudent is just really nicethestudent is just really nicethestudent is just really nice
 



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.
????: NamePros.com http://www.namepros.com/webmaster-tutorials/115055-http-auth-with-php-and-mysql.html
????: NamePros.com http://www.namepros.com/showthread.php?t=115055

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','1212120','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.
thestudent is offline  
Old 08-15-2005, 08:26 PM   #2 (permalink)
Senior Member
 
Porte's Avatar
Join Date: May 2005
Location: I'm right here
Posts: 3,525
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!
__________________
WP Theme Developer
Your One-stop for Premium Magazine/CMS WordPress Themes
Deluxe Themes
Porte is offline  
Old 08-23-2005, 06:16 PM   #3 (permalink)
NamePros Member
Join Date: Aug 2005
Posts: 198
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  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


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

 
All times are GMT -7. The time now is 12:54 AM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger