-1

I have a pop up for login in php that contains username, password text fields, a selectbox and a submit button. The current scenario is after filling details, selecting value from the select box and then clicking on submit button takes me to the site developed in joomla, changing select box value and submitting the login form takes me on a site developed in wordpress as a logged in user. Now what I want is to provide a third value in selectbox of login pop up,upon selecting which I will be logged in into drupal site. Should I use any web service, if yes then what is the process for that? Kindly suggest me an optimal solution.Thanks in advance.

  • can you show the code of your login form? – xurshid29 Dec 10 '13 at 13:17
  • Generally Drupal does not allow anyone to simply pass off-site values into a Drupal form - if for a given session form wasn't displayed, it can't be submitted. Anyway, here are some pointers. – Mołot Dec 10 '13 at 13:18
2

I found solution after navigating a number of urls and so sharing this with those who are looking for the answer :

I used ajax call and curl functions to fulfill my requirement.

The steps I followed are:

  1. I used ajax to call a local curl file on selection of a particular value in dropdown on login popup.

       function restapilogin1(){ 
          var email = $('#apploginusername').val();
          var password = $('#apploginpassword').val();
          var program = $('#apploginprogram').val();
    
    if(program == 'DrupalTest'){ 
        data={username:email,password:password};
        $.ajax({
            url: "<?php echo $root; ?>modules/mod_loginpopup/islogin_drupal.php",
            type: "POST",
            data: data,
            dataType: "html",
            success: function(response){
                if(response == 'error'){
                    $(".popup_login_status").html('The username or password you entered is incorrect.');
                }
                else{
                    var login_url = 'http://localhost/drupal-test/testlogin_1.php?user='+ email +'&pass='+ password;
                    window.location.href = login_url;
                }
            },
            error: function(err){
                console.log(err);
            }
        });
    }
    

    }

  2. The curl file is sending http request to drupal file to chek user authentication.Following is islogin_drupal.php (contains curl functions for sending http request to different server)

       $username = $_REQUEST['username'];
       $password = $_REQUEST['password'];
    
       $data = array("username" => $username, "password" => $password);
    
       $data_string = json_encode($data);
       $url='http://localhost/drupal-test/testlogin.php';
       $ch = curl_init($url);
       curl_setopt($ch, CURLOPT_HEADER, false);
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
       curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
       curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Access-Control-Allow-Origin: *',       'Content-Type: application/json'));
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
       $result = curl_exec($ch);
       curl_close($ch);
       $json_result = json_decode($result, true);
       echo $json_result['res'];
    
  3. I created a file in drupal root directory to check user authentication and return success/failure message.Here I used user_authenticate() function.

$postdata = file_get_contents("php://input");
$string = json_decode($postdata);
$username = $string->username;  
$password = $string->password;

//change this line and put your username and password instead of 'admin' and 'password'
$account = user_authenticate($username, $password);
if ($account) {
    $user = user_load($account, TRUE);
    drupal_session_regenerate();
    $token = 'sucess';
} 
else {
    $token = 'error';
}

echo json_encode(array('res' => $token));
  1. I created a new file in drupal root directory that is being called on success of ajax call. This file takes the username and password from query string and log in the user accordingly.

    //this gets the root of the drupal installation
    define('DRUPAL_ROOT', getcwd());
    //require the bootstrap include
    require_once './includes/bootstrap.inc';
    //Load Drupal
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    global $user;

    $account1 = user_authenticate($_GET['user'], $_GET['pass']);

    $user = user_load($account1, TRUE);
    drupal_session_regenerate();
    drupal_goto('http://localhost/drupal-test/');

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.