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

Unable to create cookies with session=false #675

Open
divinity76 opened this issue Jan 6, 2025 · 1 comment · May be fixed by #676
Open

Unable to create cookies with session=false #675

divinity76 opened this issue Jan 6, 2025 · 1 comment · May be fixed by #676

Comments

@divinity76
Copy link
Contributor

The following code:

<?php

declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_error_handler(
    function (int $errno, string $errstr, string $errfile, int $errline) {
        if (error_reporting() & $errno) {
            throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
        }
    }
);
require_once 'vendor/autoload.php';
$factory = new \HeadlessChromium\BrowserFactory();
$browser = $factory->createBrowser(array(
    'customFlags' => [
        '--no-sandbox',
        '--disable-gpu-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-breakpad',
        '--disable-crash-reporter',
        '--no-crashpad',
    ],
));
$page = $browser->createPage();
$page->setCookies([
    \HeadlessChromium\Cookies\Cookie::create("session_false", "session_false", ["session" => false, "domain" => "example.com"]),
    \HeadlessChromium\Cookies\Cookie::create("session_true", "session_true", ["session" => true, "domain" => "example.com"]),
])->await();
$cookies = $page->getAllCookies();
foreach ($cookies as $cookie) {
    var_dump($cookie->getName(), $cookie->offsetGet("session"));
}

produce this output:

string(13) "session_false"
bool(true)
string(12) "session_true"
bool(true)

but I expected this output instead:

string(13) "session_false"
bool(false)
string(12) "session_true"
bool(true)
@divinity76
Copy link
Contributor Author

Turns out you can't directly set session=false, but session will be set to false if the cookie has an expiration date, meaning if the code is changed to

$page->setCookies([
    \HeadlessChromium\Cookies\Cookie::create("session_false", "session_false", ["expires" => PHP_INT_MAX, "domain" => "example.com"]),
    \HeadlessChromium\Cookies\Cookie::create("session_true", "session_true", ["session" => true, "domain" => "example.com"]),
])->await();

it will print the expected

string(13) "session_false"
bool(false)
string(12) "session_true"
bool(true)

🤔 not a chrome-php bug at least, this is how the DevTools protocol is designed.

We could make a convenience wrapper tho

divinity76 added a commit to divinity76/chrome that referenced this issue Jan 7, 2025
Previously
```php
$page->setCookies([
    \HeadlessChromium\Cookies\Cookie::create("session_false", "session_false", ["session" => false, "domain" => "example.com"]),
    \HeadlessChromium\Cookies\Cookie::create("session_true", "session_true", ["session" => true, "domain" => "example.com"]),
])->await();
```
would create 2 cookies with session=true, but now it will create 1 cookie with session=false and 1 with session=true

close chrome-phpGH-675
@divinity76 divinity76 linked a pull request Jan 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant