当前位置:首页 > 其他 > 正文内容

简单的PHP网址导航程序

23vps11个月前 (06-06)其他336

下面是一个简单的示例,实现一个基于单文件无数据库的 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下测试运行正常无报错


效果图:


本网站由提供服务

扫描二维码推送至手机访问。

版权声明:本文由主机测评网发布,如需转载请注明出处。

本文链接:https://23vps.com/post/73.html

标签: 建站
分享给朋友:

“简单的PHP网址导航程序” 的相关文章

小众技术工具库

小众技术工具库

主机哥今天给大家分享一个非常有用的网站,里面非常好用的网站实在是太多了,废话不多说,贴图上链接!网址:https://www.xiaozhongjishu.com/...

MobaXterm配置SSHTunnel

MobaXterm配置SSHTunnel

MobaXterm作为一款良心的SSH工具,它支持SSHTunnel隧道功能,网上的教程讲的并不详细,所以主机哥写个教程:由于主机哥买了台NAT服务器,由于NAT服务器只有SSH端口没有其他公网端口,安装宝塔后要访问8888端口进行面板设置才能用,这时候SSHTunnel就可以用上了点击Tunnel...

syncthing文件同步软件

syncthing文件同步软件

这是一款可以满足办公、文件共享、团队协作、家庭存储等文件同步需求的开源软件,不依赖中心服务器,完全由自己掌握数据,安全可靠。Syncthing是免费且开源的,而且跨平台支持windows、mac、linux、android等主流平台,除了PC、手机以外,在部分路由器、树莓派等硬件上都能轻松运行,听说...

debian/ubuntu/centos7系统安装syncthing文件同步软件教程

debian/ubuntu/centos7系统安装syncthing文件同步软件教程

实验系统:debian10/ubuntu/CentOS7 64位用putty或者其他ssh工具连上服务器,然后用wget命令下载Syncthing的Linux 64位版,版本号对应官网上的最新版,请自行选择:wget https://github.com/syncthing/syncthi...

搭建Syncthing发现和中继服务器和配置Syncthing客户端

搭建Syncthing发现和中继服务器和配置Syncthing客户端

折腾借口Syncthing好是挺好的,但就是同步不给力,公共网络服务其质量参差不齐,网络也说不清。最好的解决方案还是自己搭建发现服务器Syncthing Discovery Server和中继服务器Syncthing Relay Server,有效解决网络连接差,非局域网连接慢的问题。不同于Resi...

魔方财务迁移教程

魔方财务迁移教程

主机哥的魔方财务需要迁移到新的服务器压缩打包所有网站文件备份下载数据库文件在新服务器上安装宝塔添加网站设置伪静态location / { if (!-e $request_filename) { rewrite ^(.*)$ ...