我正在创建我的第一个CMS风格的网站,但还有很多方法可以去,但我想知道: 我可以通过php脚本更新php配置文件吗?
例如:
<?php
$forum="1";
$about="1";
$register="0";
?>
<?php
if ($about==("1"))
echo '<a href="about.php">About Us</a>';
if ($forum==("1"))
echo '<a href="forum.php">Forum</a>';
if ($register==("1"))
echo '<a href="register.php">Register</a>;
?>
我想创建一个脚本,当我在网站上以管理员身份登录时,我可以更新第一个集来更改不同页面的值,以便我可以选择显示什么和不显示什么
答案 0 :(得分:1)
答案 1 :(得分:0)
您必须为此使用存储或数据库。将配置存储在可编辑的位置。
以下是使用file:
的示例<?php
$forum = file_get_contents('forum');
$about = file_get_contents('about');
$register = file_get_contents('register');
然后,您可以创建一个管理模块,将这些设置写入相应的文件,如下所示:
<?php
// Admin, overwriting configurations
file_put_contents('forum', 1); // should the admin choses to enable Forum.
file_put_contents('about', 1); // should the admin choses to enable About Us.
file_put_contents('register', 0); // should the admin choses to disable Register.
//... etc for the rest of the modules
答案 2 :(得分:0)
在大多数情况下,我同意@uzyn和@Ibu - 最好了解有关使用数据库的更多信息,然后将其用于配置数据。话虽如此,为了完整起见,我提供以下内容。但请注意,此方法存在安全问题。
我创建了三个文件:配置文件,名为config.cfg;一个用于显示配置的PHP文件,名为show_config.php;另一个名为write_config.php的PHP文件用于保存配置更改,然后调用show_config.php重新显示配置。所有这些都过于简单,并在下面显示。
<强> [的config.cfg] 强>
cfg_data: 42
<强> [show_config.php] 强>
<html>
<head>
<title>Configuration</title>
</head>
<body>
<?php
$cfg_name = "config.cfg";
$cfg_data = file_get_contents($cfg_name);
$lines = explode("\n", $cfg_data);
$cfg_vals = explode(":", $lines[0]);
$cfg_value = trim($cfg_vals[1]);
?>
<form action="write_config.php">
Configuration Value: <?php echo "<input type=\"text\" name=\"config_val\" value=\"$cfg_value \" >" ?> </input>
<br>
<input type="submit">
</form>
</body>
</html>
<强> [write_config.php] 强>
<?php
$cfg_name = "config.cfg";
$new_data = "cfg_data: " . $_REQUEST['config_val'];
if (file_exists($cfg_name)) {
unlink($cfg_name);
}
try {
file_put_contents($cfg_name, $new_data);
} catch (Exception $e) {
echo "Exception caught! " . $e -> getMessage() . "<br> \n";
}
header("Location:show_config.php");
$cfg_name = "config.cfg";
$new_data = "cfg_data: " . $_REQUEST['config_val'];
if (file_exists($cfg_name)) {
unlink($cfg_name);
}
try {
file_put_contents($cfg_name, $new_data);
} catch (Exception $e) {
echo "Exception caught! " . $e -> getMessage() . "<br> \n";
}
header("Location:show_config.php");
?>
请注意,为了使其正常工作,必须将config.cfg上的权限设置为o:rwx(使用> chmod o+wx config.cfg
),这是安全问题之一。此外,此代码不进行输入清理。如果您实际使用此类内容,则应在保存任何内容之前检查并清除所有用户输入。