验证来自 POST 值的输入?

你好亲爱的网络朋友,今天我有一个问题,我不知道如何验证 POST 中的 INPUT 数据,如果有人可以帮助我验证这些数据,我将不胜感激,这就是代码:


include("GameEngine/Village.php");


$unarray = array(1 => U1, U2, U3, U4, U5, U6, U11, U12, U13, U14, U15, U16, U21, U22, U23, U24, U25, U26, U31, U32, U33, U34, U35, U36, U41, U42, U43, U44, U45, U46, U0);


if (isset($_GET['id']) && $_GET['id'] == 1) {

    $res_qty = $_POST['resqty'];

    $cost = $res_qty * 30;

    $allres_qty = $res_qty * 100000;

    $database->query("UPDATE " . TB_PREFIX . "users SET `gold` = `gold`- " . $cost . " WHERE id =" . $session->uid);

    $database->query("UPDATE " . TB_PREFIX . "vdata SET `wood` = `wood` + " . $allres_qty . ", `clay` = `clay` + " . $allres_qty . ", `iron` = `iron` + " . $allres_qty . ", `crop` = `crop` + " . $allres_qty . " WHERE wref =" . $village->wid);

    $_SESSION['info'] = $allres_qty . " Wood, Clay, Iron, Crop, added at the cost of " . $cost . " gold.";


    header("Location:dorf1.php");

}

if (isset($_GET['id']) && $_GET['id'] == 2) {

    $troop_type = $_POST['trooptype'];

    $troop_qty = $_POST['troopqty'];

    $cost = $troop_qty * 30;

    $dt = array();


    header("Location:dorf1.php");


    for ($x = 1; $x < 11; $x++) {

        $dt[$x] = 3333;

        if ($x == $troop_type) $dt[$x] = floor($troop_qty * 3333);

    }


    $tribe = $database->getUserField($database->getVillageField($village->wid, "owner"), "tribe", 0);


    if ($tribe == 1) {

        $u = "";

    } elseif ($tribe == 2) {

        $u = "1";

    } elseif ($tribe == 3) {

        $u = "2";

    } elseif ($tribe == 4) {

        $u = "3";

    } else {

        $u = "4";

    }


    $database->modifyUnit(

        $village->wid,

        array($u . "1", $u . "2", $u . "3", $u . "4", $u . "5", $u . "6", $tribe . "0", "hero"),

        array($dt[1], $dt[2], $dt[3], $dt[4], $dt[5], $dt[6], 0),

        array(1, 1, 1, 1, 1, 1)

    );

}

您好,我需要验证的是我给我的用户提供的部队和资源的数量,这是在我的游戏中提供部队的脚本,但我需要验证部队和资源,非常感谢!如果有人能帮助我,我将不胜感激!


慕码人2483693
浏览 78回答 1
1回答

慕容3067478

这是一般数据验证的示例:<?php// define variables and set to empty values$name = $email = $gender = $comment = $website = "";if ($_SERVER["REQUEST_METHOD"] == "POST") {&nbsp; $name = test_input($_POST["name"]);&nbsp; $email = test_input($_POST["email"]);&nbsp; $website = test_input($_POST["website"]);&nbsp; $comment = test_input($_POST["comment"]);&nbsp; $gender = test_input($_POST["gender"]);}function test_input($data) {&nbsp; $data = trim($data);&nbsp; $data = stripslashes($data);&nbsp; $data = htmlspecialchars($data);&nbsp; return $data;}?>资料来源: https: //www.w3schools.com/php/php_form_validation.asp主要思想是只接受与您想要作为有效数据接收的内容相匹配的模式。
打开App,查看更多内容
随时随地看视频慕课网APP