Solved

Basic HTTP Authentication

Posted on 2007-11-19
2
154 Views
Last Modified: 2010-05-18
Hi,

I want to password protect a single php page. Can someone tell me how to do this.

Thanks,


R.
0
Comment
Question by:roscoeh23
2 Comments
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 20312339
You could check for a value from a password field if it's only for one page:



Hope this helps.
<?php 
 
$password = "mypasswordhere";
 
if(isset($_POST['password'])){
if($_POST['password']==$password){
// user is authenticated, show them some stuff:
?>
Display some authenticated stuff here in the page.  
 
<?php 
} else{ 
// display a form for them to login:
 
?>
<form action="post">
<input type="password">
<input name="submit" type="button" value="submit" /></form>
<?php 
}
?>

Open in new window

0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20314246
if you want something really simple, here is something that works. don't forget to customize your contents (commented) and your username and password.
<?php
// specify password & username
$username = "test";
$password = "testing";
 
if (!isset($_SERVER['PHP_AUTH_USER'])) 
{
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    exit;
}
 
if($_SERVER['PHP_AUTH_USER'] != $username || $_SERVER['PHP_AUTH_PW'] != $password)
{
	header('WWW-Authenticate: Basic realm="My Realm"');
	header('HTTP/1.0 401 Unauthorized');
	exit;
}
 
// Your PHP CONTENTS HERE IF THEY ENTER CORRECT PASSWORD & USERNAME!
echo "Success!!";
?> 

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question