我需要做些什么改变才能使这段代码更安全

问题描述 投票:0回答:1

我正在为大学做一个任务,这个代码将进行安全性和高效性测试,它是获取矩形区域的基本应用程序

define('MIN_VALUE', '0'); //made constant for security  

define('MAX_VALUE', '10000000'); //made constant for security  


if(is_null($_GET['width']) && is_null($_GET['height']))
{
        echo "width and height must have a value";
}
else
{
    //assiang the value to user input
    define('INT_WD', $_GET['width']);
    define('INT_HE', $_GET['height']);



    if (is_numeric(INT_WD) && is_numeric(INT_HE)) //validate that its the proper value
    {


        if((INT_WD < MIN_VALUE || INT_WD > MAX_VALUE) && (INT_HE < MIN_VALUE || INT_HE > MAX_VALUE))
        {
            echo "width and height must be between " . MIN_VALUE . " and " . MAX_VALUE;
        }

        else
        {

            $area = INT_WD * INT_HE;
            echo htmlentities("The Area of The Rectangle is " . $area ." cm2"); //escape the output 
        }
    } 
    else
    {
            echo("width and height must be an integer or float");
    }
}

你们怎么想?我怎样才能提高它的安全性和程序的效率

php security
1个回答
-1
投票

您必须清理用户输入。

你可以这样更安全: define('INT_WD', strip_tags($_GET['width'])); define('INT_HE', strip_tags($_GET['height']));

这将剥离任何可以通过<script>标签创建XSS攻击的html标签。

define()不是必需的。

© www.soinside.com 2019 - 2024. All rights reserved.