[英]Change a page for other users by what a admin selects in a form in PHP
I am stuck with a bit of a problem. 我陷入了一个问题。
I have to build an application that shows everyone that visits the webserver a message wich sais if there is maintenance or a failure on the server. 我必须构建一个应用程序,以向访问网络服务器的每个人显示一条消息,表示服务器是否维护或出现故障。
Wich of the 2 messages gets shown to the user depends on what the admin sets it to. 向用户显示的2条消息中的一部分取决于管理员将其设置为什么。 This is done by clicking one of two buttons on the admin page.
通过单击管理页面上的两个按钮之一来完成此操作。
I got it working to the point where if I click one of the two buttons on the admin page it redirects me to the index page and shows the right text. 我得到的结果是,如果我单击管理页面上的两个按钮之一,它将重定向到索引页面并显示正确的文本。
My problem is that the choice I made is a one time thing and will not be saved. 我的问题是我所做的选择是一次性的,不会被保存。 Meaning that if anyone else visits the site he/she gets to see an empty index page.
这意味着如果其他人访问该站点,他/她将看到一个空的索引页面。
I am not allowed to use a database to store the choice i made, so I will have to store the variable somewhere else. 我不允许使用数据库存储所做的选择,因此我将不得不将变量存储在其他位置。
But I have no idea how to save a variable without a database. 但是我不知道如何在没有数据库的情况下保存变量。
My code goes something like this: 我的代码是这样的:
Index.php: Index.php:
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
$page = $_POST['sb'];
// // echo $page;
//
if($page == 'Maintenance')
{
require './pages/index.html';
}
elseif($page == 'Failure')
{
require './pages/fail.html';
}
Admin.php: Admin.php:
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = $page;
require './functions.php';
$page = $_POST['sb'];
change();
Functions.php: Functions.php:
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
echo '<div class="switch">' .
'<form method="POST" action="../index.php">' .
'<input class="button" type="submit" name="sb" value="Maintenance">' .
'<input class="button" type="submit" name="sb" value="Failure">' .
'</form>' .
'</div>';
}
I would recommend using a txt file as a place to store your data and retrieve it in the index file to display the correct page. 我建议使用txt文件作为存储数据的地方,并在索引文件中检索它以显示正确的页面。
Posting "sb" to txt file on server: 将“ sb”发布到服务器上的txt文件:
<?php
if($_POST['sb'] != ""){
$file = fopen('sbstore.txt', 'w');
fwrite($file, $_POST['sb']);
fclose($file);
}
?>
Retrieving data from txt file on server: 从服务器上的txt文件中检索数据:
<?php
if (filesize('sbstore.txt') != 0){
$txt_file = file_get_contents('sbstore.txt');
$sb = $txt_file;
} else {
$sb = "Default";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.