0

I've been looking for a while for something that can help me out with this.

This is example FORM that I want simulated login with curl

<form action="http://www.website.com/login?from=login" class="lightbox_form" method="post" name="login">
<input name="authenticity" type="hidden" value="da1249c71258cd02a8bd038cf3ccca1c9ebef155" />
<input type="text" name="login_or_email" size="20" class="input" id="login">
<input type="password" size="20" name="login_password" class="input" id="word_user">
<button type="submit" class="new_submit"><span class="inner">Log In</span></button>
</form>

If see you see below

<input name="authenticity" type="hidden" value="da1249c71258cd02a8bd038cf3ccca1c9ebef155" />

That is auto generated hidden value that changed everytime reloading website.

I'm using CURL but I dont know how to get the hidden field and submitting back in same session. Tried using get_meta_tags but that not working

set_time_limit(0);
$http_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)";
$header = array();
$header[0]  = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[]   = "Cache-Control: max-age=0";
$header[]   = "Connection: keep-alive";
$header[]   = "Keep-Alive: 300";
$header[]   = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[]   = "Accept-Language: en-us,en;q=0.5";
$header[]   = "Pragma: "; // browsers keep this blank.

curl_setopt($ch, CURLOPT_URL, "http://www.website.com/login?from=login");

curl_setopt( $ch, CURLOPT_USERAGENT, $http_agent );
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, ROOT_DIR.'cookies.txt');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, 'login_or_email=mail&login_password=pass&authenticity_token='.$cag.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);


$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);



//print $output;
print_r ($output);

Any ideas?

3 Answers 3

1

I believe that you have to parse the HTML returned by the curl_exec call manually. The curl library does not process HTML for you, it will handle the HTTP stuff but processing the data returned from the server is up to your application.

1

You can use pret_match() to get the random value

//$html is the return of the curl request
preg_match('/<input name="authenticity" type="hidden" value="([^"]*)" \/>/', $html, $match);

the value will be in $match[1].

3
  • yeah thats great but, if I getting random value with curl and send back with curl, Its will regenerate with new one..
    – radiaku
    Mar 5, 2012 at 17:06
  • I did not understand your comment. You have the value of the hidden field in $match[1] so you can use it on the next curl request.
    – botzko
    Mar 6, 2012 at 18:07
  • Yeah I have already $match[1], but when I make CURL again, its will generated with a new one. So I must getting hidden value and send back in one CURL, is that posible?
    – radiaku
    Mar 6, 2012 at 19:04
0
$regex= '/<input name="authenticity" type="hidden" value="([^"]*)" \/>/i';
if ( preg_match($regex, $html, $list) )
   $rand= $list[0];
else 
    print "Not found";
1
  • Welcome to Stack Overflow. Please consider editing your answer to include an explanation as to how your code works, so that it is more useful to future readers :).
    – Matt
    Aug 9, 2015 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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