简体   繁体   中英

PHP CURL - Session expired

I'm trying to make an voip call with PHP CURL and MEGAVOIP. The problem is i can't manage the session to access the page protected by a password. I looked which variables are posted to the login page to post it with Curl. But my code doesn't work.

Following Colin Morelli and Waygood's advices, I just added those lines in both commands:

curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);

but it's still the same:

Megavoip returns: SESSION EXPIRED

So here is my full code:

<?php
ini_set("display_errors", 1);  
$username="***"; 
$password="***"; 
$url="https://www.megavoip.com/login"; 
$url2="https://www.megavoip.com/phone_to_phone/";
$timeout = 10;
$cookies_file = 'cookies.txt';

// HERE I GET THE TOKEN

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);


preg_match_all('/<input[^>]+>/i',$content, $result); 
preg_match_all('/(id|value)=("[^"]*")/i',$result[0][5], $img);
$img1=str_replace('"', '', $img[0][0]);
$img2=str_replace('"', '', $img[0][1]);
$img1=substr($img1,3);
$img2=substr($img1,6);
$postdata = "login%5Busername%5D=".$username."&login%5Bpassword%5D=".$password."&page_referrer=login&".$img1."=".$img2; 

// HERE I SEND THE VARIABLES

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

$content = curl_exec($ch);

// IF LOGGED HERE I'LL MAKE THE CALL

curl_close($ch);

echo $content;
exit;
?>

Any ideas to help me?

This is a test account so feel free to use my login and password if you want to have a look on this and help me!

Thank you a lot in advance.

Ok you need to forward the cookie/session after login,

you need to first extract the cookie from Header after login like following

// HERE I GET THE TOKEN

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);    
.... .... 
.... ....
$content = curl_exec($ch);

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $content, $m);
parse_str($m[1], $cookies);
$cookie = $cookies['NAMEOFCOOKIEUNEEDHERE'];

After that need to use $cookie variable in curl options like that

// HERE I SEND THE VARIABLES

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, 'NAMEOFCOOKIEUNEEDHERE='.$cookie);

I hope your problem will be resolved.

Thanks

Moin

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