Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save the login state , so that do not have to log in again and again #22

Open
ShayanArifButt opened this issue Sep 6, 2017 · 1 comment
Assignees

Comments

@ShayanArifButt
Copy link

ShayanArifButt commented Sep 6, 2017

So i am trying to save the login state , by getting the cookie and saving it ;

$cookieArray = $windowObj->getCookies(); file_put_contents(__DIR__ .'/tmp/cookies/cookies.ser', serialize($cookieArray));

image

and then on the next script run, load that cookie file again , loop over it , but the login page still shows , here is my code;

`//open the login page:
$myUrl = "https://www.amazon.com/gp/css/account/address/view.html?ie=UTF8&ref_=myab_view_new_address_form&viewID=newAddress";
$browserObj = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs');

$windowObj      = $browserObj->getNewWindow();
	
$agentName	= "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 OPR/46.0.2597.57";
$windowObj->setUserAgent($agentName);
	
$windowObj->setUrl($myUrl);

//read array
$cookieArray = unserialize(file_get_contents(__DIR__ . '/tmp/cookies/cookies.ser'));

foreach($cookieArray as $key=>$value){
	
	/* echo "<pre>";
	var_dump($cookieArray[$key]);
	echo "</pre>"; */
	
	$name			= $cookieArray[$key]['name']; //String: mandetory cookie name.
	$value			= $cookieArray[$key]['value']; //String: mandetory cookie value.
	$domain			= $cookieArray[$key]['domain']; //String: defaults to current domain in window URL
	$path			= $cookieArray[$key]['path']; //String: defaults to "/";
	$expireTime		= $cookieArray[$key]['expiry']; //Int (epoch): default is 2147483647
	$serverOnly		= false; //Bool: default false; if true cookie cannot be read by client scripts.
	$secureOnly		= false; //Bool: default false; if true cookie can only be transmitted using secure (defined by the user agent, i.e. TLS) channel.

	$windowObj->setCookie($name, $value, $domain, $path, $expireTime, $serverOnly, $secureOnly);
	
}

//perform a screenshot:
$screenshotData	= $windowObj->screenshot();

//render it:
echo '<img src="data:image/png;base64,' . base64_encode($screenshotData) . '" />';`
@merlinthemagic
Copy link
Owner

merlinthemagic commented Sep 6, 2017

after setting all the cookies, try doing another getCookies() and compare the saved array to the one you set. like this:

$savedCookies = unserialize(file_get_contents(DIR . '/tmp/cookies/cookies.ser'));
foreach($savedCookies as $key=>$value){
//... your logic
}
$newCookies = $windowObj->getCookies();

//then compare the saved to the current
var_dump($savedCookies);
var_dump($newCookies);

Chances are the domain attribute is wrong for one of more cookies (see this issue )

#18

@merlinthemagic merlinthemagic self-assigned this Sep 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants