Я использую базовую HTTP-аутентификацию для использования веб-сервисов REST из компонента joomla, и мои пользователи не должны ничего вводить (нужно только один раз войти в joomla).Я просто хватаю пользователя, уже вошедшего в систему, и затем отправляю его в свой веб-сервис, используя CURL
$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
}