A _php instance for preventing XSS cross-station attacks in Laravel5

Source: Internet
Author: User
Tags aliases pack smarty template

The examples in this article describe the ways to prevent XSS cross-site attacks in Laravel5. Share to everyone for your reference, specific as follows:

Laravel 5 itself does not have the capability to prevent XSS cross-site attacks, but it can use Purifier expansion pack Integration Htmlpurifier prevent XSS cross-site attacks.

1, installation

Htmlpurifier is a rich text HTML filter based on PHP that we can use to prevent XSS cross-site attacks, and for more information on Htmlpurifier, please refer to its official website: http://htmlpurifier.org/. Purifier is an expansion pack that integrates htmlpurifier in Laravel 5, and we can install this expansion pack through Composer:

Composer require Mews/purifier

After the installation is complete, register the Htmlpurifier service provider in the providers of the profile config/app.php:

' Providers ' => [/
 /...
 ] Mews\purifier\purifierserviceprovider::class,
]
then registers the purifier façade in the aliases:
' aliases ' => [/
 /...
 ' purifier ' => mews\purifier\facades\purifier::class,
]

2, configuration

To use a custom configuration, publish the configuration file to the Config directory:

PHP Artisan Vendor:publish

This will generate a purifier.php file in the Config directory:

return [
 ' encoding ' => ' UTF-8 ',
 ' Finalize ' => true,
 ' preload ' => false,
 ' CachePath ' => null ,
 ' Settings ' => ['
  default ' => [
   ' HTML]. Doctype '    => ' XHTML 1.0 Strict ',
   ' HTML. Allowed '    => ' div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src] ',
   ' CSS. Allowedproperties ' => ' Font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color, Background-color,text-align ',
   ' autoformat.autoparagraph ' => true,
   ' Autoformat.removeempty ' => true
  ],
  ' test ' => [
   ' Attr.enableid ' => true
  ],
  ' YouTube ' => [
   HTML. Safeiframe "=> ' true ',
   " URI. Safeiframeregexp "=>"%^ (http://|https://|//) (www.youtube.com/embed/|player.vimeo.com/video/)% ",
  ],
 ],
];

3, using the example

You can use the Accessibility function clean:

Clean (input::get (' InputName '));

Or use the Clean method provided by the purifier façade:

Purifier::clean (Input::get (' InputName '));

You can also dynamically configure in your application:

Clean (' It is my H1 title ', ' titles ');
Clean (' It is my H1 title ', Array (' Attr.enableid ' => true));

Or you can use the purifier façade to provide the method:

Purifier::clean (' This are my H1 title ', ' titles ');
Purifier::clean (' This are my H1 title ', Array (' Attr.enableid ' => true));

PHP prevents XSS attacks

 

More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "

I hope this article will help you with the PHP program design based on Laravel framework.

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.