繁体   English   中英

复杂的服务器端验证

[英]Complex Server-Side Validation

你好,Stack Overflowites:

我这里有一个复杂的系统/表单,并且已经完成了整个前端验证(使用jQuery)。

我需要帮助(在PHP的服务器端)进行验证...我希望这可能是我们一起努力的事情(因为现在我的大脑快要炸了)...我想在这一点上我只需要一些帮助逻辑...

您可以在此处查看我的表单:(由于篇幅太长,请在此处查看HTMLjQuery /源代码) http://www.thesportinghub.com/lms/make-my-picks

如您所见,它发生了很多事情,这非常紧张...这是我的基本验证要求:

1.)您只能在整整17周内选择同一支球队一次。2.)您不能在比赛结束后的一周内选择一支球队。

有关此计划的所有信息都在XML文档中,该XML文档使用以下格式设置(这只是BIGGER XML代码的一部分):

<week id="1">
        <matchup id="1" date="08/29/11" time="1:53 PM">
            <away city="New Orleans">Saints</away>
            <home city="Green Bay">Packers</home>
            <finalscore>
                <away>6</away>
                <home>0</home>
            </finalscore>
        </matchup>
        <matchup id="2" date="09/11/11" time="1:00 PM">
            <away city="Atlanta">Falcons</away>
            <home city="Chicago">Bears</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
</week>
    <week id="2">
        <matchup id="1" date="09/18/11" time="1:00 PM">
            <away city="Oakland">Raiders</away>
            <home city="Buffalo">Bills</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
        <matchup id="2" date="09/18/11" time="1:00 PM">
            <away city="Kansas City">Chiefs</away>
            <home city="Detroit">Lions</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
        <matchup id="3" date="09/18/11" time="1:00 PM">
            <away city="Baltimore">Ravens</away>
            <home city="Tennessee">Titans</home>
            <finalscore>
                <away></away>
                <home></home>
            </finalscore>
        </matchup>
</week>

所以,告诉我我的逻辑是否正确...或者在这种情况下您会做什么...

提交表单后,我将需要创建一个多维数组,用于存储提交的数据(在将其存入数据库之前)...该数组将包含所选团队,被选中的那一周以及他们选择的时间被选中。

然后,我将不得不以某种方式加载我的XML(使用SimpleXML和PHP)? 每周确认游戏结束后没有其他选择。

老实说,我的头现在真的断了。 我怀疑你们中有人在追随我吗?

您能否至少将我引向正确的方向? 这是一个包含大量内容的密集脚本。 所有前端验证均已完成。 我只需要将提交的信息与XML进行比较,并确保覆盖了我所有的漏洞。

更新:

到目前为止,这是我的PHP代码,但是它实际上并没有工作...我只是在显示它以帮助描绘我正在使用的内容。

  <?php 
    if( isset($_POST['submit']) ) {
    $schedule = "schedule.xml";

$xml = simplexml_load_file($schedule) or die ("Unable to load XML file!");
    date_default_timezone_set('US/Eastern');
    $time = date("h:i:s", time());
    $week1 = $_POST['Week_1'];
    $week2 = $_POST['Week_2'];
    $week3 = $_POST['Week_3'];
    $week4 = $_POST['Week_4'];
    $week5 = $_POST['Week_5'];
    $week6 = $_POST['Week_6'];
    $week7 = $_POST['Week_7'];
    $week8 = $_POST['Week_8'];
    $week9 = $_POST['Week_9'];
    $week10 = $_POST['Week_10'];
    $week11 = $_POST['Week_11'];
    $week12 = $_POST['Week_12'];
    $week13 = $_POST['Week_13'];
    $week14 = $_POST['Week_14'];
    $week15 = $_POST['Week_15'];
    $week16 = $_POST['Week_16'];
    $week17 = $_POST['Week_17'];

    foreach($xml->week as $week)
    {
    $week_number = $week['id'];

    foreach($week->matchup as $matchup)
    {
    $week_name = "Week_" . $week_number;
    $away_city = $matchup->away['city'];
    $home_city = $matchup->home['city'];
    $away_teamname = $matchup->away;
    $home_teamname = $matchup->home;
    $game_time = $matchup['time'];
    $game_date = $matchup['date'];
    $away_full = "{$away_city} {$away_teamname}";
    $home_full = "{$home_city} {$home_teamname}";
    $home_score = $matchup->finalscore->home;
    $away_score = $matchup->finalscore->away;

    date_default_timezone_set('US/Eastern');
    $game = "{$game_date} {$game_time}";

    ?>
    <div class="savedbox">
    <?php
    if (strtotime($game) <= time()) {
    ?>
    <strong>You cannot pick the <?php echo $away_full ?> or <?php echo $home_full ?>. Those teams have been locked for the respective week.</strong>
    <?php
    }
    }
    }
    ?>
    <?php

    if (count(array_unique($_POST))  ===  count($_POST)) {
    ?>
    <strong>Your picks have been saved!</strong><br/><br/>

    <strong>Week 1 Pick:</strong> <?php echo $week1 ?><br/>
    <strong>Week 2 Pick:</strong> <?php echo $week2 ?><br/>
    <strong>Week 3 Pick:</strong> <?php echo $week3 ?><br/>
    <strong>Week 4 Pick:</strong> <?php echo $week4 ?><br/>
    <strong>Week 5 Pick:</strong> <?php echo $week5 ?><br/>
    <strong>Week 6 Pick:</strong> <?php echo $week6 ?><br/>
    <strong>Week 7 Pick:</strong> <?php echo $week7 ?><br/>
    <strong>Week 8 Pick:</strong> <?php echo $week8 ?><br/>
    <strong>Week 9 Pick:</strong> <?php echo $week9 ?><br/>
    <strong>Week 10 Pick:</strong> <?php echo $week10 ?><br/>
    <strong>Week 11 Pick:</strong> <?php echo $week11 ?><br/>
    <strong>Week 12 Pick:</strong> <?php echo $week12 ?><br/>
    <strong>Week 13 Pick:</strong> <?php echo $week13 ?><br/>
    <strong>Week 14 Pick:</strong> <?php echo $week14 ?><br/>
    <strong>Week 15 Pick:</strong> <?php echo $week15 ?><br/>
    <strong>Week 16 Pick:</strong> <?php echo $week16 ?><br/>
    <strong>Week 17 Pick:</strong> <?php echo $week17 ?><br/>

    <?php
    } else {
    ?>
    <strong>Trying to pull a <em>fast one</em>? We don't think so. You can only pick the same team once. If you need more clarification of the rules, please visit <a href="how-to-play">How To Play</a>.</strong>
    <?php
    }
    ?>
    </div><br/>
    <?php
    }
    ?>

我还要提到,这也将全部绑定到mySQL数据库表中。 我将需要处理人们提交的所有信息,并将其插入我设置的数据库表中。 只有它们通过了我上面概述的所有验证,它才会进入数据库。

任何和所有帮助将不胜感激。 即使您只是给我一些入门的基本指示。

谢谢克里斯

<?php 

if( isset($_POST['submit']) )
{
    $required_weeks = 17;
    $schedule = "schedule.xml";
    $xml = simplexml_load_file($schedule) or die ("Unable to load XML file!");
    date_default_timezone_set('US/Eastern');
    $time = date("h:i:s", time());

    foreach($xml->week as $week)
    {
        foreach($week->matchup as $matchup)
        {
            $away_city = $matchup->away['city'];
            $home_city = $matchup->home['city'];
            $away_teamname = $matchup->away;
            $home_teamname = $matchup->home;
            $game_time = $matchup['time'];
            $game_date = $matchup['date'];
            $away_full = $away_city . ' ' . $away_teamname;
            $home_full = $home_city . ' ' . $home_teamname;
            $home_score = $matchup->finalscore->home;
            $away_score = $matchup->finalscore->away;

            date_default_timezone_set('US/Eastern');
            $game = $game_date . ' ' . $game_time;

?>
<div class="savedbox">
<?php

            if (strtotime($game) <= time())
            {

?>
<strong>You cannot pick the <?php echo $away_full ?> or <?php echo $home_full ?>. Those teams have been locked for the respective week.</strong>
<?php

            }
        }
    }

    //if (count(array_unique($_POST))  ===  count($_POST))
    if (count(array_unique($_POST)) === $required_weeks)
    {

?>
<strong>Your picks have been saved!</strong><br/><br/>
<?php

        for($a = 1; $a <= $required_weeks; $a++)
        {

?>
<strong>Week <?php print $a; ?> Pick:</strong> <?php echo $_POST['Week_' . $a] ?><br/>
<?php

        }
    }
    else
    {

?>
<strong>Trying to pull a <em>fast one</em>? We don't think so. You can only pick the same team once. If you need more clarification of the rules, please visit <a href="how-to-play">How To Play</a>.</strong>
<?php

    }

?>
</div><br/>
<?php

}

?>

我整理了一下您的代码(当您可以只使用$ _POST和更灵活的总周数时,无需使用vars),但是您的代码看起来不错。

除了检查17个(在您的示例中)唯一的选择并确保将来所有选择之外,您还需要检查/做其他事情吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM