Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Basic HTTP Authentication

Posted on 2007-11-19
2
Medium Priority
?
162 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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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

636 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