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

Bug: Cookie Helper and Response class issue #2783

Closed
MisterAnmar opened this issue Mar 31, 2020 · 9 comments
Closed

Bug: Cookie Helper and Response class issue #2783

MisterAnmar opened this issue Mar 31, 2020 · 9 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@MisterAnmar
Copy link

MisterAnmar commented Mar 31, 2020

Description
Im trying to create a cookie using response class as per the user guide.

`

$cookie = [
        'name'   => 'Something',
        'value'  => 'The Value',
        'expire' => '86500',
        'domain' => '.some-domain.com',
        'path'   => '/',
        'prefix' => 'myprefix_',
        'secure' => TRUE,
        'httponly' => FALSE
];

$this->response->setCookie($cookie);

`
of course data where different

try to retrieve the cookie i cant

$this->request->getCookie('Something');

Also i tried using helper. i just cant to seem to make it work..
However when i use the native php function it works like a charm

setcookie('mya', 'Some Value', time()+3600, '/');

and on a different function i just do


	public function testc()
	{
               // Does not work
		echo $this->request->getCookie('mya');
		// Works fine
                 if (isset($_COOKIE['mya'])) {
		 	echo $_COOKIE['mya'];
		 }
	}


CodeIgniter 4 version
CI4.0.2 Appstarter build using composer

Context

  • OS: Win10
  • Web server WAMP64
  • PHP version 7.3.5
@MisterAnmar MisterAnmar added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 31, 2020
@MGatner
Copy link
Member

MGatner commented Mar 31, 2020

setCookie() is a method for Response, not Request! I think this isn't a bug so much as just an honest mistake :)

@MGatner MGatner closed this as completed Mar 31, 2020
@MGatner
Copy link
Member

MGatner commented Mar 31, 2020

Oops! Honest mistake is mine, I was looking at your getCookie() code. Looking into this...

@MGatner MGatner reopened this Mar 31, 2020
@MGatner
Copy link
Member

MGatner commented Mar 31, 2020

Hard to tell with your test data, but it looks like you might be setting a per-cookie prefix and then not requesting the same prefix with getCookie(). In your example, you would either need your App config cookiePrefix set to 'my_prefix_' or you would need to request the cookie like this: $this->request->getCookie('Something', 'myprefix_');

@MisterAnmar
Copy link
Author

Ok MGatner i give it a try...

@MisterAnmar
Copy link
Author

MisterAnmar commented Mar 31, 2020

Ok i gave it a try.. it does not work,..

         public function index()
	     {
		$cookie = [
		        'name'   => 'ccc',
		        'value'  => 'The Value',
		        'expire' => '86500',
		        'domain' => '.some-domain.com',
		        'path'   => '/',
		        'prefix' => 'myprefix_',
		        'secure' => TRUE,
		        'httponly' => FALSE
		];

		$this->response->setCookie($cookie);
             }

and my 2nd method

public function testc()
{
           // This line gives me the default ci_session ID
	var_dump($this->response->getCookie());
            // Does not seem to work
	echo $this->request->getCookie('ccc');
	echo $this->response->getCookie('ccc');
	echo $this->response->getCookie('ccc', 'myprefix_');
            echo $this->request->getCookie('ccc', 'myprefix_');
}

i also tried without myprefix no luck.
Im just following the manual . Working with cookies

@samsonasik
Copy link
Member

It seems caused by the definition of "secure" => TRUE,, I tried with "secure" => FALSE in localhost and it working fine.

@samsonasik
Copy link
Member

could you try define ?

'secure' => $this->request->isSecure(),

@lonnieezell
Copy link
Member

I have just tested using the helpers (which in turn use the current request/response) and this simple test works. I think @samsonasik is spot on here.

public function index()
	{
		helper('cookie');

		$cookie = get_cookie('foo');
		d($cookie);

		set_cookie([
			'name' => 'foo',
			'value' => 'bar'. time(),
			'expire' => \time() + 3600,
			'httponly' => FALSE
		]);

		return view('welcome_message');
	}

Are you still having problems or did his suggestion clear it up?

@michalsn
Copy link
Member

This seems like resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

5 participants