Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php http authentication

// Check auth:
if ($_SERVER['PHP_AUTH_USER']){
	echo "Authorized";
} else {
    header('HTTP/1.1 401 Unauthorized');
	echo "Unauthorized";
}
Comment

php authentication


<?php
$realm = 'Restricted area';

//user => password
$users = array('admin' => 'mypass', 'guest' => 'guest');


if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm.
           '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');

    die('Text to send if user hits Cancel button');
}


// analyze the PHP_AUTH_DIGEST variable
if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
    !isset($users[$data['username']]))
    die('Wrong Credentials!');


// generate the valid response
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);

if ($data['response'] != $valid_response)
    die('Wrong Credentials!');

// ok, valid username & password
echo 'You are logged in as: ' . $data['username'];


// function to parse the http auth header
function http_digest_parse($txt)
{
    // protect against missing data
    $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
    $data = array();
    $keys = implode('|', array_keys($needed_parts));

    preg_match_all('@(' . $keys . ')=(?:(['"])([^2]+?)2|([^s,]+))@', $txt, $matches, PREG_SET_ORDER);

    foreach ($matches as $m) {
        $data[$m[1]] = $m[3] ? $m[3] : $m[4];
        unset($needed_parts[$m[1]]);
    }

    return $needed_parts ? false : $data;
}
?>

Comment

PREVIOUS NEXT
Code Example
Php :: get elasticsearch data magento 2 
Php :: api symfony 4 @ApiResource 
Php :: acf looping through post types 
Php :: jsondecode php array 
Php :: nested for loop in php 
Php :: laravel copy 
Php :: php insert to mysql 
Php :: laravel route multiple middleware 
Php :: laravel create many to many table 
Php :: laravel import data from csv 
Php :: integer data type php 
Php :: middleware command in laravel 
Php :: php call method from another class 
Php :: php array push with key 
Php :: join string php 
Php :: laravel update multiple select query 
Php :: laravel pagination with search filter 
Php :: php explode empty string 
Php :: How to use Query builder with eloquent in Laravel 8? 
Php :: Laravel Migrations from an existing database 
Php :: how to get the previous page url in php 
Php :: lastinsertId php 
Php :: auto refresh extintion php 
Php :: get users by role name - spatie/laravel-permission 
Php :: Simple 301 redirect 
Php :: laravel request file empty 
Php :: seo_url.php location opencart 
Php :: fallo al conectar al servidor ftp wordpress 
Php :: sqlstate[22023]: invalid parameter value: 
Php :: remove duplicate characters in a string in php 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =