By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

PHP script as web client - Sessions and Cookies

P: n/a
Hi!

I've got an unusual problem here. I'm trying to write a PHP script that
behaves like a web client. Why? I want to automatically check specific
URLs for changes.

I'm using file_get_contents(URL) to read the pages and this seems to
work just fine as long as there are no logins, sessions or cookies.

But I'm getting a new PHPSESSID from the site whenever I get a new page
(or refresh the same page) even though I did a login through the URL
(...login.php?user=...&pass=...) and it seemed to work. So the login
does not really work.

So what do I have to do to make the login stick? Use a different way to
read the pages? Work with sessions? Cookies?

Don't forget that the script is not running on a server, it is the
client! I have not found any documentation for this rare case.

I'm curious if any of the PHP pros out there knows an answer to this
problem. If anyone does, an answer would be appreciated. Thanks in
advance.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Anonymous <an*******@nowhere.invalid> wrote:
[php client]
So what do I have to do to make the login stick? Use a different way to
read the pages? Work with sessions? Cookies?


You should use a different way to request URLs. How are you requesting
them now? fopen? There are many intelligent ways to implement scripts
that pretend to be http clients. curl might be available on your
machine. Else there are classes like snoopy or the httpclient class in
PEAR. These will make handling cookies easier...

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
Hi,

Are you passing the PHPSESSID back to the server in your URLs?

Kelvin

"Anonymous" <an*******@nowhere.invalid> wrote in message
news:41***************@nowhere.invalid:
Hi!

I've got an unusual problem here. I'm trying to write a PHP script that
behaves like a web client. Why? I want to automatically check specific
URLs for changes.

I'm using file_get_contents(URL) to read the pages and this seems to
work just fine as long as there are no logins, sessions or cookies.

But I'm getting a new PHPSESSID from the site whenever I get a new page
(or refresh the same page) even though I did a login through the URL
(...login.php?user=...&pass=...) and it seemed to work. So the login
does not really work.

So what do I have to do to make the login stick? Use a different way to
read the pages? Work with sessions? Cookies?

Don't forget that the script is not running on a server, it is the
client! I have not found any documentation for this rare case.

I'm curious if any of the PHP pros out there knows an answer to this
problem. If anyone does, an answer would be appreciated. Thanks in
advance.


---
Mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.773 / Virus Database: 520 - Release Date: 05/10/2004
Jul 17 '05 #3

P: n/a
Anonymous wrote:

I've got an unusual problem here. I'm trying to write a PHP script that
behaves like a web client. Why? I want to automatically check specific
URLs for changes.

I'm using file_get_contents(URL) to read the pages and this seems to
work just fine as long as there are no logins, sessions or cookies.

But I'm getting a new PHPSESSID from the site whenever I get a new page
(or refresh the same page) even though I did a login through the URL
(...login.php?user=...&pass=...) and it seemed to work. So the login
does not really work.

So what do I have to do to make the login stick? Use a different way to
read the pages? Work with sessions? Cookies?


I did this a couple times using fsockopen (though I hear CURL is good),
extracted the cookie information, and resent the cookie info with each request.
Read the manual and especially the comments for fsockopen. You may find you
need to send some other, custom headers (notably user agent) for some sites, as
the default settings may be interpreted as spambots.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #4

P: n/a
Daniel Tryba wrote:

Anonymous <an*******@nowhere.invalid> wrote:
[php client]
So what do I have to do to make the login stick? Use a different way to
read the pages? Work with sessions? Cookies?
You should use a different way to request URLs. How are you requesting
them now? fopen? There are many intelligent ways to implement scripts


As I already wrote I'm using file_get_contents(URL).
that pretend to be http clients. curl might be available on your
machine. Else there are classes like snoopy or the httpclient class in
PEAR. These will make handling cookies easier...


What is PEAR?
Jul 17 '05 #5

P: n/a
Kelvin Mackay wrote:
Are you passing the PHPSESSID back to the server in your URLs?


No. I have already thought of that trick but it doesn't work.
Jul 17 '05 #6

P: n/a
Shawn Wilson wrote:
So what do I have to do to make the login stick? Use a different way to
read the pages? Work with sessions? Cookies?


I did this a couple times using fsockopen (though I hear CURL is good),
extracted the cookie information, and resent the cookie info with each request.
Read the manual and especially the comments for fsockopen. You may find you
need to send some other, custom headers (notably user agent) for some sites, as
the default settings may be interpreted as spambots.


Could you please elaborate on that?

What is CURL?

Could you give me some lines of sample code how to request and store a
page in a string variable with fsockopen? Which custom headers do you
think are required and how do I send them?

Thanks.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.