I am running a script that sets a $_COOKIE['menu_item_id'] to the last insert id. I am doing this using jquery's $.post.
我正在运行一个脚本,将$ _COOKIE ['menu_item_id']设置为最后一个插入ID。我正在使用jquery的$ .post这样做。
When the post request using $.post, I can't access that cookie in another part of my application. I can only access it in the same php page where post request was made.
当使用$ .post发布请求时,我无法在我的应用程序的另一部分访问该cookie。我只能在发出请求的同一个php页面中访问它。
I tested this out using sessions and that worked well, but I don't to use sessions for such a simple thing.
我使用会话测试了这个并且运行良好,但我不会将会话用于这么简单的事情。
Why can't I access the cookie value through out my application ??
为什么我不能通过我的应用程序访问cookie值?
Below is my code.
以下是我的代码。
index.php
$.post("inserNewItem.php", {menu_cat_id : cat_id }
inserNewItem.php
//Preform the insert statment. Get the last_id and assign it to the cookie value "menu_item_id"
$last_id = $db->insert_id;
setcookie("menu_item_id", $last_id);
index.php
//Check if the menu item id has been set after the $.post ajax has been completed
if(isset($_COOKIE['menu_item_id'])){
echo $_COOKIE['menu_item_id'];
} else {
echo "cookie is not set"; // I keep getting this
}
1 个解决方案
#1
0
Look at manual of setcookie(). You have to pass as 4th parameter path as root /
. Then cookie will be accessible on each part of your site. Example:
看一下setcookie()的手册。您必须以root /作为第4个参数路径传递。然后,您的网站的每个部分都可以访问Cookie。例:
setcookie('YourCookie', $value, time()+3600, '/');
Remember to set also expire time. In example above it expires after one hour.
记得设置过期时间。在上面的示例中,它在一小时后过期。
#1
0
Look at manual of setcookie(). You have to pass as 4th parameter path as root /
. Then cookie will be accessible on each part of your site. Example:
看一下setcookie()的手册。您必须以root /作为第4个参数路径传递。然后,您的网站的每个部分都可以访问Cookie。例:
setcookie('YourCookie', $value, time()+3600, '/');
Remember to set also expire time. In example above it expires after one hour.
记得设置过期时间。在上面的示例中,它在一小时后过期。