473,503 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP behaving like a user using a browser

Some sites seem to be session driven in the sense that if I visit the
homepage and do a few clicks I can navigate anywhere I want, but if I
paste the current location into a new browser window after having
navigated to some page, it doesn't work. It just returns to the start
page or says "timeout" etc.

This means that I can't read these pages from PHP with

$string = file_get_contents('http://some.url/blah/deep/link');

or whatever.

I guess the way to do then is to make PHP appear as a user-driven
browser as far as the page is concerned. And then start at the start
page and navigate down to the page in question "pressing" buttons and
"choosing" menu items from drop down items etc. from within PHP.

But how do I do this?

/David
Jul 17 '05 #1
5 11179
David Rasmussen wrote:
Some sites seem to be session driven in the sense that if I visit the homepage and do a few clicks I can navigate anywhere I want, but if I paste the current location into a new browser window after having
navigated to some page, it doesn't work. It just returns to the start page or says "timeout" etc.

<snip>

http://groups.google.com/groups?selm...%40comcast.com

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #2

"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
David Rasmussen wrote:
Some sites seem to be session driven in the sense that if I visit the

homepage and do a few clicks I can navigate anywhere I want, but if I

paste the current location into a new browser window after having
navigated to some page, it doesn't work. It just returns to the start

page or says "timeout" etc.

<snip>

http://groups.google.com/groups?selm...%40comcast.com

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


I forgot to mention how to obtain the cookie.

------------------------
Q. My PHP application retrieves a page from a web site that uses cookies.
How do I get the cookie?
A. Use fopen() to open a connection to the server, then call
stream_get_meta_data() to obtain information about the connection. The
function returns an associative array. The 'wrapper_data' element holds an
array containing the HTTP response headers. Loop through it and parse the
string that begins with "Set-Cookie."

----------------------


Jul 17 '05 #3
Q: How do I retrieve a page from a web site?
A: Pass a URL to file() or file_get_contents(). The former returns the
contents as an array of lines. The latter returns the same as string.

Example:

$html = file_get_contents('http://www.example.com/');

Q: How do I retrieve a page from a web site that does browser
detection?
A: Use ini_set() to change the configuration option "user_agent." This
sets the User-Agent header sent by PHP.

Example:

ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1)');
$html = file_get_contents('http://www.example.com/');

Q: How do I retrieve a page from a web site that requires a cookie?
A: Use stream_context_create() to create a HTTP context with Cookie as
one of the headers. Then, if you are coding in PHP 5, pass the context
to file() or file_get_contents() as the third parameter. In PHP 4
either function accepts a context, so you need to open the URL with
fopen() and retrieve the data a chunk at a time with fread().

Example:

$opts = array(
'http'=>array(
'method'=> 'GET',
'header'=>
"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);

$context = stream_context_create($opts);
$f = fopen($url, "rb", false, $context);
while($data = fread($f, 1024)) {
echo $data;

}

stream_context_create() is available in PHP 4.3.0 and above. If you are
using an older version, you would need the cURL functions or use
fsockopen() to open the connection and send the cookie header with
fputs().

Example 1:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Cookie: foo=bar'));
curl_exec($ch);
curl_close($ch);

Example 2:

$fp = fsockopen($host, $port);
fputs($fp, "GET / HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Cookie: foo=bar\r\n\r\n");

while ($data = fgets($fp, 1024)) {
echo $data;
}

Refer:
http://www.php.net/curl
http://curl.haxx.se/libcurl/php/exam...=cookiejar.php

Q. My PHP application retrieves a page from a web site that uses
cookies. How do I get the cookie?
A. Use fopen() to open a connection to the server, then call
stream_get_meta_data() to obtain information about the connection. The
function returns an associative array. The 'wrapper_data' element holds
an array containing the HTTP response headers. Loop through it and
parse the string that begins with "Set-Cookie."

Refer:
http://www.phpclasses.org/httpclient
http://pear.php.net/package/HTTP_Client

++++++
@revision 2 Janwillem Borleffs added other examples
@revision 3 URLs changed to www.example.com. Added reference links. Few
double quotes quickly converted to single quotes
@todo Cleanup. Trim

Jul 17 '05 #4
Great thing, but it misses an issue:
Suppose I have a file, let's call it incl.php, that I include in my
scripts, but I don't want to be accessible directly - when somebody
tries http://my.server/incl.php, though the file is actually there, I
want to display the webserver's 404 Error default message. I don't want
it to be hardcoded, though (like
$err404='<HTML><BODY>...</BODY></HTML>';), in other words I'd like to do
something like:

$err404=file_ge_contents('http://localhost/foobar.htm'); # foobar.htm
does not exist
$err404=str_replace('http://localhost/foobar.htm', __FILE__, $err404);
echp $err404;
die();

But - file_get_contents returns noting and an error:
Warning: file_get_contents(http://localhost/no_such_file.php): failed to
open stream: HTTP request failed! HTTP/1.1 404 Not Found in blahblah.php
on line 666.

what shuld I do to get the err404 page? Besides CURL, there must be
another way. :)

TIA
Mike
Jul 17 '05 #5
Michal Wozniak wrote:
<snip>
what shuld I do to get the err404 page? Besides CURL, there must be
another way. :)


http://www.php.net/fsockopen

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3948
by: Richard White | last post by:
I'm developing a web site, which I test first on my PC (http://localhost/...) and then upload to the web site on the internet when ok. I have a little bit of PHP code (in file download.php) which...
60
7201
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
4
10910
by: Drew Lettington | last post by:
I'm making a simple call to display error messages in a MessageBox from a Windows form and the MessageBox is not behaving in a modal fashion. My modal form displays, the user clicks a button and...
3
1973
by: kajol | last post by:
Hi everyone I am trying to get the content of any webpage (URL) using XMLHTTP, and it is working fine for me, but suddenly I have got a URL "http://www.bizrate.com/" which is causing a system...
5
2143
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
88
12318
by: Mike | last post by:
Is there a way to determine what a user's default email client is? I read a post from 3 years ago that said no. I guess I'm hoping something has come along since then.
7
2899
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
2
1036
by: bingomanatee | last post by:
I have developed what amounts to a fancy shopping cart wizard for a scientific instrument using VB.NET. We are having some disturbing phenomena relating to dropdown controls. On my system and...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.