<?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>Webmaster-Source &#187; authentication</title>
	<atom:link href="https://www.webmaster-source.com/tag/authentication/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.webmaster-source.com</link>
	<description>Useful Resources For Webmasters</description>
	<lastBuildDate>Thu, 24 Aug 2017 02:01:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Basic HTTP Authentication With PHP</title>
		<link>https://www.webmaster-source.com/2009/06/03/basic-http-authentication-with-php/</link>
		<comments>https://www.webmaster-source.com/2009/06/03/basic-http-authentication-with-php/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 11:25:43 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=2235</guid>
		<description><![CDATA[Basic HTTP authentication is rudimentary method of requesting a username and password, then allowing or denying access based on the credentials entered. You&#8217;ve probably seen it in action somewhere or another. If you try to subscribe to a protected RSS feed, such as the feed for your friends timeline on Twitter, for example. It&#8217;s not [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Basic HTTP authentication is rudimentary method of requesting a username and password, then allowing or denying access based on the credentials entered. You&#8217;ve probably seen it in action somewhere or another. If you try to subscribe to a protected RSS feed, such as the feed for your friends timeline on Twitter, for example.</p>
<p>It&#8217;s not the most user-friendly way to authenticate a user, but it has it&#8217;s uses. It&#8217;s great for APIs and other things where a pretty interface isn&#8217;t being delivered, where a more low-level solution is required. It&#8217;s also good for restricting access to parts of your server that most people just don&#8217;t need to be accessing.</p>
<p><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-2236" title="Basic HTTP Authentication" src="//www.webmaster-source.com/wp-content/uploads/httpbasicauth.jpg" alt="Basic HTTP Authentication" width="500" height="212" /></p>
<p><span id="more-2235"></span>Now how do you do that for yourself? It&#8217;s a fairly simple matter with PHP. Basically you send an <code>HTTP/1.0Â 401Â Unauthorized</code> header, and a <code>WWW-Authenticate: Basic realm="Name of Realm"</code>. This tells the client that it&#8217;s not authorized to view the page, and that it should try to become authorized.</p>
<p>That&#8217;s all it takes to have the little box show up and demand a username and password. Now all you have to do is check the submitted username and password against the correct ones. Simply check the <code>$_SERVER['PHP_AUTH_USER']</code> and <code>$_SERVER['PHP_AUTH_PW']</code> global variables.</p>
<p>Here&#8217;s an example of a minimal script to request and check a username and password:</p>
<pre class="brush: php; title: ; notranslate">&lt;?php

if ( !isset($_SERVER['PHP_AUTH_USER']) ) {
header('WWW-Authenticate: Basic realm=&quot;You Shall Not Pass&quot;');
header('HTTP/1.0 401 Unauthorized');
exit;
}
else {
if ( $_SERVER['PHP_AUTH_USER'] == 'me' &amp;&amp; $_SERVER['PHP_AUTH_PW'] == 'password' ) {
echo &quot;&lt;p&gt;Welcome, {$_SERVER['PHP_AUTH_USER']}.&lt;/p&gt;&quot;;
} else {
echo &quot;Wrong password, Balrog!&quot;;
}
}

?&gt;</pre>
<p>A quick word of caution: Whenever you store passwords, be they in a database, text file or simply hard-coded into the script, you should always encrypt them with a one-way hash. The PHP Security Consortium has <a href="http://phpsec.org/articles/2005/password-hashing.html">an article on how to do this</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2009/06/03/basic-http-authentication-with-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: www.webmaster-source.com @ 2026-06-09 04:32:31 by W3 Total Cache
-->