Filtering input for security

Discussion in 'PHP' started by FarrisFahad, Nov 17, 2022.

  1. #1
    So I am trying to make my website more secure. Is using htmlspecialchars and htmlentities enough? And should I filter output or just input?
     
    FarrisFahad, Nov 17, 2022 IP
  2. adSellerMarketing

    adSellerMarketing Peon

    Messages:
    10
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    3
    #2
    filter_var and filter_input are also used for validation of forms and external variables. https://www.w3schools.com/php/php_filter.aspis https://www.php.net/manual/en/function.filter-input
     
    adSellerMarketing, Nov 17, 2022 IP
    Vooler, FarrisFahad and qwikad.com like this.
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,255
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #3
    Definitely, the input. You don't want any garbage in your DB.

    Search stackoverflow for viable solutions.
     
    qwikad.com, Nov 17, 2022 IP
  4. FarrisFahad

    FarrisFahad Well-Known Member

    Messages:
    487
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    173
    Digital Goods:
    4
    #4
    Thank you guys :)
     
    FarrisFahad, Nov 18, 2022 IP
  5. moh-joh

    moh-joh Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    The good practice is NOT to htmlspecialchars or htmlentities the input data before you store it in database (it should be raw). You just need to use htmlspecialchars function when you output it to the browser.
    Read this related question and answers: https://stackoverflow.com/questions/9299152/do-i-need-to-use-html-entities-when-storing-data-in-the-database
     
    moh-joh, Apr 26, 2023 IP
  6. dascos

    dascos Member

    Messages:
    11
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    33
    #6
    Have a look at xss_clean function of CodeIgniter...
     
    dascos, Apr 27, 2023 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    THIS!

    The bigger question is are you using prepare/execute like a good little doobie so that you have auto-sanitization/escaping of user input to the database? A lot of the batshit crazy hoops people jump through still thinking they're writing PHP 4.5 can be skipped -- and thus most framework BS can also be skipped -- if you just use PDO with prepare/exec.

    Making it so you don't have to sanitize or escape input. But yes, htmlspecialchars your output whenever possible.

    But if like so many you're still slopping variables into query strings like it's 20 years ago, then ... well, you have to go crazy escaping input for zero valid reason other than, well... having one's cranium stuck up PHP 4's rectum.
     
    deathshadow, May 19, 2023 IP
    Vooler likes this.