Friday, March 1, 2013

php authentication basic username and password

index.php


<?php 
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="Need Authentication"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Need Authentication';
    exit;
} else {
    if(empty($_SERVER["PHP_AUTH_USER"]) || $_SERVER["PHP_AUTH_USER"] != "apm"
        || empty($_SERVER["PHP_AUTH_PW"]) || $_SERVER["PHP_AUTH_PW"] != "gh3412") {
        header('WWW-Authenticate: Basic realm="Authentication Error"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Authentication Error';
        exit;
    }
}
?>

 
<?php 
$ch = curl_init();

if(is_array($get_variables))
{
    $get_variables = '?' . str_replace('&amp;','&',urldecode(http_build_query($get_variables)));
}
else
{
    $get_variables = null;
}
curl_setopt($ch, CURLOPT_URL, "http://dom.com/index.php" . $get_variables);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //CURL doesn't like google's cert
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'adm:pwd3012');
$headers[0] = "Authorization: Basic " . base64_encode("apm:gh3412");

if($post_variables == null || !is_array($post_variables)) {
    $post_variables = array();
}
if(is_array($post_variables))
{
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables);
}

if(is_array($headers))
{
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
}

$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

curl_close($ch);
?> 
 

No comments:

Post a Comment