![]() Write Secure Scripts with PHP 4.2!Kevin Yank |
For the longest time, one of the biggest selling points of PHP as a server-side scripting language was that values submitted from a form were automatically created as global variables for you. As of PHP 4.1, the makers of PHP recommended an alternate means of accessing submitted data. In PHP 4.2, they switched off the old way of doing things! As I'll explain in this article, these changes have been made in the name of security. Together, we'll explore the new features of PHP for handling form submissions and other data, and how they can be used to write more secure scripts. What's wrong with this picture?
Consider the following PHP script, which grants access to a Web page only if the correct username and password are entered:
Okay, I'm sure about half the readers in the audience just rolled their eyes and said "That's so stupid -- I would never make a mistake like that!" But I guarantee that a good number of you are thinking "Hey, that's not bad. I should write that down!" And of course there's always the rather confused minority ("What's PHP?"). PHP was designed as a "nice and easy" scripting language that beginners can start to use in minutes; it should also protect those beginners from making scary mistakes like the one above. For the record, the problem with the above script is that you can easily gain access to it without supplying the correct username and password. Simply type the address of the page into your browser with So, easy fix, right? Just set |
How does PHP 4.2 change things?
As of PHP 4.2, a fresh PHP installation has the Beginning with PHP 4.1, EGPCS values are now available in a set of special arrays: $_ENV -- Contains system environment variables $_GET -- Contains variables in the query string, including from GET forms$_POST -- Contains variables submitted from POST forms$_COOKIE -- Contains all cookie variables$_SERVER -- Contains server variables, such as HTTP_USER_AGENT$_REQUEST -- Contains everything in $_GET, $_POST, and $_COOKIE$_SESSION -- Contains all registered session variables
Prior to PHP 4.1, developers who worked with First, let's re-write the broken script from the previous section for use under PHP 4.2 (i.e. with /#pc#/<?php // Check the username and password As you can see, all I had to do was add three lines to the top of the script:
Since we're expecting the username and password to be submitted by the user, we grab these values out of the
We also fetch the commonly-used
Other than 'allowing in' these three variables, the script hasn't changed at all. Turning Note for the Nitpickers: The default |
But doesn't that mean more typing?
Yes, in simple scripts like the one above, the new way of doing things in PHP 4.2 does often require more typing. But hey, look on the bright side -- you could start charging by the keystroke! Seriously though, the makers of PHP are not entirely insensitive to your pain (I have a half brother who suffers from repetitive stress injuries). A special feature of these new arrays is that, unlike all other PHP variables, they are totally global. How does this help you? Let's extend our example a little to see. To allow for multiple pages on the site to require a username/password combination to be viewed, we'll move our authorization code into an include file ( /#pc#/<?php /* protectme.php */ function authorize_user($authuser, $authpass) // Check the username and password Now, our protected page looks like this:
Nice and simple, right? Now here's a challenge for the especially eagle-eyed and experienced -- what's missing from the What's missing is the declaration of /#pc#/ function authorize_user($authuser, $authpass) .../#epc#/ In PHP, unlike in other languages with similar syntax, variables outside a function are not automatically available inside the function. You need to specifically bring them in with the With /#pc#/ function authorize_user($authuser, $authpass) $username = $HTTP_POST_VARS['username']; But in PHP 4.1 or later, the special
|
How does this affect sessions?
The introduction of the special Let's consider another authorization example. This time, it will use sessions to mark a user as authorized for the remainder of his or her stay on your site. First, the PHP 4.0 version (with /#pc#/<?php if ($username == 'kevin' and $password == 'secret') Now, spot the security hole. As before, adding Here's how the script looks when we add our special arrays (PHP 4.1) and switch off /#pc#/<?php if ($_POST['username'] == 'kevin' and See? Much more straightforward! Instead of registering a normal variable as a session variable, we set the session variable (in the Summary
In this article I explained the reasoning behind recent changes to the PHP scripting language. In PHP 4.1, a set of special arrays were added to the language to access external data values. These arrays are available in any scope to make external data access a more convenient. In PHP 4.2, |
URL: http://www.sitepoint.com/article/758
|