【问题标题】:Forward Basic Authentication to a form?将基本身份验证转发到表单?
【发布时间】:2010-08-11 19:57:19
【问题描述】:
好吧,不过我不知道是否有人尝试过这样做。
我有一个网站,就叫它本地主机吧。目前。我在那个页面上有一个表格。但是,我希望能够跳过表单,并使用基本身份验证方法将我的数据重定向到表单。例如:http://admin:admin@localhost 会将用户名和密码发送到我的表单。
有没有办法在我不必修改整个网站的情况下做到这一点?
【问题讨论】:
标签:
php
authentication
forms-authentication
basic-authentication
【解决方案1】:
您无需将其发送到表单。
你可以这样做
if(!isset($_SESSION['admin'])){
//we are not logged in yet,
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Admin"');
header('HTTP/1.0 401 Unauthorized');
echo 'Please Contact us if you are having problems logging in';
exit;
} else {
//not their first time through
//check their username and password here
$username = trim($_SERVER['PHP_AUTH_USER']);
$password = trim($_SERVER['PHP_AUTH_PW']);
... your auth code here....
【解决方案2】:
Very easy using the $_SERVER array.
<form>
<input name="username" value="<?php echo $_SERVER['PHP_AUTH_USER']; ?>">
<input type="password" value="<?php echo $_SERVER['PHP_AUTH_PW']; ?>">
</form>