tags:

views:

123

answers:

2

Hi ,

How does a PHP Proxy work ?

I am looking to make a little script which is similar to other php proxies

But how does it actually work ?

+2  A: 

That's more work than you might think. Simply calling a remote web page and displaying its contents is not enough (that would be readfile('http://google.com') in the simplest case), you have to rewrite the urls in the html document to point to your own proxy again, you need to be able to process https (or you would be allowing normal access to sensitive data, if the target page needs https) and many others (that have partially been compiled in RFC 3143).

Maybe apache's mod_proxy has all you need, but if you really want to write one yourself, studying the source code of other projects (like php-proxy) might give you more insight into the matter.

soulmerge
+2  A: 

I'm thinking of a PHP Proxy, used to go around AJAX Sane Origin Policy. If you need a real HTTP proxy, the process is much more complex.

Simplest pseudocode:

  • get the URL from request (e.g. from $_POST['url'])
  • reject invalid URLs (e.g. don't make requests to localhost (or within your private subnet, if you have several servers))
  • (optional) check your script's cache, return cached response if applicable
  • make request to target URL, e.g. with cURL
  • (optional) cache response, if applicable
  • return response

Note: in this simplest form, you are allowing anyone to access any URL on the Internet through your PHP Proxy; some access control should be implemented (e.g. logged-in users only, depending on what you use the proxy for).

Piskvor