UK Webmaster Talk - Online Marketing - SEO


 

HTTP Authentication

This is a discussion on HTTP Authentication within the Programming Articles forums, part of the Webmaster Articles/Tutorials category; Have you ever wanted a log in system for your site, to protect your admin cp, or to protect an ...


Go Back   UK Webmaster Talk - Online Marketing - SEO > Webmaster Articles > Webmaster Articles/Tutorials > Programming Articles

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Notices

Reply

 

LinkBack Thread Tools Display Modes
Old 12-04-2005, 00:50   #1 (permalink)
Tim
Cool Newbie
 
Join Date: Apr 2005
Posts: 32
iTrader: 0 / 0%
Tim is just really nice
Default HTTP Authentication

Have you ever wanted a log in system for your site, to protect your admin cp, or to protect an important file? With php, and a little feature in apache, you can easily do this using "HTTP Authentication." Let's get started by defining the username and password for the page.
PHP Code:
<?php
$username 
"Bob";
$password "hello2";
Simple, right? There are a few variables that php will set automatically when a user authentication form has been submitted. One of them being $PHP_AUTH_USER -- and the other $PHP_AUTH_PW. With that, let's do this:
PHP Code:
if (($PHP_AUTH_USER !== $username) || ($PHP_AUTH_PW !== $password))
{
header('WWW-Authenticate: Basic realm="Protected Area"');
header('HTTP/1.0 401 Unauthorized');
die(
"You do not have access to this file.");
}
?> 
With this, if the user is not authenticated, a log-in box is generated, a 401 message sent to the browser, and the text to display if the log in fails a few times (triggered by the browser. You can guess what will happen after the conditional if- if the user is authenticated, you can continue with outputting your file, or executing php.
Here is what the log in box will look like in IE6:

And in FireFox 1.0:


Hopefully, your website is a lot more secure now.

Tim
Tim is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-04-2005, 23:51   #2 (permalink)
*poke* *poke*
 
Join Date: Apr 2005
Posts: 134
iTrader: 0 / 0%
mck9235 is on a distinguished road
Default

Aw, thanks Tim. I never knew how to make one of those screens before, and how it easy it is to make one. Excellent!
mck9235 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-05-2005, 19:46   #3 (permalink)
WMT Addict
 
Join Date: Apr 2005
Location: Brighton,UK
Posts: 162
iTrader: 0 / 0%
asgsoft is on a distinguished road
Default

tim when i add this to index.php

PHP Code:
<?php
$username 
"admin"
$password "admin";
if ((
$PHP_AUTH_USER !== $username) || ($PHP_AUTH_PW !== $password)) 

header('WWW-Authenticate: Basic realm="Protected Area"'); 
header('HTTP/1.0 401 Unauthorized'); 
die(
"You do not have access to this file."); 


?>
it just keeps asking for the password and then says i cannot access the file
asgsoft is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-05-2005, 20:18   #4 (permalink)
Moderator
 
Join Date: Jan 2005
Location: post is there >>
Posts: 1,586
iTrader: 0 / 0%
robertall is just really nice
Send a message via AIM to robertall Send a message via MSN to robertall
Default

Quote:
Warning: Cannot modify header information - headers already sent by (output started at /home/robert/public_html/teen games/index.php:8) in /home/robert/public_html/teen games/index.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at /home/robert/public_html/teen games/index.php:8) in /home/robert/public_html/teen games/index.php on line 14
You do not have access to this file.
http://www.robertall.com/teen%20games/index.php

Have i done something wrong?

Code:
<html>
<head>
<title></title>
</head>


<body bgcolor="#FFFFFF">
<?php
$username = "admin";
$password = "admin";
if (($PHP_AUTH_USER !== $username) || ($PHP_AUTH_PW !== $password))
{
header('WWW-Authenticate: Basic realm="Protected Area"');
header('HTTP/1.0 401 Unauthorized');
die("You do not have access to this file.");
}

?> 
</body>


</html>
I named it index.php.

It could be that i am doing it incorrectly by the PHP version, do i need 4.x or 5.x?
__________________
Free .org domain - Click for details!
Do not pm asking for things. My inbox is full. Add me to MSN!
robertall is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-05-2005, 23:17   #5 (permalink)
Super Moderator
 
Join Date: Mar 2005
Location: Herts, UK
Posts: 1,026
iTrader: 1 / 100%
monaghan is a jewel in the rough
Default

You need to put the php code before any browser output otherwise the header() function will fail as you are seeing. Move the <?php.... ?> above the <html> and it should be fine.
monaghan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 13-05-2005, 11:13   #6 (permalink)
Moderator
 
Join Date: Jan 2005
Location: post is there >>
Posts: 1,586
iTrader: 0 / 0%
robertall is just really nice
Send a message via AIM to robertall Send a message via MSN to robertall
Default

Ah, ok, thanks for the help, i will try again when i get home
__________________
Free .org domain - Click for details!
Do not pm asking for things. My inbox is full. Add me to MSN!
robertall is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
authentication, http

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

Similar Threads

Thread Thread Starter Forum Replies Last Post
http://www.wallpaperseek.com tulip4heaven Website Reviews 0 26-12-2005 15:29
http://www.bintangor.com Bmg Ads, Offers & Services 7 06-04-2005 04:07
http://www.knot.in knot Buy and Sell Ad Space 0 02-04-2005 14:34


All times are GMT +1. The time now is 01:56.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
UK Webmaster Forum © WebmasterTalk.co.uk | Design by Forbairt

Ad Management by RedTyger

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41