Where am I going wrong with the function below?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • christyPaddy
    New Member
    • Jun 2016
    • 1

    #1

    Where am I going wrong with the function below?

    Code:
    function sanitize_string(string $var)
    {
        $var = strip_tags($var);
        $var = htmlentities($var);
        $var = stripslashes($var);
        $var = trim($var);
        return mysqli_real_escape_string($var);
    }
    Last edited by Dormilich; Jun 18 '16, 10:57 AM. Reason: please use code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Where am I going wrong with the function below?
    nowhere.

    which sanitising function to use strongly depends on the context change, e.g.
    - when using prepared statements, escaping for SQL is unnecessary
    - htmlspecialchar s() is only for printing into HTML/XML and nothing else.
    - strip_tags() is only against XSS
    - a decently recent PHP installation doesn’t add slashes and stripping slashes off a windows directory name is not the best of ideas
    - trimming a string is a bad idea if you need to retain whitespace

    essentially, you always have to decide which sanitisation you need depending on the circumstances.

    Comment

    Working...