php authentication

Solutions on MaxInterview for php authentication by the best coders in the world

showing results for - "php authentication"
Chole
12 Feb 2020
1
2<?php
3$realm 'Restricted area';
4
5//user => password
6$users array('admin' => 'mypass''guest' => 'guest');
7
8
9if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
10    header('HTTP/1.1 401 Unauthorized');
11    header('WWW-Authenticate: Digest realm="'.$realm.
12           '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
13
14    die('Text to send if user hits Cancel button');
15}
16
17
18// analyze the PHP_AUTH_DIGEST variable
19if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
20    !isset($users[$data['username']]))
21    die('Wrong Credentials!');
22
23
24// generate the valid response
25$A1 = md5($data['username'] . ':' . $realm ':' . $users[$data['username']]);
26$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
27$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
28
29if ($data['response'] != $valid_response)
30    die('Wrong Credentials!');
31
32// ok, valid username & password
33echo 'You are logged in as: ' . $data['username'];
34
35
36// function to parse the http auth header
37function http_digest_parse($txt)
38{
39    // protect against missing data
40    $needed_parts array('nonce'=>1'nc'=>1'cnonce'=>1'qop'=>1'username'=>1'uri'=>1'response'=>1);
41    $data array();
42    $keys = implode('|', array_keys($needed_parts));
43
44    preg_match_all('@(' . $keys ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@'$txt$matches, PREG_SET_ORDER);
45
46    foreach ($matches as $m) {
47        $data[$m[1]] = $m[3] ? $m[3] : $m[4];
48        unset($needed_parts[$m[1]]);
49    }
50
51    return $needed_parts false : $data;
52}
53?>
54
55
queries leading to this page
php authentication examplephp header authenticationauthentication in phplogin authentication in phpauthentication system phpphp make authhttp basic authentication php user authentication phpphp authenticatephp mysql authentication php authentication class examplebest authentication method for phpphp auth commandphp read basic authenticationphp authentication tutorialauthentication server phpdatabase authentication phpphp simple authenticationphp authentication libraryphp login authenticationphp page access user authenticationhow to use basic authentication in php header requestauth phpphp get request basic auth basic auth with phpsimple authentication phpauth phpoauth authentication php examplephp authentication componentauthentication framework phpauthentication class phpauth library phpphp user authenticationauthentication code in phpphp framework for authenticationphp header authentication basicauthentication method phphttp authentication php exampleauthentication php classphp login authentication demologin authentication phplogin php authenticationhow to make php authphp http authenticationfucntions php for login authenticationget function in php using basic authbasic authentication phpbasic auth phpphp basic authfirebase authentication phpauthentication phpwhat is auth in phpform authentication in phpsimple php authenticationphp header authentication page examplephp authentication systemuser authentication phpphp authentication frameworkphp authhttp authentication with phpuser authentication and authorization in phpphp basic authentication examplephp authenticationbasic php authenticationhow to use basic authentication in php headerauth0 php examplephp header authentication examplesercure authentication phpphp authentication dependencysimple user authentication in phphow to create user authentication phphow to use c 23 for authentication inside phpauth in phpauthentication does which of the following phpuser authentication in phpphp authentificationphp basic http authenticationdevelop authentication system in phpauth using phpphp authentication