Created
February 2, 2016 23:46
-
-
Save ikarius6/81fe8171656b1f34b139 to your computer and use it in GitHub Desktop.
CI Auth Basic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
//application/controllers/Test.php | |
class Test extends MY_Controller { | |
private $username = 'admin'; | |
private $salt = 'immaveryveryveryverylongsalt'; | |
private $password = 'c25c068f620bae40f6de49aa32bc7a036989baae382f64d7cc41f89a741ff6ae'; //supersecret | |
function __construct(){ | |
parent::__construct(); | |
if (isset($_SERVER['PHP_AUTH_USER'])) { | |
$username = $_SERVER['PHP_AUTH_USER']; | |
$password = $_SERVER['PHP_AUTH_PW']; | |
} elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) { | |
if (strpos(strtolower($_SERVER['HTTP_AUTHORIZATION']),'basic')===0) | |
list($username,$password) = explode(':',base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); | |
} | |
if ( | |
empty($username) | |
|| empty($password) | |
|| !($username == $this->username && $this->password == hash('sha256', $this->salt.$password)) | |
) { | |
header('WWW-Authenticate: Basic realm="My Realm"'); | |
header('HTTP/1.0 401 Unauthorized'); | |
echo '<h1>Forbidden</h1>'; | |
die(); | |
} | |
} | |
public function index(){ | |
echo "Auth Basic"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment