Solved

Basic HTTP Authentication

Posted on 2007-11-19
2
151 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:ingwa
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Imagine a situation that you have installed SSL (http://en.wikipedia.org/wiki/Secure_Sockets_Layer) Certificate on your Cisco ASA (Cisco Adaptive Security Appliance) firewall. Installation of SSL certificate on ASA is an another topic for which you …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now