How to send HTTP Headers to an included page in php?
I need to include some URL in php.. I used the following code:
<? include 'http://my_server.com/some_page.jsp?test=true'; ?>
The problem is that, the page some_page.jsp
act differentially based on the request headeruser-agent
that appears n开发者_如何学Cot being sent by the above include statement...
So How can I force the browser to send the request headers too to the included page?
Thanks.
That is a bad pattern to use, if it is on your own server, you should just include it via a relative path (why invoke HTTP) and if it isn't, you are basically handing the keys to your site over to the other domain.
To request another document with a different user agent, try the cURL library.
Do not continue, for informative purposes only...
If you must run the resulting PHP code (and I strongly advise you don't, and most importantly why?) you can then eval()
the response.
Update
You need allow_url_include
on to include a URL, and it is off by default. If you enable it, you can then set the user agent with the user_agent
option.
If you are doing this to join a php
and jsp
site together, you should try and stick to only sending data between them, not code for the other to run over HTTP.
The statement:
<? include 'http://my_server.com/some_page.jsp?test=true'; ?>
is not going to act as you expected.
Check-out: HttpClient
http://scripts.incutio.com/httpclient/
There are user manual and example
You can't set all the HTTP headers that are sent, but you can set the user-agent string that is sent like so:
<?
ini_set("user_agent", $_SERVER["HTTP_USER_AGENT"]);
include 'http://my_server.com/some_page.jsp?test=true';
?>
The user_agent value is NULL by default, which is why you don't currently see anything being sent.
That should do the trick. Note that if the .jsp file returns html content rather than php, you should use readfile
, not include
.
I'd highly advise you not to use your code in a live environment, ever. Instead, use Client-URL.
For example, you can do it like this:
$ch = curl_init();
// the website
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/path/to/webpage');
// this line sets up curl so the website contents will be returned
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// the user agent, for example firefox
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$html = curl_exec($ch);
If you absolutely need to execute the code afterwards, then you can perform a lot of checking and eval()
it, but only if you have to. A better approach would be some kind of dictionary.
精彩评论