Is there any way to customize Drupal login to use authentication from an external server?

I'd like to know about any modules or if Drupal core can implement this function.

Source code:

// Include FirePHP for debug

function authdave_form_alter(&$form, &$form_state, $form_id) {
    // Authentication based on username and password
    if ( ($form_id == 'user_login' || $form_id == 'user_login_block') ) {
        $form['#validate'][] = 'authdave_user_form_validate';
* Custom form validation function
function authdave_user_form_validate($form, &$form_state) {
    if( !authdave_authenticate($form_state) ) {
        form_set_error('name', t('Unrecognize username.'));
* Custom user authentication function
function authdave_authenticate($form_state) {
    // get first four characters from user name
    $complete_username = $form_state['input']['name'];
    $password = $form_state['input']['pass'];

    // retrieve users credentials from DB
    $credential = get_all_credentials();

    while($row = $credential->fetch_assoc()) {
        // check if there are any match
        if ( $row["username"] ==  $complete_username && $row["password"] == $password) {
            user_external_login_register($complete_username, 'custom_athentication');
            return TRUE;

    return FALSE;


function get_all_credentials() {

    //credenziali di accesso al dbms
    $server = "****";
    $username = "***";
    $password = "***";

    //nome db
    $db_name = "credenziali_db";

    //creo una connessione con il dbms
    $conn = new mysqli($server, $username, $password, $db_name);

    if ($conn -> connect_error) {
        die("Connection failed: " . $conn->connect_error);

    echo "Connection successfully";

    //sql query
    $sql = "SELECT username, password FROM credenziali";
    $result = $conn -> query($sql); //risultato della query

    return $result;




This depends what you mean by external server? There's a tonne of contribution modules at the moment that support a variety of external logins.

You can implement Open Auth 2 to allow a single sign on:


Or you can use Facebooks Oauth authentication:

  • I would allow users using credentials of private company server in wich they are signed up instead of drupal authentication. – rafneb Sep 8 '15 at 7:39
  • In that case you need to write your own solution hooking into the Drupal login system. Check the Drupal API documentation on its login and authentication hooks. You should make a note to be very careful here as mistakes could open your site to abuse. – Daniel Waters Sep 8 '15 at 7:41
  • I've written my custom module to customize user authentication. I've used external server database to verify user access credentials and everything works fine. I've only one problem: after user authentication drupal site show me an error message "Sorry, unrecognized username or password. Have you forgotten your password?", but the user is logged in. What should be the mistake? – rafneb Sep 8 '15 at 16:05
  • Can you provide any source code of this? be best in your question – Daniel Waters Sep 8 '15 at 19:10
  • You can see source code in the question above – rafneb Sep 8 '15 at 22:27

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.