Form verification for PHP forms

Source: Internet
Author: User
PHP form verification 1. form Security 1. htmlspecialchars () function

Converts special characters to HTML objects. This means that HTML characters such as <and> are replaced with <and>. This prevents attackers from exploiting the code by injecting HTML or JavaScript code (cross-site scripting) into the form.

Cross-Site Scripting (XSS): it is not the abbreviation of Cascading Style Sheet (CSS), so Cross-Site Scripting (XSS) attacks are abbreviated. A malicious attacker inserts malicious html code into a Web page. when a user browses this page, the html code embedded in the Web is executed, so as to achieve the special purpose of malicious attacks.

HTML entity: Reserved characters in HTML must be replaced with character entities. To correctly display reserved characters, we must use character entities in the HTML source code ).

Show result description object name entity number
Space    
< Yu No. < <
> Yu No. > >
& And number & &
" Quotation marks " "
' Marker '(Not supported by IE) '
Bytes Minute Bytes Bytes
£. Pound £. £.
¥ Yen ¥ ¥
? Euro € €
§ Section § §
© Copyright © ©
® Registered trademark ® ®
? Trademark ™ ™
× Multiplication number × ×
& Pide; Division number & Pide; Bytes

A simple addition tool.Htmlspecialchars)

 
2. create a form verification function
Form test
 
 Name: ". $ name; echo"
Email: ". $ email; echo"
Website: ". $ website; echo"
Comment: ". $ comment;?>
2. the form is required Field verification rules
Name Required. Must contain letters and spaces.
Email Required. A valid email address (including @ and.) must be included .).
Website Optional. If this parameter is optional, a valid URL must be included.
Comment Optional. Multi-line input field (text box ).
  Form required    
   
 Name: ". $ name; echo"
Email: ". $ email; echo"
Website: ". $ website; echo"
Comment: ". $ comment;?>
III. format matching

ExploitationRegular expression(Regular Expression) verifies the format of user input data. For more information about regular expressions, see the regular expression 30-minute Getting Started Tutorial and regular expression symbol interpretation.

IntPreg_match(String $ pattern, string $ subject );
Search for a match between the subject and the regular expression specified by pattern.

Regex quick reference

Symbolic meaning
[Abc] A single character: a, B or c
[^ Abc] Any single character but a, B, or c
[A-z] Any single character in the range a-z
[A-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\ Start of string
\ Z End of string
. Any single character
\ S Any whitespace character
\ S Any non-whitespace character
\ D Any digit
\ D Any non-digit
\ W Any word character (letter, number, underscore)
\ W Any non-word character
\ B Any word boundary character
(...) Capture everything enclosed
( B)
A? Zero or one of
A * Zero or more of
A + One or more of
A {3} Exactly 3 of
A {3 ,} 3 or more of
A {3, 6} Between 3 and 6 of
1. name matching

"/^ [A-zA-Z] * $ /"
Only spaces and letters are allowed. "^" indicates the beginning, "$" indicates the end, and [a-zA-Z] indicates a-z or a character in a A-Z or space.

$ Name = test_input ($ _ POST ["name"]); if (! Preg_match ("/^ [a-zA-Z] * $/", $ name) {$ nameErr = "only letters and spaces are allowed! ";}
2. matching email

"/([\ W-] + \ @ [\ w-] +. [\ w-] + )/"
"\ W" matches any word characters that contain underscores. Equivalent to '[A-Za-z0-9 _]';
+ Match the previous subexpression once or multiple times;
"-" Match "-".

3. URL matching

"/\ B (? :(? : Https? | Ftp): \ // | www.) [-a-z0-9 + @ # \/%? = ~ _ |! :,.;] * [-A-z0-9 + @ # \/% = ~ _ |]/I"

4. keep the values in the form

Principle: embed a PHP script into the input tag.
If type = "text", the embedded value =" "
If type = "radio", embed

Finally, a simple logon form is written:

A simple logon form
 
 "; Echo" email: ". $ email; echo"
"; Echo" password: ". $ passwd;?>

Copyright Disclaimer: This article is an original Lshare article. if you need to reprint it, please contact me. if you have any questions, please comments or private messages.

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.