laravel5 set cookie on ajax call - ajax

When the user clicks add to cart I create an new cart and add the product to the cookie of the user. But how do I set the cookie on an ajax response. Im trying:
//set the values for the view make
$cartId = 'someval i set earlyer'
$cookie = Cookie::forever('cartid', $cartId);
$currentCart = Cart::findOrFail($cartId);
$items = CartItem::where('cart_id','=',$currentCart->id)->get();
//this function also check the $request on a valid cookie
$total = $this->calculateCartTotal($request);
return Response::json(View::make('front.cart.render',compact('items', 'total'))->withCookie($cookie)->render());
But the value is never set, I tryd refreshing the page but there is still no cookie for cartid. How can I set a cookie for an ajax reponse

Try this:
return Response::json(
View::make('front.cart.render',compact('items', 'total'))->render()
)->withCookie($cookie);
rendor belongs to View
withCookie belongs to Response
Check Response headers.
Set-Cookie:cartid=...

Related

Session object Not getting clear after log out when calling through postman

I have a page when i login and then do f12 , i copy the request header , cookies and the request url and using post in postman i get the data.
After i logout and refersh the page again i use the same cookie in the postman and post the url data but i still get the data, even if i have done log out i still get the data.
I dont want to get any data after i logged out.i want the cookie to get expire or change when i logout.
If useing postman after logout should not return any data in the response.
Please help me.
Here is my logout code
Dim authCookie As HttpCookie = Request.Cookies(FormsAuthentication.FormsCookieName)
Dim authTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
Session.Abandon()
Session.Clear()
FormsAuthentication.SignOut()
End If
Session.Contents.Clear()
Session.Clear()
Session.RemoveAll()
Session.Abandon()
HttpContext.Session.Clear()
FormsAuthentication.SignOut()
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetNoStore()
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
Response.Cache.SetNoStore()
Response.Expires = 60
Response.ExpiresAbsolute = Now
Response.CacheControl = "no-cache"
HttpContext.User = New GenericPrincipal(New GenericIdentity(String.Empty), Nothing)
Response.Redirect(System.Configuration.ConfigurationManager.AppSettings("root").ToString() + "logon.aspx")

Can't take Laravel cookie that's been set

I have a checkbox in my login form and if it is checked I want to save user's email and password in a cookie so then he'll be automatically logged in. This is the part of my code that checks if the checkbox is checked and sets the cookie.
if ($remember == 'on') {
$array = array(
"email" => $email,
"password" => $password
);
$time = time() + (10 * 365 * 24 * 60 * 60);
Cookie::queue('user', serialize($array), $time);
}
But when I try to get the cookie and print it out it prints "false".
$cookie = Cookie::get('user');
dd(unserialize($cookie));
How can I get the cookie? It shows the cookie that's been set in inspect.
What's the best way to check if the cookie is set so i'll redirect the user to profile view straight away? Do I have to write a middleware for it?
The problem is your dd function call.
Cookies will store in user's browser when he retrieve the response with your Cookies attached (set-cookie headers), and you can read them in the next request that he will send to you.
when you call dd, it breaks the response chain and removes the set-cookie headers.
just remove the dd and in another route write:
dd(unserialize(\Request::cookie('user')));

Laravel can't read a "recently" created cookie

I have a project who authenticate the user using cookies like token_ and refreshToken_, and a middleware who intercept my routes and verify if the user is logged or not.
In my middleware, when i need to renew the token_ I have the following code:
namespace App\Http\Middleware\VerifyAccessToken
$cookie_name = "token_";
$cookie_value = $obj->access_token;
$expires_in = $obj->expires_in;
$time = time() + $expires_in; // 3600 = 1 hora
$path = "/";
$domain = env('COOKIE_DOMAIN');
setcookie($cookie_name, $cookie_value, $time, $path, $domain, false, true);
$cookie_name = "refreshToken_";
$cookie_value = $obj->refresh_token;
setcookie($cookie_name, $cookie_value, $time + 3600, $path, $domain, false, true);
return $next($request);
It works apparently fine, but the problem is:
After the middleware intercep my route and renew the cookie, the request proced to his controller, but there, I can't access the cookie using $_COOKIE['token_'] and I get an error, but if I look in the chrome's inspector, the cookie is there and reloading the page (F5) I can access the cookie in controller
Have a method for me access the cookie in controller without need to go to the view before?
To read the value of Cookie in Laravel, do you need to use:
$token = Cookie::queued('token_');
dd($token->getValue());
https://api.symfony.com/3.0/Symfony/Component/HttpFoundation/Cookie.html

How to set and retrieve cookies in code igniter

I have tried setting cookies in codeigniter like this, but not working don't know what is the problem
//setting cookie
$this->load->helper('cookie');
if($loaded_position == 'nativelocation' ){
$this->input->set_cookie('nativelocation' , $request_place, '86400');
//echo $this->input->cookie('nativelocation');
}
//retrieving cookie
$this->load->helper('cookie');
$savedlocation = array();
if( $this->input->cookie('addlocation_1') )
{
$savedlocation[]= $this->input->cookie('addlocation_1') ;
}
It looks like you are setting a cookie called "nativelocation" but then you try to retrieve a cookie called "'addlocation_1", and there isn't one. Try:
if( $this->input->cookie('nativelocation') ){
echo $this->input->cookie('nativelocation');
}
Also, know that when setting a cookie, you can not access it on the same "page" you'll need a page refresh, or access the cookie $this->input->cookie('nativelocation') on another page/controller.

XmlHttpRequest in Symfony2

Here follows my problem. In a Symfony2.1 controller I receive an Ajax call. If I use the Request object I'm not able to get the sent param. If I use the PHP _REQUEST object the param is found!
I can confirm that the request is XHR, if it matters.
Here is my code:
public function savedataAction(Request $request){
if($request->isXmlHttpRequest())
echo 'Ajax Call';
$param1 = $request->request->get('myParam'); // Nothing is returned, but $request is obviosly not null
$param2 = $_REQUEST['myParam']; // The value is given
....
}
Any idea?
PS: If helps, notice that the AJAX call is sent by the file uploader jQuery plugin provided by Valums.
Normally its:
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
Look here are the fundamentels.
http://symfony.com/doc/current/book/http_fundamentals.html
Edit:
$request in your case is only filled when you send the form from the Symfony2 site. It is possible, that the CSRF protection block the request.
Try this in your controller to get the Request object:
$request = $this->get('request');
http://symfony2forum.org/threads/5-Using-Symfony2-jQuery-and-Ajax

Resources