13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

450 Chapter 19 Interacting with the File System <strong>and</strong> the Server<br />

way, you stop users from maliciously (or otherwise) executing comm<strong>and</strong>s on your system.You<br />

can call it like this:<br />

system(escapeshellcmd($comm<strong>and</strong>));<br />

You should also use the escapeshellarg() function to escape any arguments you plan<br />

to pass to your shell comm<strong>and</strong>.<br />

Interacting with the Environment: getenv() <strong>and</strong><br />

putenv()<br />

Before we leave this discussion, let’s look at how to use environment variables from<br />

within <strong>PHP</strong>.Two functions serve this purpose: getenv(), which enables you to retrieve<br />

environment variables, <strong>and</strong> putenv(), which enables you to set environment variables.<br />

Note that the environment we are talking about here is the environment in which <strong>PHP</strong><br />

runs on the server.<br />

You can get a list of all <strong>PHP</strong>’s environment variables by running phpinfo(). Some<br />

are more useful than others; for example,<br />

getenv(“HTTP_REFERER”);<br />

returns the URL of the page from which the user came to the current page.<br />

You can also set environment variables as required with putenv(), as in this example:<br />

$home = “/home/nobody”;<br />

putenv (“ HOME=$home “);<br />

If you are a system administrator <strong>and</strong> would like to limit which environment variables<br />

programmers can set, you can use the safe_mode_allowed_env_vars directive in<br />

php.ini.When <strong>PHP</strong> runs in safe mode, users can set only environment variables whose<br />

prefixes are listed in this directive.<br />

Note<br />

If you would like more information about what some of the environment variables represent, you can look<br />

at the CGI specification at http://hoohoo.ncsa.uiuc.edu/cgi/env.html.<br />

Further Reading<br />

Most of the file system functions in <strong>PHP</strong> map to underlying operating system functions<br />

of the same name.Try reading the man pages for more information if you’re using Unix.<br />

Next<br />

In Chapter 20,“Using Network <strong>and</strong> Protocol Functions,” you learn to use <strong>PHP</strong>’s network<br />

<strong>and</strong> protocol functions to interact with systems other than your own web server.<br />

This again exp<strong>and</strong>s the horizons of what you can do with your scripts.

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

Saved successfully!

Ooh no, something went wrong!