Jump to content

read a form and then submit it


severndigital

Recommended Posts

I've been working on a script to automatically log into one of our internal systems and perform search functions.

 

I need to log into the system to do this.

 

I used the HttpRequest() class to get in. But I've been having some problems.

 

Apparently the login form has a hidden field in it called login_token that needs to be sent along with the post data.

 

the login page and the action for the form are two different pages.

 

how do i read the value of the login_token field and pass it along with the post data to the login action?

 

here is what I have so far.

 

$DSUrl = 'http://my.site.com';

$user = "username";
$pass = "password";

$login = new HttpRequest($DSUrl . '/ApplyLogin',HttpRequest::METH_POST);
$post_data = array("username"=>$user,"password"=>$pass,"domain"=>'DocuShare');
//$post_data = array("username"=>$user,"password"=>$pass);
$login->addPostFields($post_data);
//$login->addHeaders($headers);
try {
echo $login->send()->getBody();

}catch(HttpException $ex){
echo $ex;
}

 

Thanks in advance.

-P

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/165910-read-a-form-and-then-submit-it/
Share on other sites

I do not know the exact method, but you will need to hit the page and search the source for the hidden field, there must be a method of grabbing the contents between two set values.

 

Something like this might help:

 

$content=''; // YOUR HTML SOURCE HERE
$word1='<input type="hidden" name="token" value="';
$word2='" />';
$between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1));

 

I haven't tested this code, you will have to set your HTML to $content, and modify $word1 and $word2 to match the code around your hidden field. It might be benificial to remove line breaks in your HTML before testing it.

 

I hope this helps.

 

ILMV

Here is a function that you can call to make POST requests against resource locators (should be all you need).

 


function makePost($url, $referer, $_data) {

    $data = array();   

    while(list($n,$v) = each($_data)){

        $data[] = "$n=$v";

    }   

    $data = implode('&', $data);

    $url = parse_url($url);

    if ($url['scheme'] != 'http') {

        die('Only HTTP request are supported !');

    }

    $host = $url['host'];
    $path = $url['path'];

    //CREATE A SOCKET
    $fp = fsockopen($host, 80);

    //HEADERS
    fputs($fp, "POST $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Referer: $referer\r\n");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-length: ". strlen($data) ."\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $data);

    $result = '';

    while(!feof($fp)) {

        $result .= fgets($fp, 128);
    }

    fclose($fp);

    $result = explode("\r\n\r\n", $result, 2);

    $header = isset($result[0]) ? $result[0] : '';

    $content = isset($result[1]) ? $result[1] : '';

    return array($header, $content);
}


$data = array(
    'field1' => 'value1',
    'field2' => 'value2',
    'field3' => 'value3'
);

list($header, $content) = makePost("http://www.example.com/", "http://www.referersite.com/", $data);

print $content;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.