这在php中是安全的做法吗?

这在php中是安全的做法吗?,php,Php,我正在尝试做一个在线计算器,人们可以在我的网站上使用表单进行计算,人们可以在表单中发布,结果是通过url字符串得到的。安全吗 html php 您可以使其更加简洁 if (is_numeric($_GET['user_price']) && is_numeric($_GET['selling'])) { echo 'Total '. $_GET['user_price'] * $_GET['selling']; } else { echo 'Something

我正在尝试做一个在线计算器,人们可以在我的网站上使用表单进行计算,人们可以在表单中发布,结果是通过url字符串得到的。安全吗

html


php


您可以使其更加简洁

if (is_numeric($_GET['user_price']) && is_numeric($_GET['selling'])) {
    echo 'Total '. $_GET['user_price'] * $_GET['selling'];
} else {
    echo 'Something went wrong';
}

你可以让它更简洁

if (is_numeric($_GET['user_price']) && is_numeric($_GET['selling'])) {
    echo 'Total '. $_GET['user_price'] * $_GET['selling'];
} else {
    echo 'Something went wrong';
}

以下是我将如何编写代码

$userPrice = isset($_GET['user_price']) ? $_GET['user_price']) : NULL;
$selling = isset($_GET['selling']) ? $_GET['selling'] : NULL;

if (is_numeric($userPrice) AND is_numeric($selling)) {
   echo 'Total '. $userPrice * $selling;
} else {
   echo 'enter number not characters';
} 

请注意,如果回显用户提交的字符串,则要养成一个好习惯,即使用
htmlspecialchars()

将它们包装起来。下面是我将如何编写这些字符串的代码

$userPrice = isset($_GET['user_price']) ? $_GET['user_price']) : NULL;
$selling = isset($_GET['selling']) ? $_GET['selling'] : NULL;

if (is_numeric($userPrice) AND is_numeric($selling)) {
   echo 'Total '. $userPrice * $selling;
} else {
   echo 'enter number not characters';
} 

请注意,如果回显用户提交的字符串,要养成一个好习惯,即使用
htmlspecialchars()

是的,这非常安全,根本没有意义
(int)$\u GET['user\u price']
将值转换为整数,它不表示“如果值是整数”

您正在寻找:

if (isset($_GET['user_price'], $_GET['selling']) &&
    is_numeric($_GET['user_price']) && is_numeric($_GET['selling'])) {
   ...
} else {
   echo 'Please enter numbers';
}

是的,那是非常安全的,只是没有意义
(int)$\u GET['user\u price']
将值转换为整数,它不表示“如果值是整数”

您正在寻找:

if (isset($_GET['user_price'], $_GET['selling']) &&
    is_numeric($_GET['user_price']) && is_numeric($_GET['selling'])) {
   ...
} else {
   echo 'Please enter numbers';
}

非常安全,但是当使用GET和POST时,您应该在处理表单数据之前声明一个变量

非常安全,但是当使用GET和POST时,您应该在处理表单数据之前声明一个变量

不是在那里强制转换,而不是强制转换变量本身吗?@alex嗯,是的,这仍然和
中的
不一样,或者
中的
是\u numeric
+1,因为它把我指向了我自己的眼睛没有看到的地方(并且是正确的:)。不是在那里投射,而不是投射变量本身吗?@alex嗯,是的,这仍然和
中的
或者
是\u numeric
+1,因为它把我指向了我自己的眼睛没有看到的地方(正确:)(字符串)和(数组)强制转换变量,它们不测试它们的类型虽然这些答案都很好,但我不得不建议您使用javascript来执行这些计算,因为它们看起来不那么复杂。(字符串)和(数组)强制转换变量,它们不测试它们的类型虽然这些答案都很好,我不得不建议您使用javascript来执行这些计算,因为它们看起来没有那么复杂。+1为了包含
isset
检查,在我的答案中采用了这一点o) @deceze PHP的核心真的需要类似()的东西,其他东西都有+1为了包括
isset
检查,我在回答中采用了这一点…:o) @deceze PHP的核心真的需要类似()的东西,其他东西都有!