どの認証、HTTPファイルをダウンロード?
-
20-09-2019 - |
質問
私のサイトかるphpスクリプトを起動するファイルをダウンロードは表示されずにフルパスでのファイルのコードのようなものです:
$path = '../examples/test.zip';
$type = "application/zip";
header("Expires: 0");
header("Pragma: no-cache");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
あり方を追加するHTTP認証を前にダウンロード?
感謝。
編集1: コンドレホフマンけんを使うことで問題を解消のHTTP基本認証!だいHTTPダイジェスト認証、以下のようにしたいのですか?また、コードを書く上での線"エコーを"ログインしてい:'.$データ['ユーザ名'];"...がエラっとした変更が行えない状態になり、ヘッダ!
<?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 'Your 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;
}
?>
【解決
コンドレ、アンソニー、に記述することができるソ
<?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 ... start the download
$path = '../examples/test.zip';
$type = "application/zip";
header("Expires: 0");
header("Pragma: no-cache");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
// 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;
}
?>
解決
必ず見 マニュアル ます。
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
//check $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']
if ($valid) {
//start download
$path = '../examples/test.zip';
$type = "application/zip";
header("Expires: 0");
header("Pragma: no-cache");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
} else {
//show error
}
}
更新:
A..htaccessファイルに基づく認証のビジネスに繋げで使用するようなユーザーです。の取組みなどについて、お話をおります。.htaccessファイル:
AuthType Basic
AuthName "Password Required"
AuthUserFile passwords.file
AuthGroupFile groups.file
ファイル パスワードをファイル を含むパスワードを利用できるもの htpasswd
ツールをapache.ファイル ます。ファイル うようにこれに類する:
GroupName: rbowen dpitts sungo rshersey
ここでは基本的にだけリストにユーザーへのアクセスを持つべきであるをクリックします。
も参照してください この barcode.net(日本語版)がバージョン
他のヒント
しようとしていませ開始のダウンロード後、スクリプトechos入する必要がありますログインが成功したとして..."?
で設定できない任意のヘッダーした後に出力さい画面になります。とすぐにエコー-印刷または何をしていますので目立っていたの体の一部にHTTPリダイレクトのHTTPレスポンスのヘッダがセットされています。
ようにしようとした場合設定のヘッダやその後の"しんだログイン"ビットの、そのままにこだわったファイルは出力画面になります。
したいことは、スクリプト出力のログインしている"との リダイレクト のスクリプトをダウンロードできます。のユーザーになる第二のページのヘッダー設定"添付".これはどのよう典型的な"コンテンツのダウンロードで始まり瞬間的に"ページです。