有没有办法让PHP重定向到新的页面,并传递HTTP-AUTH?使用HTTP-AUTH重定向?
我一直在使用卷曲,作为第二个例子在这里: Sending basic authentication information via form
不幸的是,当我这样做,(在浏览器地址栏中显示)的实际URL保留原始PHP的网址,而不是目标我正在浏览。
这里是我到目前为止有:
<?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内传递用户/密码更优雅的解决方案:
header("Location: http://" . $userpass . "website/directory/");
我也这么想过。当HTTP-AUTH被重定向时,它似乎并未将HTTP-AUTH传递给新页面;到达新位置后,我仍然通过网络服务器提示用户/通行证。 – ltwally 2013-04-04 18:36:10
您确定您要重定向到的网站正在接受您的凭证吗?我用两个不同的文件夹在本地主机上测试过它,它工作了 – hek2mgl 2013-04-04 18:41:53
我看了上面的代码,并替换了“echo $ result;”与“标题('位置:http://网站/目录');”,这是我所尝试过的。它失败了。到达http:// website/directory /后,我收到了登录提示。 – ltwally 2013-04-04 18:59:42