我的php代码安全吗?
<?php
$item = (int)$_GET['item'];
if (!isset($_GET['item'])) {
header('Location: index.php');
exit;
}
$fileName = "items/" . $item . ".php";
if (file_exists($fileName)) {
require_once ("items/" . $item . ".php");
} else {
header('Location: index.php');
}
?>
答案 0 :(得分:2)
为了更好的安全性,我认为如果你在item上添加验证应该会更好:
$valid_items = array('item1', 'item2', 'item3');
if(in_array($item, $valid_items)) {
// something if item is valid item
}
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以先检查请求方法的类型,例如
if($_SERVER['REQUEST_METHOD'] != 'GET') {
header('Location: index.php'); exit;
}
if (!isset($_GET['item'])) {
header('Location: index.php');
exit;
}
$item = (int)$_GET['item'];
/*
* just make sure that all you pass is numeric before typecasting it. If you're not
* sure...you can do this
* $item = is_numeric($_GET['item']) ? (int)$_GET['item'] : null; //or 0
*
*/
//your code here