Chris
Chris

Reputation: 3445

How to read Javascript cookie with 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?

Upvotes: 1

Views: 11672

Answers (3)

Michael
Michael

Reputation: 6405

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 .

Upvotes: 1

Nick
Nick

Reputation: 321

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 come 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.

Upvotes: 0

darkAsPitch
darkAsPitch

Reputation: 1875

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.net/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!

Upvotes: 5

Related Questions