定制摘要式身份验证
为了知道我有流动的功能:
如果用户转到另一个页面,它将得到“401访问被拒绝”,因为该请求中没有认证标头。 这就是问题所在。
我使用基本的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
}
链接地址: http://www.djcxy.com/p/22097.html