Clean\u获取PHP中的var字符串,使其仅包含字母
我有一个常用的函数php get来包含一个文件并将其显示为如下页面Clean\u获取PHP中的var字符串,使其仅包含字母,php,html,mysql,mysqli,Php,Html,Mysql,Mysqli,我有一个常用的函数php get来包含一个文件并将其显示为如下页面 index.php?F=contact <?php $file=$_GET['F']; include('the_files/'.$file.'.php'); ?> This will display file contact.php 使用某种代码,因此只有不带斜杠的文本才会包含在INCLUDE中 我试过了 <?php $clean_file=mysqli_real_escape_string($c
index.php?F=contact
<?php
$file=$_GET['F'];
include('the_files/'.$file.'.php');
?>
This will display file contact.php
使用某种代码,因此只有不带斜杠的文本才会包含在INCLUDE中
我试过了
<?php
$clean_file=mysqli_real_escape_string($clean_file,$_GET['F']);
include('the_files/'.$clean.'.php');
?>
但这似乎只是为了清理MySQLi
你知道怎么做吗?试试:
$file = preg_replace('/[^a-z_\-]/i', '', $_GET['F']);
当然,如果他们试图破解你的页面,我会运行一个测试并将它们发送到IC3。你说得对,让用户控制执行哪个脚本真的很棘手。我要做的不仅仅是对输入进行消毒。相反,我将分析完整路径并确保它在允许的目录内
// path to include files, relative to the document root
const INCLUDE_DIR = '/the_files/';
$file = $_GET['F'];
// resolve the real path (after resolving ../ and ./)
$fileFullPath = realpath(INCLUDE_DIR . $file);
// if file doesn't exist
if($fileFullPath === false ||
// or the file is not in INCLUDE_DIR
str_replace($file,'',$fileFullPath) != $_SERVER['DOCUMENT_ROOT'] . INCLUDE_DIR
):
http_response_code(404); // 404 Not Found error
exit;
endif;
// here we know that the file exists and is in INCLUDE_DIR
include $fileFullPath;
您可以执行以下操作: 这样做一个白名单,检查参数值是否在白名单中
$whitelist = array('aaa', 'bbb', 'ccc');
if(in_array($_GET['page'], $whitelist)){
include($_GET['page'].'.php');
}else{
include('default.php');
}
或者,如果文件存在,检查所有可能的值是否都是文件名
$file = preg_replace('/[^a-z]/', '', $_GET['page']).'.php'; // remove all non-a-z-Characters
if(file_exists($file)){
include($file);
}else{
include('default.php');
}
文件来自哪里?文件来自同一台服务器使用白名单或开关盒或其他任何东西。仅当您知道文件存在时才包括这些文件。不要相信自己比任何试图破坏你的网站的人都聪明。这个preg replace中的/i是什么意思?这是
不敏感的
。最后一个问题:这个代码也会禁用斜杠对不对?很好的解决方案phpglue表达式通过z
和\ucode>替换任何不是a
的字符和-
,因此它也将取代斜杠。
$file = preg_replace('/[^a-z]/', '', $_GET['page']).'.php'; // remove all non-a-z-Characters
if(file_exists($file)){
include($file);
}else{
include('default.php');
}
Use this function
$file = mysql_prep($_GET['f']);
function mysql_prep( $value ) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if( !$magic_quotes_active ) { $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}