PHP pseudo-static and anti-Injection

Source: Internet
Author: User

PHP pseudo-static, mainly used to hide the passed parameter names. After searching online, sort out the pseudo-static four methods.
<? Php
// Pseudo-static method 1

// Localhost/php100/test. php? Id | 1 @ action | 2
$ Php2Html_FileUrl = $ _ SERVER ["REQUEST_URI"];
Echo $ Php2Html_FileUrl. "<br>"; // php100/test. php? Id | 1 @ action | 2
$ Php2Html_UrlString = str_replace ("? "," ", Str_replace ("/"," ", strrchr ($ Php2Html_FileUrl ,"/"),"? ")));
Echo $ Php2Html_UrlString. "<br>"; // id | 1 @ action | 2
$ Php2Html_UrlQueryStrList = explode ("@", $ Php2Html_UrlString );
Print_r ($ Php2Html_UrlQueryStrList); // Array ([0] => id | 1 [1] => action | 2)
Echo "<br> ";
Foreach ($ Php2Html_UrlQueryStrList as $ Php2Html_UrlQueryStr)
{
$ Php2Html_TmpArray = explode ("|", $ Php2Html_UrlQueryStr );
Print_r ($ Php2Html_TmpArray); // Array ([0] => id [1] => 1); Array ([0] => action [1] => 2)
Echo "<br> ";
$ _ GET [$ Php2Html_TmpArray [0] = $ Php2Html_TmpArray [1];
}
// Echo 'false static: $ _ GET variable <br/> ';
Print_r ($ _ GET); // Array ([id | 1 @ action | 2] => [id] => 1 [action] => 2)
Echo "<br> ";
Echo "Echo $ _ GET [id]. "<br>"; // 1
Echo $ _ GET [action]; // 2
?>

<? Php
// Pseudo-static method 2

// Localhost/php100/test. php/1/2
$ Filename = basename ($ _ SERVER ['script _ name']);
Echo $ _ SERVER ['script _ name']. "<br>"; // php100/test. php
Echo $ filename. "<br>"; // test. php

If (strtolower ($ filename) = 'test. php '){
If (! Empty ($ _ GET [id]) {
$ Id = intval ($ _ GET [id]);
Echo $ id. "<br> ";
$ Action = intval ($ _ GET [action]);
Echo $ action. "<br> ";
} Else {
$ Nav = $ _ SERVER ['request _ URI '];
Echo "1:". $ nav. "<br>"; // php100/test. php/1/2
$ Script = $ _ SERVER ['script _ name'];
Echo "2:". $ script. "<br>"; // php100/test. php
$ Nav = ereg_replace ("^ $ script", "", urldecode ($ nav ));
Echo $ nav. "<br>"; // 1/2
$ Vars = explode ("/", $ nav );
Print_r ($ vars); // Array ([0] => [1] => 1 [2] => 2)
Echo "<br> ";
$ Id = intval ($ vars [1]);
$ Action = intval ($ vars [2]);
}
Echo $ id. '&'. $ action;
}
?>

<? Php
// Pseudo-static method 3


Function mod_rewrite (){
Global $ _ GET;
$ Nav = $ _ SERVER ["REQUEST_URI"];
Echo $ nav. "<br> ";
$ Script_name = $ _ SERVER ["SCRIPT_NAME"];
Echo $ script_name. "<br> ";
$ Nav = substr (ereg_replace ("^ $ script_name", "", urldecode ($ nav), 1 );
Echo $ nav. "<br> ";
$ Nav = preg_replace ("/^. ht (m) {1} (l) {0, 1} $/", "", $ nav); // the end of this sentence is .htmlor .htm.
Echo $ nav. "<br> ";
$ Vars = explode ("/", $ nav );
Print_r ($ vars );
Echo "<br> ";
For ($ I = 0; $ I <Count ($ vars); $ I ++ = 2 ){
$ _ GET ["$ vars [$ I]"] = $ vars [$ I + 1];
}
Return $ _ GET;
}
Mod_rewrite ();
$ Year = $ _ GET ["year"]; // The result is 2006'
Echo $ year. "<br> ";
$ Action = $ _ GET ["action"]; // The result is '_ add'
Echo $ action;
?>

<? Php
// Pseudo-static method 4

// Use the server variable to obtain the PATH_INFO information. In this example, It is/, 8630.html, that is, the part after the execution Script Name.
If (@ $ path_info = $ _ SERVER ["PATH_INFO"]) {
// Regular Expression Matching Parameters
If (preg_match ("// (\ d +), (\ d +), (\ d +) \. html/si", $ path_info, $ arr_path )){
$ Gid = intval ($ arr_path [1]); // get the value 1
$ Sid = intval ($ arr_path [2]); // get the value 100
$ Softid = intval ($ arr_path [3]); // get the value 8630
} Else die ("Path: Error! ");
// Equivalent to soft. php? Gid = 1 & sid = 100 & softid = 8630
} Else die ('path: Nothing! ');
?>

PHP anti-injection, mainly to prevent malicious writing to the background database;
// Anti-injection Function
Function inject_check ($ SQL _str ){
$ Check = eregi ('select | insert | update | delete | \ '| \/\ * | \. \. \/| \. \/| union | into | load_file
| Outfile ', $ SQL _str );
If ($ check ){
Echo "illegal content entered ";
Exit ();
} Else {
Return $ SQL _str;
}
}
// Convert the received Parameters
$ _ GET [type] = inject_check ($ _ GET [type]);
// Use the converted parameters later

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.