---
title: "PHP HTTP Authentication"
author: "keshion"
date: "2012-11-22T17:39:27Z"
tags: []
categories: ["编程语言"]
slug: "php-http-authentication"
draft: false
lastmod: "2012-11-22T17:39:27Z"
id: 1733497
url: "https://keshion.iteye.com/blog/1733497"
---

```
<?php
 
$config['admin_username'] = "demo";
$config['admin_password'] = "demo";
 
if (!($_SERVER['PHP_AUTH_USER'] == $config['admin_username'] && $_SERVER['PHP_AUTH_PW'] == $config['admin_password'])) {
    header("WWW-Authenticate: Basic realm=\"Papermashup.com Demo Admin\"");
    header("HTTP/1.0 401 Unauthorized");
    echo 'This is what happens if you press cancel';
    ?>
<?php
    exit;
}
// if the username and password match show the rest of the content
 
?>
```