Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:32:16+00:00 2026-05-22T12:32:16+00:00

Questions: What are the best safe1(), safe2(), safe3(), and safe4() functions to avoid XSS

  • 0

Questions:

What are the best safe1(), safe2(), safe3(), and safe4() functions to avoid XSS for UTF8 encoded pages? Is it also safe in all browsers (specifically IE6)?

<body><?php echo safe1($xss)?></body>

<body id="<?php echo safe2($xss)?>"></body>

<script type="text/javascript">
  var a = "<?php echo safe3($xss)?>";
</script>

<style type="text/css">
  .myclass {width:<?php echo safe4($xss)?>}
</style>

.

Many people say the absolute best that can be done is:

// safe1 & safe2
$s = htmlentities($s, ENT_QUOTES, "UTF-8");

// But how would you compare the above to:
//    https://github.com/shadowhand/purifier
// OR http://kohanaframework.org/3.0/guide/api/Security#xss_clean
// OR is there an even better if not perfect solution?

.

// safe3
$s = mb_convert_encoding($s, "UTF-8", "UTF-8");
$s = htmlentities($s, ENT_QUOTES, "UTF-8");

// How would you compare this to using using mysql_real_escape_string($s)?
// (Yes, I know this is a DB function)
// Some other people also recommend calling json_encode() before passing to htmlentities
// What's the best solution?

.

There are a hell of a lot of posts about PHP and XSS.
Most just say “use HTMLPurifier” or “use htmlspecialchars”, or are wrong.
Others say use OWASP — but it is EXTREMELY slow.
Some of the good posts I came across are listed below:

Do htmlspecialchars and mysql_real_escape_string keep my PHP code safe from injection?

XSS Me Warnings – real XSS issues?

CodeIgniter – why use xss_clean

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:32:17+00:00Added an answer on May 22, 2026 at 12:32 pm

    safe2() is clearly htmlspecialchars()

    In place of safe1() you should really be using HTMLPurifier to sanitize complete blobs of HTML. It strips unwanted attributes, tags and in particular anything javascriptish. Yes, it’s slow, but it covers all the small edge cases (even for older IE versions) which allow for safe HTML user snippet reuse. But check out http://htmlpurifier.org/comparison for alternatives. — If you really only want to display raw user text there (no filtered html), then htmlspecialchars(strip_tags($src)) would actually work fine.

    safe3() screams regular expression. Here you can really only apply a whitelist to whatever you actually want:

    var a = "<?php echo preg_replace('/[^-\w\d .,]/', "", $xss)?>";
    

    You can of course use json_encode here to get a perfectly valid JS syntax and variable. But then you’ve just delayed the exploitability of that string into your JS code, where you then have to babysit it.


    Is it also safe in all browsers (specifically IE6)?

    If you specify the charset explicitly, then IE won’t do its awful content detection magic, so UTF7 exploits can be ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning CakePHP, my first MVC, and I have a few best practice questions.
Secondary questions are How do we best utilize SCM in the build process? How
Three SHA512Managed related questions: Is SHA512Managed considered the best one-way hash available in .NET
I've seen some questions on deciding the best OCR result given output from different
Whats the best way to implement rollover 'buttons' like Stackoverflow has for 'Questions', 'Tags',
There are two questions to this: How can I best spread the word about
A few questions on handing errors- I assume it's best to handle them in
I found a lot of questions asking for the best tool, but none asking
I've tried my best to find out a solution with the many script questions
I've looked into some questions here where the best programming books are listed and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.