How to fix XSS vulnerabilities in php

Source: Internet
Author: User
To fix the XSS vulnerability in PHP, we can use three PHP functions. These functions are mainly used to clear the HTML flag, so there is no way to inject code. More functions are htmlspecialchars (), which can convert all and symbols into and ;. Other available functions include htmlentities (), which can use the corresponding character entity "> <LINKh

To fix the XSS vulnerability in PHP, we can use three PHP functions.

These functions are mainly used to clear the HTML flag, so there is no way to inject code. More functions are htmlspecialchars (), which can convert all the "<" and ">" symbols into "<" and "> ;". Other available functions include htmlentities (), which can replace all characters with corresponding character entities ).

PHP Code:

// The code here is mainly used to show the differences between the two functions

$ Input = '';

Echo htmlspecialchars ($ input ).'

';

Echo htmlentities ($ input );

?>

Another example of htmlentities ()

PHP Code:

$ Str = "A 'quote' is bold ";

Echo htmlentities ($ str );

Echo htmlentities ($ str, ENT_QUOTES );

?>

First Display: a' quote' isBold

Second Display: a' quote' isBold

Htmlspecialchars () instance used

PHP Code:

$ New = htmlspecialchars ("Test", ENT_QUOTES );

Echo $ new;

?>

Display: Test

The strip_tags () function replaces. deletes all HTML elements (elements) except those that require special permission, such as:, or

.

Strip_tags () instance used

PHP Code:

$ Text ='

Test paragraph.

Other text ';

Echo strip_tags ($ text );

Echo "\ n ";

// Allow

Echo strip_tags ($ text ,'

');

?>

Now we have at least known these functions. when we find that our site has an XSS vulnerability, we can use the code. I recently discovered an XSS vulnerability in the video section of GoogleBig (a plug-in of the Mybb Forum) on my website, so I was wondering how to use these function write code segments to fix this search vulnerability.

First, I found the problem lies in the search. php file. now let's take a look at some of the code in the query and output query results to study it:

PHP Code:

Function search ($ query, $ page)

{

Global $ db, $ bgcolor2, $ bgcolor4, $ sitename, $ io_db, $ module_url, $ list_page_items, $ hm_index;

$ Option = trim ($ option );

$ Query = trim ($ query );

$ Query = FixQuotes (nl2br (filter_text ($ query )));

$ Db-> escape_string ($ query );

$ Db-> escape_string ($ option );

Alpha_search ($ query );

...

In this case, we use the $ query value as the variable, and then use the htmlentities () function:

PHP Code:

$ Query = FixQuotes (nl2br (filter_text (htmlentities ($ query ))));

If you have any questions about the three functions, you can use the PHP Manual to view them:

Http://it.php.net/htmlentities

Http://it2.php.net/htmlspecialchars

Http://it2.php.net/strip_tags

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.