有没有办法让 PHP 重定向到新页面,并传递 HTTP-AUTH?
我一直在使用 cURL,作为这里的第二个例子: Sending basic authentication information via form
不幸的是,当我这样做时,实际 URL(显示在浏览器 URL 栏中)仍然是原始 PHP 的 URL,而不是我正在浏览的目标。
这是我到目前为止所得到的:
<?php
$user = "xyz";
$pass = "abc";
$userpass = $user . ":" . $pass;
$url = "http://website/directory/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $userpass);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode == 200){
header("Authorization: Basic " . base64_encode($userpass));
echo $result;
}else{
header("Location: http://website/login.php");
}
?>
我也试过替换上面的尾端:
if($httpcode == 200){
header("Authorization: Basic " . base64_encode($userpass));
header("Location: http://website/directory/");
}else{
header("Location: http://website/login.php");
}
但是失败了;它重定向到 http://website.xyz/directory ,但它没有传递用户名/密码,我在到达时收到了来自服务器的登录信息。
从技术上讲,以下工作。但是,与在 HREF 中传递 user/pass 相比,我更喜欢更优雅的解决方案:
header("Location: http://" . $userpass . "website/directory/");
最佳答案
尝试将身份验证与 url 一起传递。像这样:
header('Location: http://username:password@website/directory/');
由于 base64 与纯文本一样不安全,因此它不应该成为额外的安全风险。浏览器不会在地址栏中显示与 user:pass 相关的 url 部分。但是浏览器会显示一个消息框(或其他东西)让你确认你即将使用用户名登录到这个网站:你的用户名
关于php - 使用 HTTP-AUTH 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15818159/