It appears that you are not registered yet. Please click here to register for free!

Top Webloggers: daveincarthage.com
WebLoggers.org

Sponsored links



Go Back   WebLoggers WebLogging Forum > Design and Development > Blogging Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2005, 02:18 AM   #1 (permalink)
Junior Member
 
Join Date: Dec 2005
Posts: 23
thestudent is on a distinguished road
Post 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','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.

Last edited by WebForging; 01-09-2006 at 10:24 AM. Reason: non-bold
thestudent is offline   Reply With Quote
Old 12-09-2005, 02:10 AM   #2 (permalink)
Senior Member
 
dongan's Avatar
 
Join Date: Dec 2005
Location: Dictyo
Posts: 256
dongan is on a distinguished road
Default Re: HTTP AUTH with PHP and mySQL

Thank you. anyway, can u make it non-bold. hard to read. Only make some points highlighted.
dongan is offline   Reply With Quote
Old 01-09-2006, 09:09 AM   #3 (permalink)
Junior Member
 
Join Date: Oct 2005
Posts: 25
andyl is on a distinguished road
Default Re: HTTP AUTH with PHP and mySQL

ugh, nobody follow this guide, I'm writing up some changes but it will take me a while to finish.
andyl is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:34 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 2008Ad Management by RedTyger SEO by vBSEO 3.3.0