Solved

Basic HTTP Authentication

Posted on 2007-11-19
2
153 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I want to echo out my field names with its values into a table 10 34
echo paypal data on screen 5 52
preventing spam with mailto 22 31
Presenting past 6 months bug 7 29
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 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