0


I wanted to submit a form with php with special values but it's not working here is the code :

<?php
//create array of data to be posted
$post_data['username'] = 'something';
$post_data['password'] = 'something!';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
   $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =curl_init('http://www.parsdata.com/');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
                curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
?>

And this is what is returned :

Array ( [url] => http://www.parsdata.com/ [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 328 [request_size] => 245 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 3.876425 [namelookup_time] => 0.031218 [connect_time] => 0.238176 [pretransfer_time] => 0.23823 [size_upload] => 56 [size_download] => 72015 [speed_download] => 18577 [speed_upload] => 14 [download_content_length] => 72015 [upload_content_length] => 56 [starttransfer_time] => 1.703701 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 217.66.216.91 [primary_port] => 80 [local_ip] => 23.236.155.242 [local_port] => 43417 [redirect_url] => ) 0-

There wasn't any error but it doesn't work .
I really don't know what to do.
If you need a real user and pass I can give you !
Thank you in advance .

Ali
  • 160
  • 1
  • 12
  • 3
    What exactly is ment by "does not work"? What is stored in $result? – Marcel Oct 08 '14 at 12:38
  • The website you're probably trying to submit data to has methods in place to prevent you from doing this very thing. It's bad internet practice – sjagr Oct 08 '14 at 12:38
  • 2
    Depending on the username / password, you may need to `URLencode` the values. – blue112 Oct 08 '14 at 12:39
  • @Marcel in $result is the exact page http://www.parsdata.com/ not the redirected link and that's the problem . – Ali Oct 08 '14 at 12:42
  • @sjagr There is nothing preventing us but it make some cookies and I don't know if the curl get it or not and also we will be redirected automatically when loging in . – Ali Oct 08 '14 at 12:44
  • @blue112 I encoded it . Still not working. The problem is that it doesn't follow the redirect I think. – Ali Oct 08 '14 at 12:48

3 Answers3

1

You should use the correct URL for login. I visited the website and I found this URL for login - http://members.parsdata.com/default.aspx?dll=user&ctl=autosignin

Yash Sodha
  • 713
  • 3
  • 13
  • I test it on : http://kghaz.ir/project/new/KeyPHP/auto.php Still there is a problem that it redirects to http://kghaz.ir/default.aspx?dll=system&ctl=dashboard&a=1&cul=fa-IR instead of : https://members.parsdata.com/default.aspx?dll=system&ctl=dashboard&a=1&cul=fa-IR How can I fix this ? – Ali Oct 08 '14 at 15:04
  • Then you should do what @blue112 suggested:- If you want to login to a website, all you need to do is to parse the Set-Cookie header in the response, and to pass the Cookie header to every request you'll do after that. – Yash Sodha Oct 08 '14 at 15:11
  • I add this for parsing cookies didn't work ! curl_setopt( $curl_handle, CURLOPT_COOKIESESSION, true ); curl_setopt( $curl_handle, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt' ); curl_setopt( $curl_handle, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt' ); – Ali Oct 09 '14 at 11:41
1

Actually, it may works.

If you want to login to a website, all you need to do is to parse the Set-Cookie header in the response, and to pass the Cookie header to every request you'll do after that.

You can use a cookie jar to do that, more informations here : PHP Curl And Cookies

Community
  • 1
  • 1
blue112
  • 52,634
  • 3
  • 45
  • 54
0

If you want to login to a website, all you need to do is to parse the Set-Cookie header in the response, and to pass the Cookie header to every request you'll do after dat.

Rahul Shah
  • 19
  • 2