知道我有流动的功能:
如果用户转到另一个页面,它将获得“401访问被拒绝”,因为该请求中没有身份验证标头。这就是问题所在。
答案 0 :(得分:1)
我正在使用基本的HTTP身份验证来从joomla组件中使用REST Web服务,而我的用户不必输入任何内容(只需要登录joomla一次)。我只是抓住已登录的用户,然后使用CURL将其发送到我的Web服务
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//$username and $pass are the vars that will be used for authentication you can get them from your session or a cookie or in my case i got them from joomla JFactory::getUser()->username and JFactory::getUser()->password
curl_setopt($ch, CURLOPT_USERPWD, JFactory::getUser()->username.':'.JFactory::getUser()->password);
//here comes the important thing
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response=curl_exec($ch);
另一方面,您只需针对您的数据库检查$_SERVER['PHP_AUTH_USER']
和$_SERVER['PHP_AUTH_PW']
即可完成
if (!isset($_SERVER['PHP_AUTH_USER'])||$_SERVER['PHP_AUTH_USER']==''||!isset($_SERVER['PHP_AUTH_PW'])||$_SERVER['PHP_AUTH_PW']=='') {
header('WWW-Authenticate: Basic realm="Something"');
header('HTTP/1.0 401 Unauthorized');
echo 'You must be a valid user to access this contents';
exit;
} else {
// go to your database check they are valid and return whatever you want to return
}