how to remove a cookie from the $request in Laravel - cookies

I am passing a cookie in the middleware between requests using
$cookie = Cookie::make('key', 'some-key, 10);
$next = $next($request);
$next->withCookie($cookie);
But am having problem removing the cookie from the $request after I retrieve its value. I can't find any command or helper in the docs to achieve this.

You cannot enforce all clients to delete a cookie. What you can do is set the cookie value to an empty string and set a short expiry time.

Related

TYPO3 how to set custom cookie inside a form finisher

typo3 9.5.8
we are implementing a newsletter subscription flow with multiple steps, on submit of the second step we query graphQL in a finisher to see if the Email is valid to subscribe or not - and we set a cookie with the "state" of the email address.
setcookie("isEmailSubscribable", ($content->data->isEmailSubscribable) ? "1" : "0", time() - 3600, "/", "x.at", false);
we have to display a message on the third step based on that "state" written into a cookie. but no matter what i try the cookie does not get set (i guess).
whats the deal with cookies and typo3? is it to late to set a cookie inside a form finisher? but if yes how could i solve this?
help is much appreciated
Inside the Finisher:
// Set a cookie
$this->getTypoScriptFrontendController()->fe_user->setKey('ses', 'value', 'test');
// Get the cookie
$this->getTypoScriptFrontendController()->fe_user->getKey('ses', 'test');
ses are Session cookies
user This cookies require a fe_user to be logged in
Value can be an array, it will be stored serialized in the Database fe_sessions.ses_data
UPDATE:
Or you can try it with an PSR-15 Middleware: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/RequestHandling/Index.html
In your Middleware Class you get a $request and $response and use them to set or read a cookie:
// Write a cookie
$response = $response->withAddedHeader(
'Set-Cookie',
$cookieName . '=' . $yourValue . '; Path=/; Max-Age=' . (time()+60*60*24*30)
);
// Read a cookie
$request->getCookieParams()[$cookieName];
You just have to check request for email address and maybe an hidden field to detect that your for was submitted.

Display username in top div using cookies in laravel 5.5

"Hi, I am using Laravel 5.5 I want to display username in top div, I am not using Auth, writing logic manually. Also can any one tell me how to use cookies to store username ? Thanks in advance."
You can do
// Creating a cookie, with a basic value.
$cookie = cookie('key', 'value');
// Retrieving a cookie.
$cookie = cookie('key');
You can read the documentation here

Reading value from cookie in asp.net core

I have a problem with reading cookies. In first request I add cookie with method
Response.Cookies.Append("UserName", "Name", new Microsoft.AspNetCore.Http.CookieOptions()
{
Path = "/",
HttpOnly = false,
Secure = false
});
In next request I want to read value from cookies
var name = Request.Cookies["UserName"]
but Request.Cookies is null. But also, when I call this method by typing api in browser
localhost:5555/api/tempController/getCurrenUserName
var name = Request.Cookies["UserName"] returns me a value and Request.Cookies contains all cookies, which I see in browser.
I don't understand, why in one case it works in another does not work.
the problem was with method fetch in knockout.js, I replaced it with jQuery methods and now it works!!!

UWP WebRequest Replacement HttpClient Cookie?

As i am either too dump to find the proper answer or it is simply not out there ... how the hek i replace the "outdated" WebRequest properly with the HttpClient "replacement"?
In the WebRequest i tendet to serialize & analyze the actual cookie as the webpage returns a partial JSON cookie ... however ... i still did not found a way to get a proper CookieContainer (or whatever form of cookie) from the frking HttpClient ... also ... every google request leads me to 20000000 years old answers or outdated documents (+ some upToDate docs which all just refer to "GET" requests without any cookies involved -.-*))
would be kindfull if somebody could lead me to the correct path ...
thx
greets
X39
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();
client.DefaultRequestHeaders.UserAgent.TryParseAdd(app.Settings.UserAgent);
var response = await client.PostAsync(new Uri(app.Settings.Pr0grammUrl.Api + "user/login"), new Windows.Web.Http.HttpStringContent(postDataBuilder.ToString()));
By default, HttpClient handles cookies by itself through the default HttpBaseProtocolFilter. You can get cookies associated with a URI through GetCookies method of the HttpCookieManager class:
Gets an HttpCookieCollection that contains the HttpCookie instances
that are associated with a specific URI.
using (var protocolFilter = new HttpBaseProtocolFilter()) {
var cookieManager = protocolFilter.CookieManager;
var cookies = cookieManager.GetCookies(uri);
foreach (var cookie in cookies) {
// Here is each cookie
}
}
You should also be able to set/get cookies through HTTP request and response headers. To disallow HttpClient from handling cookies by itself, create an instance of HttpBaseProtocolFilter and set the CookieUsageBehavior to HttpCookieUsageBehavior.NoCookies:
NoCookies: Do not handle cookies automatically.
// Create http filter
httpFilter = new HttpBaseProtocolFilter();
httpFilter.CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies;
// Create http client
httpClient = new HttpClient(httpFilter);
// Handle cookies through HTTP headers

Read and Write Cookie in Symfony2

I want to store some information in the local browser cookie. After hours looking for a nice tutorial, I managed to store some data in a non-session cookie:
controller - indexAction()
$cookieGuest = array(
'name' => 'mycookie',
'value' => 'testval',
'path' => $this->generateUrl('my_route'),
'time' => time() + 3600 * 24 * 7
);
$cookie = new Cookie($cookieGuest['name'], $cookieGuest['value'], $cookieGuest['time'], $cookieGuest['path']);
$response = new Response();
$response->headers->setCookie($cookie);
$response->send();
I wonder if this is the correct way. Furthermore I tried several ways to read the cookie with the HttpFoundation Component, but without success. Is there another way than accessing the cookie via $_COOKIE['mycookie'] ?
Here is where I try to read the cookie
controller - cookieAction()
public function cookieAction($_locale, $branch, $page)
{
$response = new Response();
$cookies = $response->headers->getCookies();
var_dump($cookies);
// TODO: Get params for indexAction from cookie if available
return $this->indexAction($_locale, $branch, $page);
}
This is the correct way of setting cookie.
To read cookie already written in the browser do:
$request->cookies->get('myCookie');
But after I created cookie in the $response object:
$cookie = new Cookie('myCookie', 'contentOfMyCookie');
$response = new Response();
$response->headers->setCookie($cookie);
I call this method:
$response->headers->getCookies();
I get an array of cookies, which are to be written in the browser - not those already existing there.
Figuratively, between $request and $response there is a time of executing controller's code.
Besides, in a twig template you can use
{{ app.request.cookies.get('myCookie') }}
you thus get value of the cookie already written in the browser, not that from the $response object! Newly created cookie from the browser you can read only after having reloaded page (ajax doesn't need to reload whole page).
To sum it up, you can read cookies using $request object, and create them with $response object. (Obviously, for some reasons, you can also read $response object cookies - but these are rather rare situations).
$response->headers->getCookies();
should return an array of cookies look in ResponseHeaderBag class for more information about that function
this can be useful for someone trying to make cookies in symfony2 :
use Symfony\Component\HttpFoundation\Cookie;
Example how to use Cookies and Session:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function indexAction()
{
// Set session value
$session = $this->getRequest()->getSession();
$session->set('test', 1);
// Get session value
$value = $session->get('test');
// Set cookie value
$response = new Response();
$cookie = new Cookie('test', 1, time()+3600);
$response->headers->setCookie($cookie);
// Get cookie value
$this->getRequest()->cookies->get('test');
}
}
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
// set current active tab in cookie
$cookie = new Cookie('myProfileActiveTab', 'myaddress', strtotime('now + 60 minutes'));
$response = new Response();
$response->headers->setCookie($cookie);
$response->send();
// get current active tab from cookies
$cookies = $request->cookies;
if ($cookies->has('myProfileActiveTab')) {
$activetab = $cookies->get('myProfileActiveTab');
}
More interesting cookies information links (http_fundation component for symfony2):
Symfony2 http_fundation component
Symfony2 http_fundation api
Symfony2 http_fundation component (spanish)