tags:

views:

33

answers:

1

I'm trying to use a PHP script to login to another web site (a Moodle install, in this case) by POSTING to the login page.

This code seems to partially work. The problem is that only the first parameter is recognized by the receiving end. For the code shown below, only the 'username' parameter is recognized. However, if you switch the order of 'username' and 'password' so that 'password' is listed first, then only 'password' is recognized using $_POST['password'].

$postdata = http_build_query(
    array(
        'username' => $currentUser,      
        'password' => $userPassword,      
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents($loginURL, false, $context);

Any idea on why only the first parameter is being recognized?

A: 

Ok, I see now that the code is correct and that something external is interfering with the POST request. This code was being run inside of Drupal so I think that has something to do with it. When I save this separately as a .php file it works as expected.

Stewart