如何使用SESSION变量随机化多页表单?

时间:2018-05-08 19:37:56

标签: php arrays random pagination session-variables

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

这是我的情景:

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

例如,如果选择sql3.php,则用户可以在登录后点击sql3.php start.php

当用户点击“下一页”时,系统会从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>

很想知道缩短的做法,但仍然使用堆栈,还是有其他方法可以在没有堆叠的情况下执行它?

如果可能的话,我希望将其优化用于未来的项目。

2 个答案:

答案 0 :(得分:0)

start.php 上,您需要:

// build a list of sql.php pages
$_SESSION[ 'sql_pages' ] = explode( ' ', 'sql'.implode( '.php sql', range(1, 10) ).'.php' );

// randomize those pages
shuffle($_SESSION[ 'sql_pages' ]);

$pop = array_pop($_SESSION[ 'sql_pages' ]);
echo '<a href="'.$pop.'">'.$pop.'</a>';

以及您需要的 sql {x} .php 页面:

if(isset($_SESSION[ 'sql_pages' ]) && count($_SESSION[ 'sql_pages' ]) > 0)
{
    $pop = array_pop($_SESSION[ 'sql_pages' ]);
    echo '<a href="'.$pop.'">'.$pop.'</a>';
}
else
{
    echo 'No more pages!';
}

答案 1 :(得分:0)

To&#34;优化&#34;我试图通过将重要组件存储在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>