For one of my Selenium Cests I want to set a cookie but this is simply not working. In order to find the problem I reduced my code to the absolut minimum and to my surprise setting cookies seems not to work at all.
/**
* Test if we can set simple cookies
*
* #param \AcceptanceTester $i
* #throws Exception
*/
public function settingCookieSetsCookie(AcceptanceTester $i)
{
$cookieDefaultParams = [
'path' => '/',
'secure' => false,
'httpOnly' => false,
'expiry' => 900,
'domain' => 'www.testdomain.local'
];
$i->amOnPage('/cookieCest.php');
$i->setCookie('example', 'myvalue', $cookieDefaultParams);
$i->amOnPage('/cookieCest.php'); // reload page
$cookieValue = $i->grabCookie('example');
$i->assertEquals('myvalue', $cookieValue);
}
For testing purposes I set up a local test domain www.testdomain.local which works fine.
Unfortunately the above test fails with the following error:
There was 1 failure:
1) BackendLoginCest: Setting cookie sets cookie Test codeception\acceptance\CookieCest.php:settingCookieSetsCookie Step
Assert equals "myvalue",null Fail Failed asserting that null matches
expected 'myvalue'.
Scenario Steps:
$I->assertEquals("myvalue",null) at codeception\acceptance\CookieCest.php:36
$I->grabCookie("example") at codeception\acceptance\CookieCest.php:35
$I->amOnPage("/cookieCest.php") at codeception\acceptance\CookieCest.php:34
$I->setCookie("example","myvalue",{"path":"/","secure":false,"httpOnly":false,"expiry":900,"domain":"www.new-ep...})
at codeception\acceptance\CookieCest.php:33
$I->amOnPage("/cookieCest.php") at codeception\acceptance\CookieCest.php:32
FAILURES! Tests: 1, Assertions: 1, Failures: 1.`
As far as I can see the cookie params configuration looks good (and seems to be needed, as leaving it out results in an error).
I am currently using selenium-server-standalone-3.141.59.jar but tried older versions which produce the same problem.
The referenced file cookieCest.php is just a simple script which var_dumps $_COOKIE so I can see that there are no cookie values available in PHP.
Does anyone experience a similar problem and knows how to handle it?
Your code should work. What may be a solution is that the page is not loaded when you want to set the cookie so it fails there.
Also if you want to check your cookie you can use the function seeCookie like:
Try this solution and let me know if it did any change.
$i->amOnPage('/cookieCest.php');
$i->wait(5);
$i->setCookie('example', 'myvalue', $cookieDefaultParams);
$i->seeCookie('example');
Try this test with older Chrome version (e.g. 66).
I think all cookie related stuff are broken with last several Chrome versions.
Related
Using PHP 5.5.12
Using CakePHP 2.6.7
Running
debug($url); // output = "http://google.co.uk"
debug(parse_url($url));
/* output = array(
'host' => '*****',
'scheme' => 'http'
)
*/
I had been using this without trouble but now copy/pasting a section of my code to have it as method (to save repeating myself) has started giving me this output. But testing it back in the same place I had it originally gives me this output too.
Can anyone explain why the hostname is stars and why the rest of the array doesn't appear (I realise all other elements should be expected to be NULL)?
Edit
Just tried it again with a url that had a path to a page after the host. The path shows up fine but the host is still starred out.
Partial Answer
Just thought to try debug(parse_url($url)['host']) and it prints the host correctly. I realised that the other elements would only be set if they exist in the url.
However, can anyone explain why printing out the array prints several stars instead of the hostname even though it is definitely stored there?
The reason this happens is because of how debug() works. Many moons ago people were not pleased that they could accidentally have their database credentials dumped out in error pages (which use the same underlying code as debug()). Because of this, debug() and Debugger::export() blacklist a set of array keys that could have database credentials. The following keys are replaced with ***'s:
password
login
host
database
port
prefix
schema
I'm using AWS SimpleDB for my site, however if I udpate an attribute with something completely different, searching that property with either the new value or the old value are both returning the same record.
Let's say the 'login' property's current value is 'dev'. I then change that value to 'myvar'.
$response = $this->simpledb->select(vsprintf(select * from mydomain where login='%s',array('myvar')),array('ConsistentRead' => 'true'));
# returns the newly updated row
$response = $this->simpledb->select(vsprintf(select * from mydomain where login='%s',array('dev')),array('ConsistentRead' => 'true'));
# returns the same row even though 'login' has changed
Am I doing something wrong with the consistent read argument? I have no clue why this is happening. Also, it's been about a half hour and this issue is still happening, I highly doubt it takes AWS that long to propagate changes across servers.
Anyone have any ideas?
I did not realize this at the time, but I was using v1 of the SDK, after updating to V2 all consistency issues were solved.
I'm trying to login into a website using PHPQuery's WebBrowser plugin. I'm able to successfully login but I'm not sure how to reuse cookies from a previous call to the next.
$client = phpQuery::browserGet('https://website.com/login', 'success1');
function success1($browser) {
$handle = $browser
->WebBrowser('success2');
$handle
->find('input[name=name]')
->val('username');
$handle
->find('input[name=pass]')
->val('password')
->parents('form')
->submit();
}
function success2($browser) {
print $browser; // prints page showing I'm logged in
// make authenticated requests here
}
How do I make other requests with session/login cookies?
I've taken a look at the source code to aid you with this problem. My first impression was that the code was very poorly written. Debugging code commented out, typos all over the place, mile-long functions, etc. You really might want to consider switching to a different solution in the long run because if the author changes something in this code, you might end up having your own code broken with an upgrade.
That being said, the WebBrowser plugin gives you access to the browser object itself, which contains a function called getLastResponse(). This returns a Zend_Http_Response object, which you can theoretically use to get the cookies.
The problem is that you don't have any way of setting those cookies. You would have to patch the web browser plugin somewhere around line 102 to include your own HTTP request object (parameter 2 for phpQuery::ajax()) with your cookies set, around here:
$xhr = phpQuery::ajax(array(
'type' => 'GET',
'url' => $url,
'dataType' => 'html',
));
Alternatively you could also patch phpQuery.php line 691 to include a global cookie jar you could define as a singleton or so. (Right where it says $client->setCookieJar();).
Again, this code is very poorly written, you are probably MUCH better off using raw curl calls, even if it lacks a bit of functionality.
I don't know anything about agavi and there might be a lot of error in this..
I want to set up a cookie:
$this->getResponse()->setCookie( ID, 'ID=2342&ClickBannerID=634&SubID=&ClickDateTime=' + time(), mixed $lifetime = time() * 2, string $path = '/', string $domain = 'mydomain', bool $secure = null)
I get:
Parse error: syntax error, unexpected T_VARIABLE
it's form: http://www.agavi.org/apidocs/index.html
what am I doing wrong
Usually within Agavi you set cookies on an AgaviView on the AgaviWebResponse object via its setCookie method. The method signature is as follows:
public function setCookie($name, $value, $lifetime = null, $path = null, $domain = null, $secure = null, $httponly = null)
This means, that you can set a simple cookie using the following syntax:
$this->getResponse()->setCookie('cookieName', $cookieValue);
You may use PHP's strtotime function to set an easily readable lifetime for the cookie if you want a cookie that is only valid for a certain amount of time:
$this->getResponse()->setCookie('cookieName', $cookieValue, '+14 days');
As your question states you may want to use the additional parameters (domain etc.) as well. Reading your question it may also be possible, that you want to set multiple cookies instead of one consisting of a string containing ID, ClickBannerId etc., but only you and your application (or your dev) can tell that.
Please note, that you can set the additional parameters per cookie or configure some sane defaults for your application in the app/config/factories.xml file per environment (usually for context _web_) to save on typing in your views:
<response class="AgaviWebResponse">
<ae:parameters name="cookie_httponly">true</ae:parameters>
</response>
The valid parameter names are:
cookie_lifetime (lifetime in seconds)
cookie_path (path on the server the cookie should be available for)
cookie_domain (the domain the cookie is available on)
cookie_secure (set this to true if the cookie should only be available via HTTPS)
cookie_httponly (determine whether the cookie should only be allowed to use via HTTP and not client side scripts)
Please note, that the ae namespace was not necessary in older Agavi versions and thus it may be needed that you use parameter instead of ae:parameter.
To remove a cookie you just call unsetCookie with all the same parameters you used for setting the cookie. To get a cookie value simply call getCookie($name). As cookies are untrusted user provided information usually Agavi requires you to validate all incoming parameters, files, headers and cookies before you can access them in your actions and views. This means, that you may need to validate your cookie prior to accessing its value. You do this via a validate.xml file:
<validator class="string" source="cookie" required="false">
<argument>cookieName</argument>
</validator>
This example is simplified and should not be used in production. Use source _cookie_ and the name of the cookie for the argument and then validate your cookie value according to your rules (format etc.). It may be necessary to write a custom validator if the builtin AgaviValidator classes (like string, regex etc.) are not sufficient, but this is a topic for another day. You may find the Agavi User FAQ somewhat helpful. Sorry for the shameless plug and good luck with your problem at hand. :-)
you mixed function declaration with its run
$this->getResponse()->setCookie( 'your-cookie-id','ID=2342&ClickBannerID=634&SubID=&ClickDateTime=' . time(), time() * 2, '/',
'mydomain', null)
and as OcuS stated in comment - probably hire a PHP developer
I'm trying to create a cookie with codeigniter for like 2 days -.- (I was to ashame to ask the question before...)
Anyone care to explain me what is wrong with this code:
$websiteUrl = preg_replace("/^[\w]{2,6}:\/\/([\w\d\.\-]+).*$/","$1", base_url());
$this->load->helper('cookie');
$cookie = array(
'name' => 'rememberMe',
'value' => $this->encrypt->encode(serialize($serialize)),
'expire' => (time() + $this->config->item('remember_me')),
'domain' => '.'.$websiteUrl,
'path' => '/',
'prefix' => 'chv_',
'secure' => false,
);
set_cookie($cookie);
$this->input->set_cookie($cookie)
($this is CI instance)
Docs: http://codeigniter.com/user_guide/helpers/cookie_helper.html
Make sure $this->config->item('remember_me') > 0
I was working with CI many times and i was always using native setcookie() function, because i really don't need any framework to set cookie (it's simple operation)... But according to documentation using CI instance and input CI->input->set_cookie() should do the job. Remember NO FRAMEWORK is 100% perfect working... It's only framework... You can debug step-by-step CI code to see what happens.
(from comment)
Make sure that there is no extra whitespace being loaded before you are running set_cookie that is preventing the cookie header from being sent to the browser. I have spent many an hour tracking down that issue and found i had an extra space at the end of a closing tag somewhere. If you turn E_WARNING on, this should reveal the issue.