Do not usedie()
because then the cookie will not be set. Let the method return instead so CI can output cookies and headers.
You do not need the helper, don't load it.
site_url()
does not produce the string you should be using. It includes the protocol, i.e.https://example.com
when all you should use is 'example.com'.
You have the domain and path arguments reversed and as others have said the call is tosetCookie()
as in
$this->response->setCookie('forgetpwd', $token, 3600, example.com);
The expire argument can be the life (in seconds) you want. ThesetCookie()
method will addtime()
to it for you.
I don't supply a path argument because you need the default value of '/'.
From php.net - https://www.php.net/manual/en/function.setcookie.php
Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expires parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
This code works for me. I get the cookie from the request and set it in the response. (Remember to validate the user input before processing it)
<?php
namespace App\Controllers;
use Config\Services;
class Home extends BaseController {
public function setLang($lang = ""){
// remember filter your inputs
Services::response()->setCookie('lang', $lang);
echo("<script>location='/index';</script>");
}
public function index(){
//default value
$lang = "es";
//if empty return NULL.
$tmpLang = Services::request()->getCookie("lang");
if(in_array($tmpLang, ["es", "en", "ja", "pt"])){
$lang = $tmpLang;
}
//show cookie value
echo $lang
}
}
?>
try using to redirect with cookies like
helper('cookie');
set_cookie('cookie_name', 'value', 14400);
return redirect()->to('url')->withCookies();
and on the page try getting by this way
print_r(get_cookie('cookie_name'));
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.