2017-08-02 31 views
-1
<input name="dollar"... 
<input name="cent"... 

<?PHP 

if (!is_numeric($_POST['dollar'])) { return; } 
if (!is_numeric($_POST['cent'])) { return; } 

$total = $_POST['dollar'].".".$_POST['cent']; 

我有一个交易页面,价值包含美元&百分之操纵交易价值的美元和美分

我所做的是支票号码第一和合并在一起美元&美分。

它工作正常,但昨晚我注意到一些交易价值是不正确的

前。

用户输入203.06

,但我的日志显示2030.6

是可能的黑客操纵美元像上面2030.6

+1

为什么用户不能在$ _POST ['dollar']和$ _POST ['cent']中放置2030? – PressingOnAlways

+0

比我的日志会显示2030.06不是2030.6 –

+0

不用担心返回,它只是停止脚本并返回错误信息返回查看 –

回答

0

让我们限值用户可以输入+使用money_format功能,打印结果正确&美分。

<input name="dollar"... 
<input name="cent"... 

<?php 
    //Dollar is an integer above zero 
    $dollar = max(0, intval($_POST['dollar'])); 

    //Cent is an integer between 0 and 99 
    $cent = min(99, max(0, intval($_POST['cent']))); 

    //Version to place into logs 
    $total = $dollar.".".$cent; 

    // Added from another answer to keep everything together 

    //Set the local to parse monetary values correctly 
    setlocale(LC_MONETARY,"en_US"); 

    //Get correct monetary value with 2 decimal place and **$** sign 
    //Version to print 
    $total = money_format("%.2n", $total); 
+0

我现在所做的是100次----> 203.06 * 100 –

0

有一个叫做money_format()的函数可以使用。在这种情况下,解决办法是:

$total = money_format($_POST['dollar'].".".$_POST['cent']); 

甚至更​​好:

$dollar = filter_input(INPUT_POST, "dollar"); 
$cent = filter_input(INPUT_POST, "cent"); 

//Set the local to parse monetary values correctly 
setlocale(LC_MONETARY,"en_US"); 

//Get correct monetary value with 2 decimal place and **$** sign 
$total = money_format("%.2n", $dollar.'.'.$cent); 

希望有所帮助。 祝你好运!