Results 1 to 8 of 8

Thread: Allowing others to post comments via PHP

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Question Allowing others to post comments via PHP

    I would like to add posting of comments a feature of my website. The problem is, I am worried I won't secure it correctly since I'm still new to PHP.

    I tried a similiar system hosted on a different database to see how it would work.

    This is the function calling:
    PHP Code:
    add_comment(basic_formatter($_POST['id']), basic_formatter($_POST['name']), $_SERVER['REMOTE_ADDR'], basic_formatter($_POST['message'])); 
    I use a function that formats the input:
    PHP Code:
    function basic_formatter($message)
    {
        
    $message str_replace(">"">"$message);
        
    $message str_replace("<""&lt;"$message);
        
    $message str_replace("\"""&quot;"$message);
        return 
    nl2br($message);

    and here is the part where we add our stuff to the database:
    PHP Code:
    function add_comment($post_id$name$ip$message)
    {
        
    connect();
        
    $result mysql_query("INSERT INTO comments (post_id, name, ip, message) VALUES('$post_id', '$name', '$ip', '$message')");
        if (!
    $result) {
            die(
    'Invalid query: ' mysql_error());}
        
    header("Location: index.php?id=".$post_id);

    What do you think? Am I careful enough or am I lacking in security?

    EDIT: I probably should have put this into the PHP forum. Sorry. Could someone please move it?
    Last edited by Kasracer; Nov 10th, 2005 at 06:04 PM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Allowing others to post comments via PHP

    There are a couple of steps which are un-necessary in the basic_formatter() function. These two lines can be replaced with the htmlspecialchars() function.

    You should also us the mysql_escape_string() function to escape an meta characters, making your text safe for SQL queries. You only need to d this however, if mgaic quotes is on:
    PHP Code:
    function basic_formatter($message)
    {
        
    $message htmlspecialchars($message)
        
        if (! 
    get_magic_quotes_gpc()) {
            
    $message mysql_escape_string($message);
        }

        return 
    nl2br($message);

    It looks secure enough to me , however, once you are done writing oyour script it would be wise to remove the mysql_error() call from your die() message, as error message can be used to compromise the security of your script.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Allowing others to post comments via PHP

    Thanks a lot!

  4. #4

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Allowing others to post comments via PHP

    Thanks again. I got my solution working correctly: www.binaryidiot.com


  5. #5
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    In Front of my computer...
    Posts
    367

    Re: Allowing others to post comments via PHP

    put spam protection if not people will have fun with your site comments. ^^
    Born to help others
    (If I've been helpful then please rate my post. Thanks)

    call me EJ or be slapped!

  6. #6

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Allowing others to post comments via PHP

    Quote Originally Posted by EJ12N
    put spam protection if not people will have fun with your site comments. ^^
    I need to add this.

    I am limiting the amount of characters someone can input, however; I'm not sure whatelse to do. How can I make sure they're on the apge for, like 20 seconds before posting? Or do you have other recommendations?

  7. #7
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    In Front of my computer...
    Posts
    367

    Re: Allowing others to post comments via PHP

    Quote Originally Posted by kasracer
    I need to add this.

    I am limiting the amount of characters someone can input, however; I'm not sure whatelse to do. How can I make sure they're on the apge for, like 20 seconds before posting? Or do you have other recommendations?
    yes either save in a file their IP,TIME or do it by mysql

    then in ur script check if that IP has posted any msges in last X minutes u want...additionally u can register session variable or put a cookie
    something like $_SESSION['last_comment'] = time();
    then in ur script check for that...I hope you get the idea
    Born to help others
    (If I've been helpful then please rate my post. Thanks)

    call me EJ or be slapped!

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Allowing others to post comments via PHP

    The easiest thing you can do is to cast input like age to the appropriate data type in PHP. you can doo this by using the cast operators like (in) and (bool) and (float). Strings are a little more complicated becuase it depends what context you will be using them in.

    To display the value inside html and not have it parsed, use htmlspecialchars(). If you want to include the value inside adatabase query, you should use one of the functions made avaialable by the db abstraction functiosn you are using, e.g mysql_escape_string() and pg_escape_string (). If you are using the value inside a regular expression you have preg_quote() and it is part of a shell command, use escapeshellcmd().

    Whatever you are using the input for, it is very important that you know exactly what form the data is in before you apply it to the application.
    Last edited by visualAd; Nov 11th, 2005 at 02:59 AM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width