At sometime, you need to have your script to login a user via joomla API. Here's how:

 
function loginJoomlaUser($username,$password,$return=""){
              $app = JFactory:: getApplication();
              $options = array();
              $options[ 'remember'] = JRequest::getBool( 'remember', false );
              $options[ 'return'] = $return;
              $credentials = array();
              $credentials[ 'username'] = $username;
              $credentials[ 'password'] = $password;
              $err = $app->login($credentials, $options);
       }