复杂的服务器端验证

时间:2011-08-30 00:33:44

标签: php xml arrays validation foreach

你好伙伴Stack Overflowites:

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

我需要帮助验证(在服务器端使用PHP)...我希望这可能是我们一起工作的东西(因为我的大脑现在几乎被炸了)...我想在这一点上我只需要一些帮助逻辑...

你可以在这里查看我的表单:(在这里查看HTMLjQuery /源代码,因为它相当长) http://www.thesportinghub.com/lms/make-my-picks

正如您所看到的,随着许多事情的发生,这是非常激烈的...这是我的基本验证请求:

1。)你只能在整个17周内挑选同一支球队...... 2.)在比赛结束后,你不能在相应的一周内挑选一支球队......

围绕此计划的所有信息都在使用以下格式设置的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数据库表中。我需要获取人们提交的所有信息并将其插入我设置的数据库表中。如果它们通过我上面概述的所有验证,它将只进入数据库。

任何和所有帮助将不胜感激。即使你只是给我一些关于如何开始的基本指示。

谢谢, 克里斯

1 个答案:

答案 0 :(得分:1)

<?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(在您的示例中)唯一选择并确保将来所有选项之外,您还需要检查/执行其他操作吗?