简体   繁体   中英

How to set cookie using php through $.post request

i'm trying to set cookie in a $.post request.

My problem is that i want to delete cookie on browser close so i try this:

$.post("cookie.php",{name:name,surname:surname,usn:usn,psw:psw},function(msg){
   ...
});

cookie.php:

setcookie("code",$name,"/");

// i don't add expiration time because i read on the web that in this way cookies will be deleted on browser close.

If i don't add / parameter ( i read ) my cookie are not applied to every path..

but in this way i retrieve this:

Warning:  setcookie() expects parameter 3 to be long, string given in ....

how can i fix it?

According to the manual: http://php.net/manual/en/function.setcookie.php

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

setcookie("code",$name, 0, "/");

The actual problem is that you are trying to pass parameter 4 without having passed parameter 3, so the compiler can't understand what you mean.

Set parameter 3 like so:

setcookie("code",$name, 0, "/");

you can use jquery cookie plugin

https://github.com/carhartl/jquery-cookie

Using.

$.cookie('the_cookie', 'the_value');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM