最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to read Javascript cookie with PHP - Stack Overflow

programmeradmin0浏览0评论

I'm making a PHP script that would work like an API on site that doesn't provide one. I'm using curl for it and the site is posting cookies with javascript, so I can't read them.

Is there a PHP class or PHP module that would read javascript, store the cookie and let me resubmit it with curl to the mentioned page?

I read somewhere that it is possible to read PHP from Java, so maybe there is a similar way for JS/PHP?

I'm making a PHP script that would work like an API on site that doesn't provide one. I'm using curl for it and the site is posting cookies with javascript, so I can't read them.

Is there a PHP class or PHP module that would read javascript, store the cookie and let me resubmit it with curl to the mentioned page?

I read somewhere that it is possible to read PHP from Java, so maybe there is a similar way for JS/PHP?

Share Improve this question edited Oct 13, 2013 at 9:41 user651390 asked Sep 2, 2009 at 14:39 ChrisChris 3,4454 gold badges31 silver badges34 bronze badges 4
  • Is pecl.php/package/spidermonkey a way to do it? – Chris Commented Sep 2, 2009 at 15:01
  • When you say "the site is posting cookies with JavaScript", what you do mean? – John Parker Commented Sep 7, 2009 at 20:51
  • I mean that site sets cookie with javscript setCookie() function. If it would be set with PHP i could read that from cUrl function. – Chris Commented Sep 8, 2009 at 11:51
  • 1 There's no setCookie in javascript. It is simply "document.cookie". – Christian Commented Aug 20, 2010 at 8:19
Add a ment  | 

3 Answers 3

Reset to default 5

Your best bet is to try and read the cookie from php. Javascript can't talk to php itself, but php can grab a cookie that javascript has created.

http://php/manual/en/features.cookies.php goes on to say:

Any cookies sent to you from the client will automatically be included into a $_COOKIE auto-global array...

Hope this helps!

You need to check the logic of the javascript which writes the cookie and try to replicate it using php. The server will not be able to verify if the cookie has been set by your script or the javascript because the javascript is working on the clientside .

It sounds like your script will be "between" the site and the client? eg. the client calls your API, and you log into the site pretending to be a client, then pass back the results?

If so, then all cookies that the site sets will e back through the headers of the page. You can access these by calling

curl_setopt($handle, CURLOPT_HEADER, true);

before you request the page. When you get the returned page, it will have the HTTP headers on the start of it. You can then search the header for any "set-cookie" lines, and they will contain the details of the cookie that the site is trying to send the client.

发布评论

评论列表(0)

  1. 暂无评论