Security of PHP scripts

Security of PHP scripts: Part II

On page 1 we looked at secure programming techniques in PHP, focusing on user supplied data. Here, we will look at some less obvious security issues.

Cross site scripting

Over the last year or so the security community has focused on cross site scripting (XSS) - mostly due to the significant shift in the software development industry toward Web-based applications. XSS refers to a situation in which one user can take over another user's session by manipulating a Web application in a particular way.

A popular target has been Web-based e-mail systems. When a user logs in to read their e-mail, all URLs include a unique and hard-to-guess string. For example, a link to the user A's inbox might be http://my.mail.hoster/inbox.php?usrid=123&ses=sjdhfoas983jds8. The variable 'ses' is the session string and it is very hard to guess.

However, say that user B sends user A an e-mail containing HTML - something that happens frequently. The HTML might link to an image at Web browsers are designed to identify to the Web servers to which they connect the page from which they were referred. When user A reads the e-mail from user B, the referrer URL will indicate to user B the unique session string identifying user A's session. Using this, user B can take over user A's e-mail account.

User B could also have included JavaScript code to take control of user A's session. Such code could rewrite the inbox page to resemble the login page of the e-mail hoster, with one difference: the form would be submitted to a script hosted on user B's Web server. Thinking that they have been logged out, the user may well resubmit their user name and password - exposing them to user B as a result.

Defensive measures

There are several ways to counter these possible security holes in PHP. All untrusted user-supplied data should be checked for HTML tags. Tags can be escaped with the htmlspecialchars() and htmlentities() functions. They can be removed by using strip_tags.

If your application required users to submit some HTML, but you wanted to disallow tags such as <A>, <IMG> and <SCRIPT>, you would need to write your own script. An example - xss_strip.php. It is a complicated little script, so we will go over it.

The script starts by defining an array of disallowed tags: $badcode. The code then loops to parse the input string, $input. Inside the loop, we attempt to locate a less-than symbol < - the beginning of a tag. If the function strpos() cannot find this character, there is no HTML left in $input so we break from the loop. If it does find the beginning of a tag, we copy all the text up to the < to $output, using substr().

The script then iterates through the HTML tags in $badcode, testing if a match is found. An extract follows:

for($i=0;$i<count($badcode);$i++) {
$tmp = substr($input,$s + 1,strlen($badcode[$i]));
if(strcasecmp($tmp,$badcode[$i]) == 0) {
/* matched */
$found = 1;
/* escaped start */
$output .= "&lt;";
$e = strpos($input,">",$s);
if($e === false) {
/* no closing tag, copy the rest */
$output .= substr($input,$s + 1);
$end = 1;
} else {
$output .= substr($input,$s + 1,$e - $s - 1)."&gt;";
$input = substr($input,$e + 1);

The second line of the extract copies the initial segment of the input string into a temporary variable $tmp. The length of this segment corresponds to the current item from $badcode that we are testing. The function strcasecmp() then tests the current $badcode item against the segment from the input string. If they match, we have found a tag that is disallowed.The script replaces the less-than symbol with an HTML entity representing it: &lt;. It then attempts to locate the closing greater-than tag >. If it cannot be found ($e === false), then the remainder of the string is concatenated to $output and we break from the loop. If we do find a closing > we copy all the characters after the < and before the > and append the HTML entity for the great-than symbol: &gt;.

Developers looking to improve their PHP skills should modify xss_strip.php to escape closing tags, such as '</a>'.

If you are interested in reading more about cross site scripting, visit the page at

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest News Articles


PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?