简体   繁体   中英

PHP CURL login to another server

$cookie="cookie.txt"; 
$data = array();
$data['global_id'] = "raj";
$data['global_password'] = "raj";

foreach ($data as $key => $value){
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);


$curl_connection = curl_init('http://somedomain.com/cgi-bin/login.pl');
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, 0);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_REFERER, 'http://somedomain.com/cgi-bin/login.pl');
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl_connection, CURL_COOKIEFILE, '');
$result = curl_exec($curl_connection);

if($result == "success"){
    header("Location: http://somedomain.com/cgi-bin/index.pl");
}
else{
    echo "Login failed";
}

//close the connection
curl_close($curl_connection);

Whats wrong with it... I cant login

After executing this script if I goto http://somedomain.com/cgi-bin/index.pl it should logged in . How can I do this ?

You should set CURLOPT_RETURNTRANSFER to true . Probably your $result is empty.

Moreover while setting CURLOPT_REFERER you obviously used the wrong variable. ($ch)

EDIT:

It seems as if the following line is missing:

curl_setopt($curl_connection, CURLOPT_POST, true);

Try this:

curl_setopt($ch, CURLOPT_SSLVERSION, 3); // OpenSSL issue
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  // Wildcard certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

Otherwise you should know about SSL configuration of the server. It can be an issue.

Also you can set POST vars as array:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('param1' => 'value1', 'param2' => 'value2'));

UPDATE: And You are trying to use SSL over HTTP .

$curl_connection = curl_init('http://somedomain.com/cgi-bin/login.pl');

Try correcting your URL to HTTPS .

try fix ur curl opt code, like this:

curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURL_COOKIEFILE, $cookie);
curl_setopt($curl_connection, CURL_VERBOSE, true);

hope this helpfull..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM