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

set_cookie - not working #1103

Closed
dayrui opened this issue Jul 16, 2018 · 8 comments
Closed

set_cookie - not working #1103

dayrui opened this issue Jul 16, 2018 · 8 comments

Comments

@dayrui
Copy link

dayrui commented Jul 16, 2018

File: System/Helpers/cookie_helper.php
function set_cookie($name, string $value = '', string $expire = '', string $domain = '', string $path = '/', string $prefix = '', bool $secure = false, bool $httpOnly = false)
{
// The following line shows as a syntax error in NetBeans IDE
//(\Config\Services::response())->setcookie
$response = \Config\Services::response();
$response->setcookie($name, $value, $expire, $domain, $path, $prefix, $secure, $httpOnly);
// Add
$response->sendCookies(); // public
}

File:/System/HTTP/Response.php
public function setCookie( .....) {
.....
$this->cookies[] = [
'name' => $prefix.$name,
'value' => $value,
'expires' => $expire,
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $httponly,
];

	return $this; // GO sendCookies()

}

@lonnieezell

@dayrui
Copy link
Author

dayrui commented Jul 16, 2018

Cookie , No save action

@lonnieezell
Copy link
Member

cookies are set later in the process.

Can you provide info so I can recreate the problem, with sample code? Saying "it doesn't work" isn't really helpful :)

@dayrui
Copy link
Author

dayrui commented Jul 16, 2018

`

1, 'msg' => 'success'])) } //-------------------------------------------------------------------- } ` @lonnieezell

@dayrui
Copy link
Author

dayrui commented Jul 16, 2018

`<?php namespace App\Controllers;

use CodeIgniter\Controller;

class Save extends Controller
{
public function index()
{

	helper('cookie');

    set_cookie('userid', '123', 99999);

    exit(json_encode(['code' => 1, 'msg' => 'success']))
}

//--------------------------------------------------------------------

}
`

@lonnieezell
Copy link
Member

The problem is that you're exiting the script before it has a chance to finish. Instead of existing, check out Setting the Output in the manual.

Basically, do this instead:

$this->response->setJson(['code' => 1, 'msg' => 'success']);

@dayrui
Copy link
Author

dayrui commented Jul 16, 2018

Thanks very much

@byazrail
Copy link
Contributor

@lonnieezell system/HTTP/Response.php;
setJSON two return parameter.

public function setJSON($body)
{
	$this->body = $this->formatBody($body, 'json');
	return $this;
	return $this;
}

@lonnieezell
Copy link
Member

Well - doesn't hurt anything :) Good catch. I'll take care of it, thanks.

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

3 participants