开发者

Storing data in session using jquery and accessing that value in PHP

How can I store data in session or cookie using jquery or JS and access it in php?开发者_运维问答


For cookies:

JavaScript:

document.cookie = 'foo=bar';

After that code has been run, and you open a page on the same domain with the following command in it:

PHP:

echo 'The cookie "foo" had the value ' . $_COOKIE['foo'];

The output will be "The cookie "foo" had the value bar".

For sessions:

You're out of luck, that cannot be done on the client-side, so JavaScript won't help you.


Cookies are client side storage. You can have the server emit headers which will set cookies, or you can set cookies with javascript. Session data is stored on the server. Session data can not be set via the client, although you can call a page on the server and have the server set session data. You'll have to have sessions enabled in order to do this. Cookies don't need to have sessions configured in order to work.


You can run an ajax call to the php page and store your session info and the then another ajax call to a php page that dumps the session info out as json and parse that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜