?
Solved

Basic HTTP Authentication

Posted on 2007-11-19
2
Medium Priority
?
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
#SSL #TLS #Citrix #HTTPS #PKI #Compliance #Certificate #Encryption #StoreFront #Web Interface #Citrix XenApp
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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