PHP 重复输出

PHP repeating output

出于某种原因,我的代码只输出了 'p3' 和 'p4' 两次。我想知道是否有人可以指出为什么它会重复,我们将不胜感激。这是错误的示例(如上所述,这仅在 p3 和 p4 上,p1 和 p2 工作正常)

http://i.imgur.com/glNXwr7.png

这是我的代码:

index.php -

 <?php
 session_start();

 if (!isset($_SESSION['p']))
 {
     $_SESSION['p'] = 'p1';
 }

 include('core.php');
 include('core2.php');
 $story = new Story;
 $action = new Action;
 $part = $_SESSION['p'];
 ?>
 <!DOCTYPE HTML>
 <html>
<head>
    <title>MacBeth Console</title>

    <style>
        body{
        background:black;
        color:#2ecc71;
        font-family: "Courier New", Courier, monospace
        }

        .txt{
            border:0px;
            background-color:black;
            color:#2ecc71;
            font-family: "Courier New", Courier, monospace
        }

        input:focus {outline: none; }
    </style>
</head>

<body>
    <?php

    echo 'DEBUG: P_'.$part.' <br />';

        if ($_SESSION['p'] == 'p1')
            $story->p1();
        else
            $action->continueStory($part, '');

        if (isset($_POST['submit']))
        {
            $action->ContinueStory($part, $_POST['yourAction']);
        }
    ?>
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
        <input type="hidden" name="p" value="<?php echo $part; ?>" style="vertical-align:middle;">
        <img src="cmd.png"><input type="text" id="input" name="yourAction" class="txt">
        <input type="submit" name="submit" value="continue">    
    </form>
</body>
 </html>

core.php -

 <?php
class Story
{
    public function p1()
    {
        print '<b>Witch 1</b>: When shall we all meet again? <br />
                <b>Witch 2</b>: When the hurly-burly is done, and the battle\'s lost and won. <br />
                <b>Witch 3</b>: That will be ere the set of sun. <br />
                <b>Witch 1</b>: Where\'s the place? <br />
                <b>Witch 2</b>: Upon the heath, is this location to thou satisfaction? <br />';
    }

    public function p2($action)
    {
        $action = strtolower($action);
        if ($action == 'yes')
        {
            $_SESSION['p'] = 'p3';
            print '** The meeting shall be held upon the heath to meet with MacBeth. ** -- PRESS ENTER';
        }
        else if ($action == 'no')
        {
            $_SESSION['p'] = 'p3';
            print '** Despite your opinion, the other witches decide the best place to meet would be at the heath. **';
        }
        else
        {
            print 'Unknown action "'.$action.'".';
        }
    }

    public function p3($action)
    {
        echo 'test ';
            $_SESSION['p'] = 'p4';
            /*return '<b><i>Scene II</i></b> <br />
                    <b>Duncan</b>: What bloody man is that? He can report, as seemeth by his plight, of the revolt the newest state. <br /> <br />
                    <b>Sergent</b>: Doubtful it stood; As two spent swimmers, that do cling together, and choke their art. The merciless Macdonwald-- Worthy to be a rebel, for to that
                                    The multiplying villanies of nature. Do swarm upon him--from the western isles of kerns and gallowglasses is supplied; and fortune, on his damned quarrel smiling,
                                    Show\'d like a rebel\'s whore: but all\'s too weak: For brave Macbeth--well he deserves that name-- Disdaining fortune, with his brandish\'d steel,
                                    which smoked with bloody execution like valour\'s minion carved out his passage till he faced the slave; Which ne\'er shook hands, nor bade farewell to him 
                                    till he unseam\'d him from the nave to the chaps and fix\'d his head upon our battlements.  
                    <b>Duncan</b>: O valiant cousin! worthy gentleman! <br /> <br />
                    ** Please press enter ** <br />';*/

    }

    public function p4($action)
    {
        $_SESSION['p'] = 'p1';
        echo 'test 2';
    }
}
 ?>

core2.php -

 <?php
class Action
{
    public function ContinueStory($p, $action)
    {
        $story = new Story;
        if ($p == "p1")
        {
             $story->p2($action);
        }
        else if ($p == "p3")
        {
             $story->p3($action);
        }
        else if ($p == "p4")
        {
            $story->p4($action);
        }
    }
}
?>

您的问题在这里:

<?php

echo 'DEBUG: P_'.$part.' <br />';

    if ($_SESSION['p'] == 'p1')
        $story->p1();
    else
        $action->continueStory($part, '');

    if (isset($_POST['submit']))
    {
        $action->ContinueStory($part, $_POST['yourAction']);
    }
?>

你正在做

$action->continueStory($part, '');

两次。首先在您的第一个 'if else',其次在您的 "if isset $_POST".

这应该是最好的方法:

<?php

echo 'DEBUG: P_'.$part.' <br />';


    if (isset($_POST['submit']))
    {
        $action->ContinueStory($part, $_POST['yourAction']);
    } else {
        if ($_SESSION['p'] == 'p1')
            $story->p1();
        else
            $action->continueStory($part, '');
    }
    ?>

N.B. :为了安全起见,不要在表单操作中使用 $_SERVER['PHP_SELF'] ,#_ 应该更好我觉得!