I would like to create an interface on a non-Drupal site that will allow me to login to my Drupal site?

How can I accomplish this, either with custom code or with an existing Drupal module?

  • Where you are going to use that login page? – Bala Jun 15 '13 at 12:38
  • 1
    Quick solution: Go to Drupal login page user/login and save the page as html and then copy login form (open form tag to close form tag) then paste it into php file. – Aboodred1 Jun 15 '13 at 12:52
  • @Bala I have that page in another site which is created with core php and mysql and i want t link login page with my drupal site login so that user can login thorough site on my drupal site. – sneha.kamble Jun 15 '13 at 14:12
  • @Aboodred1 can elaborate the steps. i din get it properly. – sneha.kamble Jun 15 '13 at 14:39
  • 2
    @Aboodred1 Doesn't the form build ID prevent this from working? – mpdonadio Jun 15 '13 at 14:49

Use Token authentication

Token Authentication provides a token for use in the URL to authenticate users to your site. It only works on paths configured in the settings page.

Originally this module was intended to offer simple authentication for RSS feeds to allow feed readers to pull customized content listings. You can now use it to provide a simple browsing experience of any Drupal content.

You would need to code a bit behind the scene - namely inject new token to proper table on successful off-site login, but it should be possible.

But maybe for your case opposite approach would be better? You can add custom submit handler to Drupal's login form to get arbitrary PHP code executed.


My solution to your issue; is to read/get Drupal login page as HTML using file_get_contents PHP function or cURL. Then parse through the HTML string and extract login form with its fields and submit button, using DomDocument.

Add the following code to your none Drupal login page/block:

$url = 'http://example.com/user/login';

$html = file_get_contents($url);

$dom = new DOMDocument();

// to support HTML5 doctype



// get Drupal login form
$login = $dom->getElementById('user-login');

// reset action attribute to absolute URL  
$login->setAttribute('action', $url);

// save form node
$form = $login->ownerDocument->saveHTML($login);

print $form;

Note: Drupal messages will only appear on Drupal side in case of successful login or invalid login.

  • Thanks for this solution. I tried the whole code in login.php which I have created out side drupal but after adding this code when I clicked on login of external site it goes to login.php and blank screen. Should I do something else too. Please guide me. – sneha.kamble Jul 14 '13 at 19:05
  • @sneha.kamble please view source of rendered HTML, and check if login form contain action="http://example.com/user/login" – Aboodred1 Jul 15 '13 at 21:33
  • I just used the code upto $url = 'example.com/user/login'; $html = file_get_contents($url); print $html; I am getting entered Username and pwd with $_POST. Then I am writing a javascript to add these values to respective fields n login form of drupal and then using trigger function to click submit so form redirects to drupal site. may be it's not best solution.If you have some better way please let me know so that my code will be unbreakable. thanks. – sneha.kamble Jul 16 '13 at 14:39
  • please check above comment and let me know your thoughts. – sneha.kamble Jul 16 '13 at 14:44
  • @sneha.kamble well, its not enough to use part of the code and leave the rest. the problem with printing $html is that action attribute will contain the wrong URL. So you need to set the right action to make it work for you. Let me know if you more clarification. – Aboodred1 Jul 17 '13 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.