首页 > 解决方案 > 如何使用 SESSION 变量随机化多页表单?

问题描述

所以基本上我在 php 中有一个会话登录/注册系统,并将用户重定向到 start.php 文件,该文件将显示从数组读取的导航。

这是我的场景:

login.html用户使用和成功登录后login.php,将用户重定向到start.php或允许用户转到start.php。在中,从-start.php中随机选择一个 php (带有随机函数)并将其添加为链接(显示示例,以便用户可以导航到所选页面)。sql1.phpsql10.phpstart.php

例如,如果选择 ,则用户在登录sql3.php后将能够单击并访问sql3.phpstart.php

当用户点击 Next Page 时,会从sql1.php-中随机选择一个新页面sql10.php,不包括已经选择的页面。例如,如果用户点击sql3.php,页面sql3.php将显示新信息,例如(sql7.php并有一个新的下一页)和新选择的页面(假设是sql7.php这次,sql3.php不应该被选中)。

每次用户登陆新页面时,允许用户单击上一页以返回上一页或下一页。如果在上述过程中随机选择了所有sql1.php-sql10.php文件,则在最后选择的页面中,不要显示到下一页的链接。

代码如下

session_start();

if( !isset($_SESSION['username']) ) {
    header("Location: login.html");
}
$stack = array(); 
array_push($stack, 'sql1.php');
array_push($stack, 'sql2.php');
array_push($stack, 'sql3.php');
array_push($stack, 'sql4.php');
array_push($stack, 'sql5.php');
array_push($stack, 'sql6.php');
array_push($stack, 'sql7.php');
array_push($stack, 'sql8.php');
array_push($stack, 'sql9.php');
array_push($stack, 'sql10.php');

$orderedstack = array();
$unorderedstack = array();

if(isset($_SESSION['stack'], $_SESSION['orderedstack'], $_SESSION['unorderedstack'])){
    $stack = $_SESSION['stack'];
    $orderedstack = $_SESSION['orderedstack'];
    $unorderedstack = $_SESSION['unorderedstack'];
}

$count = count($unorderedstack);

if(!empty($stack)){
    $i = 0;
    while($i < 10){
        $i ++;
        $mixing = $stack[array_rand($stack)];
        /* array_rand randomly returns the the key of $stack. In this scenario it's mixing the keys of the array */
        array_push($unorderedstack, $mixing);
        $coreinfo = array_search($mixing,$stack);
        if($coreinfo!==false){
            unset($stack[$coreinfo]);
        }
        /* Using array_search, once the file is located, the keys of the found file is used for the buttons ($currentpage, $lastpage and $thenextpage)*/
    }
}

if(isset($_GET['next'])){
    if(count($orderedstack) < 10){
        $thenextpage = array_pop($unorderedstack);
        array_push($orderedstack, $thenextpage);
        $coreinfo = array_search($thenextpage,$orderedstack);
        if($coreinfo!==false && count($orderedstack) > 1){
            $currentfile = $orderedstack[$coreinfo-1];
        }
        if($coreinfo!==false && count($orderedstack) > 2){
            $lastpage = $orderedstack[$coreinfo-2];
        }
    }
}    

if(isset($_GET['back'])){
    $prevfile = array_pop($orderedstack);
    array_push($orderedstack, $prevfile);    
    $coreinfo = array_search($prevfile,$orderedstack);
    if($coreinfo!==false  && count($orderedstack) > 2){
        $currentfile = $orderedstack[$coreinfo-2];
    }
    if($coreinfo!==false && count($orderedstack) > 3){
        $lastpage = $orderedstack[$coreinfo-3];
    }
    if($coreinfo!==false && count($orderedstack) > 2){
        $thenextpage = $orderedstack[$coreinfo-1];
    }
    $switch = array_pop($orderedstack);
    array_push($unorderedstack, $prevfile);
}

/*Using echo we created HTML tags and elements to create a form. We added buttons to navigate between the files. The form's method is get, since it's insensitive info, and the button's names is back and next. Using if statements and binary search, we were able to make sure no files were being repeated.*/

?>    
<html>
<head>
<title>Start.php  - Reece, Courtney and Ros</title>    
<style>
.box{
    text-align: center;     
}    
p{
    color: red;
}
h1{
    text-align: center;
}
</style>    
</head>    
<body>    

<?php
if (count($orderedstack) <=1) {
    echo '<h1>start.php</h1>';
    echo '<div class="box">
    <form method="GET">';
        echo '<button name = "next">This button will select a random sql{i}.php file</button>
    </form>
    </div>';
} elseif (count($orderedstack) <=2) {
    echo '<h1><a href=" '. $currentfile . '">' . $currentfile . ' </a></h1>
    <br>';
    echo '<div class="box">
        <form method="GET">
            <button name="back">Go back to start.php starting page</button>';
            echo '<p>' . $currentfile . ' is being displayed</p>';
            echo '<button name = "next">The next sql{i}.php file will be ' . $thenextpage . '</button>
        </form>
    </div>';
} elseif (count($orderedstack) < 10) {
    echo '<h1><a href=" '. $currentfile . '">' . $currentfile . ' </a></h1>
    <br>';
    echo '<div class="box">
    <form method="GET">
        <button name="back">The previous sql{i}.php file was ' . $lastpage . '</button>';
        echo '<p>' . $currentfile . ' is being displayed</p>';
        echo '<button name = "next">The next sql{i}.php file will be ' . $thenextpage . '</button>
    </form>
    </div>';
} elseif (count($orderedstack) == 10) {
    echo '<h1><a href=" '. $currentfile . '">' . $currentfile . ' </a></h1>
    <br>';
    echo '<div class="box">
    <form method="GET">
        <button name="back">You reached the last file, press here to go back</button>';
        echo '<p>' . $currentfile . ' is being displayed</p>
    </form>
    </div>';
}

$_SESSION['stack'] = $stack;
$_SESSION['unorderedstack'] = $unorderedstack;
$_SESSION['orderedstack'] = $orderedstack;
?>

<button type="button"><a href="logout.php" >Logout</a></button>
</body>
</html>

很想知道一种缩短的方法,但仍然使用堆栈,或者有没有其他方法可以在没有堆栈的情况下执行它?

如果可能的话,我想为未来的项目优化网络使用。

标签: phparraysrandompaginationsession-variables

解决方案


为了“优化”,我试图通过将重要组件存储在 SESSION 数组中来减少整个应用程序过程中的总函数调用。

您不需要将sql#.php字符串存储在堆栈数组中 - 只需要整数,因为这是字符串的唯一可变部分。

随着项目的扩展,堆栈数组有一个调整点。sql此外,如果您想放弃range()调用,您甚至可以从目录中提取文件名。

通过使用include将 sql 文件的内容放在页面上,您无需重新配置表单目标。

我已经测试过它可以在我的本地主机上工作......

代码:

<?php
session_start();
/* if(!isset($_SESSION['username']) ) {
    header("Location: login.html");
} */
?>

<html>
<body>
<form method="GET">
<?php
if (!isset($_SESSION['stack'], $_SESSION['id'])) {
    $_SESSION['stack'] = range(1, 10);                     // single point of adjustment as your project evolves/expands
    shuffle($_SESSION['stack']);                           // no subsequent rand() calls; shuffle once and only once
    $_SESSION['id'] = 0;                                   // init the current id
    $_SESSION['last_id'] = sizeof($_SESSION['stack']) - 1; // store the highest index
    echo "Ready to start this thing?";
    echo "<button>Let's Go!</button>";
} else {
    if (isset($_GET['back'])) {
        --$_SESSION['id'];                                 // decrement
    } elseif (isset($_GET['next'])) {
        ++$_SESSION['id'];                                 // increment
    }

    if ($_SESSION['id'] > $_SESSION['last_id']) {
        echo "<h1>Congratulations, you've finished.</h1>";
        session_destroy();                                     // just for my testing
        echo "<button>Play again?</button>";
    } else {
        echo "<div>DISPLAY sql{$_SESSION['stack'][$_SESSION['id']]}.php as page " . ($_SESSION['id'] + 1) . " of " . ($_SESSION['last_id'] + 1) ."</div>";
        //include("sql{$_SESSION['id']}.php");  // I recommend an absolute path to avoid monkeybusiness

        if ($_SESSION['id'] == 0) {
            echo "<button disabled>No Way</button>";
        } else {
            echo "<button name=\"back\">Back</button>";
        }
        if ($_SESSION['id'] == $_SESSION['last_id']) {
            echo "<button name=\"next\">Finish</button>";
        } else {
            echo "<button name=\"next\">Next</button>";
        }
    }
}
?>
</form>
</body>
</html>

推荐阅读