簡體   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