<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ask Your Question &#187; Apache</title>
	<atom:link href="http://askbahar.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://askbahar.com</link>
	<description>Lets Talk</description>
	<lastBuildDate>Sun, 11 Mar 2012 09:50:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to enable Apache HTTP Authentication using PHP</title>
		<link>http://askbahar.com/2009/11/11/enable-apache-http-authentication-using-php/</link>
		<comments>http://askbahar.com/2009/11/11/enable-apache-http-authentication-using-php/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 09:24:39 +0000</pubDate>
		<dc:creator>Habib Ullah Bahar</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Tips n Tricks]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://askbahar.com/?p=48</guid>
		<description><![CDATA[<p>The easiest way to password-protect a site is to use HTTP Authentication, where if a browser’s request for a protected page is not accompanied by the correct username and password, the Web server replies with an HTTP 401 error – which means “Unauthorized Access” – and an invitation for the browser to re-submit the request [...]]]></description>
			<content:encoded><![CDATA[<p>The easiest way to password-protect a site is to use HTTP Authentication, where if a browser’s request for a protected page is not accompanied by the correct username and password, the Web server replies with an HTTP 401 error – which means “Unauthorized Access” – and an invitation for the browser to re-submit the request with a proper username and password. From the user’s point of view, most of this dialogue is hidden. Following that first failed request, the browser prompts the user (in a dialog box) for a username and password, and then re-submits the request, this time with the authentication information attached. Assuming the username/password combo is on the list of allowed users, the Web server then sends the page requested. The Web browser will likewise continue to send that username/password with all subsequent requests.</p>
<p>Look at the following code:<br />
<span id="more-48"></span></p>
<pre>
<code>
<?php
/*
    Document   : auth
    Created on : Nov 11, 2009, 9:08:32 AM
    @author    : Bahar
    Email      : bahar@progmaatic.com
    Copyright  : http://progmaatic.com

    Description: Authenticate user
*/

require_once 'settings.php';

$failure_msg = " <HTML>
 <HEAD><TITLE>Authorization Failed</TITLE></HEAD>
 <BODY>
 <H1>Authorization Failed</H1>
 <P>Without a valid username and password,
    access to this page cannot be granted.
    Please click 'reload' and enter a
    username and password when prompted.
 </P>
 </BODY>
 </HTML>";

if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER']!==$site_user || $_SERVER['PHP_AUTH_PW']!==$site_pass)
   {
       header("WWW-Authenticate: Basic realm=\"Protected Page: Enter your username and password for access.\"");
       header("HTTP/1.0 401 Unauthorized");
       exit($failure_msg);
   }

?>
</code>
</pre>
<p>For each page, you want to authenticate user, each page must have the above code. You can save this code to a script (for example <em>auth.php</em>) and include <em>auth.php</em> to any page, hence save yourself from rewriting the code for each page.</p>
]]></content:encoded>
			<wfw:commentRss>http://askbahar.com/2009/11/11/enable-apache-http-authentication-using-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

