【发布时间】:2018-05-09 14:46:36
【问题描述】:
所以基本上我在 php 中有一个会话登录/注册系统,并将用户重定向到 start.php 文件,该文件将显示从数组读取的导航。
这是我的场景:
用户使用login.html和login.php成功登录后,将用户重定向到start.php或允许用户转到start.php。在start.php 中,从sql1.php - sql10.php 中随机选择一个php(具有随机函数)并将其作为链接添加到start.php 中(显示示例,以便用户可以导航到所选页面)。
例如,如果选择了sql3.php,那么用户登录后就可以从start.php点击访问sql3.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>
想知道一种缩短的方法,但仍然使用堆栈,或者有没有其他方法可以在没有堆栈的情况下执行它?
如果可能的话,我希望为未来的项目优化它以供网络使用。
【问题讨论】:
-
如果那是你的数组,为什么不
$stack = ['sql1.php', 'sql2.php', ...];,而不是所有这些推送? -
建议阅读Standard PHP Library (SPL),特别是迭代器。
-
@Michael 你离开我们了吗?
标签: php arrays random pagination session-variables