2017-07-24 103 views
0

我有常用功能的PHP得到包括文件并显示它作为这样清洁_GET变种字符串在PHP中只有字母

index.php?F=contact 
<?php 
$file=$_GET['F']; 
include('the_files/'.$file.'.php'); 
?> 
This will display file contact.php 

页面由于安全的我要过滤的

$file=$_GET['F']; 

与某种代码,因此只有文字没有simbols没有斜杠会找引入

我试着用

<?php 
    $clean_file=mysqli_real_escape_string($clean_file,$_GET['F']); 
    include('the_files/'.$clean.'.php'); 
    ?> 

但似乎这仅仅是清洁的MySQLi ...

任何想法如何做到这一点?

+0

哪里做文件从何而来? –

+0

这些文件来自同一台服务器 – NICALANICA

+0

使用白名单或开关大小写或任何其他文件。只有包含文件,如果你知道它们存在。不要相信比任何试图破坏您的网站的人都更聪明。 –

回答

1

尝试:

$file = preg_replace('/[^a-z_\-]/i', '', $_GET['F']); 

当然,我也只是运行一个测试,并将它们发送到IC3,如果他们试图破解你的页面。

+0

/我在这个preg中取代了什么? – NICALANICA

+0

这是'不敏感'。 – PHPglue

+0

最后一个问题:斜杠/////是否被此代码禁用了? – NICALANICA

0

你说得对,让用户控制执行哪个脚本真的很棘手。我会走得更远,而不仅仅是消毒输入。相反,我会分析完整路径并确保它位于允许的目录中。

// 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; 
0

你可以做到以下几点:

做一个白名单是这样,并检查参数值在白名单与否。

$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'); 
} 
0
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; 
    }