11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3You can make Firefox listen to all requests, even those coming from unsigned scripts, bytyping about:config in the address bar, <strong>and</strong> changing the value ofsigned.applets.codebase_principal_support to true.The following is the code that asks Firefox for permission to access a remote server:// ask for permission to call remote server, for Mozilla-based browserstry{// this generates an error (that we ignore) if the browser is not// Mozillanetscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');}catch(e) {}// ignore errorAny errors in this code are ignored using the try/catch construct because the code isMozilla-specific, <strong>and</strong> it will generate an exception on the other browsers.Using a Proxy Server ScriptIt is quite clear that unless you are building a solution where you can control the environment,such as ensuring that your users use Internet Explorer or Firefox (in which case you would need tosign your scripts or configure the browsers manually to be more permissive), accessing remoteservers from your JavaScript code is not an option.The very good news is that the workaround is simple; instead of having the JavaScript access theremote server directly you can have a <strong>PHP</strong> script on your server that will access the remote serveron behalf of the client. This technique is described in the following figure:Figure 3.12: Using a Proxy <strong>PHP</strong> Script to Access a Remote ServerTo read data from a remote server with <strong>PHP</strong> we will use the file_get_contents function, whosedocumentation can be found at http://www.php.net/manual/en/function.file-getcontents.php.85

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!