简单的PHP网址导航程序
下面是一个简单的示例,实现一个基于单文件无数据库的 PHP 网址导航程序:
1、创建一个data.json文件,用于存储网址数据。文件内容如下:
阿里云限量代金券 | 此广告位出租25元/月 |

{
"urls": [
{
"name": "Google",
"url": "https://www.google.com"
},
{
"name": "Baidu",
"url": "https://www.baidu.com"
},
{
"name": "GitHub",
"url": "https://github.com"
}
]
}
2、创建一个index.php文件,用于显示网址列表。文件内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网址导航</title>
</head>
<body>
<h1>网址导航</h1>
<ul>
<?php
$data = json_decode(file_get_contents('data.json'), true);
foreach ($data['urls'] as $url) {
echo '<li><a href="' . $url['url'] . '" target="_blank">' . $url['name'] . '</a></li>';
}
?>
</ul>
</body>
</html>
3、创建一个admin.php文件,用于管理网址列表。文件内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网址导航 - 管理</title>
</head>
<body>
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents('data.json'), true);
$data['urls'][] = array(
'name' => $_POST['name'],
'url' => $_POST['url']
);
file_put_contents('data.json', json_encode($data));
header('Location: admin.php');
exit();
}
?>
<h1>网址导航 - 管理</h1>
<form method="post">
<label for="name">名称:</label>
<input type="text" name="name" required>
<br>
<label for="url">网址:</label>
<input type="url" name="url" required>
<br>
<button type="submit">添加</button>
</form>
<ul>
<?php
$data = json_decode(file_get_contents('data.json'), true);
foreach ($data['urls'] as $key => $url) {
echo '<li>';
echo '<a href="' . $url['url'] . '" target="_blank">' . $url['name'] . '</a>';
echo ' <a href="delete.php?key=' . $key . '">删除</a>';
echo '</li>';
}
?>
</ul>
<a href="logout.php">退出</a>
</body>
</html>
4、创建一个 login.php 文件,用于管理员登录。文件内容如下:

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_POST['username'] === 'admin' && $_POST['password'] === '123456') {
$_SESSION['username'] = $_POST['username'];
header('Location: admin.php');
exit();
} else {
echo '用户名或密码错误';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网址导航 - 登录</title>
</head>
<body>
<h1>网址导航 - 登录</h1>
<form method="post">
<label for="username">用户名:</label>
<input type="text" name="username" required>
<br>
<label for="password">密码:</label>
<input type="password" name="password" required>
<br>
<button type="submit">登录</button>
</form>
</body>
</html>
5、创建一个 delete.php 文件,用于删除网址。文件内容如下:

<?php
$key = $_GET['key'];
$data = json_decode(file_get_contents('data.json'), true);
unset($data['urls'][$key]);
file_put_contents('data.json', json_encode($data));
header('Location: admin.php');
exit();
?>
6、创建一个 logout.php 文件,用于管理员退出。文件内容如下:

<?php
session_start();
session_destroy();
header('Location: login.php');
exit();
?>
以上就是一个简单的基于单文件无数据库的 PHP 网址导航程序的实现。您可以将这些文件放在一个目录下,通过浏览器访问 index.php 文件来查看网址列表,通过访问 admin.php 文件来管理网址列表。管理员用户名为 admin,密码为 123456。
本程序在php7.2下测试运行正常无报错
效果图: