Welcome to Gaia! ::

My Design :: Web and Graphic Design [HTML CSS PHP]

Back to Guilds

XHTML, CSS, jQuery, Javascript, PHP, mySQL, MORE! 

Tags: design, graphics, html, jquery, javascript 

Reply Tips/Tutorials
[WEB] PHP validation and security tips

Quick Reply

Enter both words below, separated by a space:

Can't read the text? Click here

Submit

syth_nyklier

PostPosted: Thu Feb 22, 2007 6:29 pm


Just some basic tips.

- Try to validate as much if not all input from users. This is very tedious and time consuming but taking some time to learn regular expressions really helps... Research regular expressions cheat sheet.

When getting information from a url or a form always use the $_ set of variables

$_POST for getting information from forms.
$_GET for getting information from urls

Example of $_POST:


post-exp.php
echo 'My name is: '.$_POST['myname'].'

';
?>

Name:




Example of $_GET:


get-exp.php
echo 'Page: '.$_GET['pg'].'

';
?>
Click Me


A few examples for validation:

email-test.php

if(!ereg("^+?[a-zA-Z0-9](([-+.]|[_]+)?[a-zA-Z0-9]+)*@([a-z0-9]+(.|-))+[a-z]{2,6}$", $_POST['email']))
{
die('
  • Illegal Characters within Email')
    }
    else
    {
    echo 'Valid email';
    }
    ?>

    Email:




  • Here is an example of a very basic regular expression validation:
    (Understand the code that actually post to this I did not give sense code very simular is above.


    if(!ereg("^[a-zA-Z]+[0-9a-zA-Z_-]{4,31}$", $username))
    {
    die('
  • Illegal Characters within Username

    *Requires: 5 to 32 characters (dash & underscore allowed)')
    }
    else
    {
    echo 'Good username';
    }

    if(!ereg("^[0-9a-zA-Z]{5,32}$", $password))
    {
    die('
  • Illegal Characters within Password

    *Requires: 5 to 32 characters or numbers')
    }
    else
    {
    echo 'good password';
    }


  • And, my last and very dirty validation script is below:


    function sqlextraction($var)
    {
    // Replaces ' with ' (its ascii representation)
    $var = str_replace('"','"',$var)

    // Replaces " with " (its ascii representation)
    $var = str_replace(''',''',$var)

    return $var;
    }
    function securestring($var)
    {
    return sqlinjection(str_replace('../','',$var))
    }


    The two above functions will take care of any sql injection problems with mysql and will also take care of any file issues with trying to go to a file under directories... There are other ways to make this script not work but then not everything is 100% smile

    Here are just a very small amount of good validation and security tips.
    I hope others will supply more information and other suggestions on security.
    PostPosted: Thu Feb 22, 2007 9:38 pm


    For protecting against SQL injection it's much easier to use htmlentities(), as this will convert every possible char into it's entity. 3nodding

    Hoggs
    Vice Captain


    syth_nyklier

    PostPosted: Fri Feb 23, 2007 5:17 am


    Ah very true, then just use html_entity_decode to remove. I guess I just hate having to figure out what all the & c o p y ; things are if I have to filter through the database... but doing html_entities is easier smile

    Thank you for your comments. I hope the rest of the tutorial is helpful.
    Reply
    Tips/Tutorials

     
    Manage Your Items
    Other Stuff
    Get GCash
    Offers
    Get Items
    More Items
    Where Everyone Hangs Out
    Other Community Areas
    Virtual Spaces
    Fun Stuff
    Gaia's Games
    Mini-Games
    Play with GCash
    Play with Platinum