[Cookies] AJAX -> file_get_contents() -> AJAX

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'd like to write a PHP (4 or 5) script that browsers can call to
fetch web pages from a remote server through the file_get_contents()
function. The call from the browser is in AJAX.

The issue is that the remote server uses some cookies to maintain
state, but the browser isn't getting them. I don't if this is due to
AJAX or file_get_contents().

Is there to fetch both the HTTP header and the HTML page and send them
as-is to the browser?

Thank you.

Re: [Cookies] AJAX -> file_get_contents() -> AJAX

Gilles Ganault schreef:
Quoted text here. Click to load it

If the remote site (the one you are leeching) uses cookies to maintain
state, you must make sure you send them too.

I think you have 2 options:
1) Use the third parameter for file_get_contents.
It is a 'context resource'.
Look at example 1 and you'll find an way of adding cookies to the request.

2) Use CURL.

But somehow you must send the cookie along with it to maintain state.

Next, of course, you must decide where you will maintain this state: on
your PHPserver, or send the cookies to the client.
Sounds a little messy to me, but I think it can be done.

About AJAX.
If you serve the content back via AJAX, you'll have a lot to worry about.
My guess is you will place the whole content in a div using innerHTML.
If you do that, you will place the whole document in there, including
the HTML, BODY, etc tags.
That leads to invalid HTML.
Also, all the JavaScript in the response won't get executed if you
simply place it inside a DIV.

An obvious question: Why do you use this indirect way? Why not go
directly to the targetted site?

Erwin Moller

"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

Re: [Cookies] AJAX -> file_get_contents() -> AJAX

On Mon, 08 Dec 2008 15:51:41 +0100, Erwin Moller
Quoted text here. Click to load it

What I'm thinking of doing, is, indeed, to use a DIV that will be used
to display the contents of such and such page that is fetched through
AJAX -> my PHP script -> remote web server, but I'll use regexes to
extract only some parts of the remote web page and insert this into
the DIV, so the data finally updated by AJAX in the DIV will be valid.

Re: [Cookies] AJAX -> file_get_contents() -> AJAX

Gilles Ganault wrote:
Quoted text here. Click to load it

file_get_contents() doesn't support cookies.  You'll need to send the
headers manually or use cURL.

The other question is - why the need for cookies on the other site?  Did
you talk to the webmaster there?  Perhaps there is a way to get the info
you want without the need for using cookies

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: [Cookies] AJAX -> file_get_contents() -> AJAX

Jerry Stuckle wrote:
Quoted text here. Click to load it

I can sense a lil tongue in cheek there Jerry, as if he wants the
webmaster to know hes grabbing his work.  hehe

Re: [Cookies] AJAX -> file_get_contents() -> AJAX

On Mon, 08 Dec 2008 09:56:24 -0500, Jerry Stuckle
Quoted text here. Click to load it

Thanks guys. I'll check out cURL for this.

Quoted text here. Click to load it

This is just a prototype. If I get it working, I'll get in contact
with that site's webmaster and offer the code so that it's included in
his site and users can hit that page directly.

Thank you.

Site Timeline