I use basic HTTP authentication to use REST web services from the joomla component, and my users should not enter anything (you only need to log in to Joomla only once). I just pick up the user who has already registered, and then send it to my web service using 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);
On the other hand, you just need to check $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] for your database, and you're done
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 {
source share