使用PHP创建简单的登录系统

<?php

// 定义错误消息变量
$error = '';

// 检查是否提交表单数据
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  // 获取表单数据
  $username = $_POST['username'];
  $password = $_POST['password'];

  // 验证用户名和密码是否正确
  if ($username === 'admin' && $password === '123456') {
    
    // 登录成功,重定向到欢迎页面
    header('Location: welcome.php');
    exit;
    
  } else {
    
    // 登录失败,显示错误消息
    $error = '用户名或密码不正确';
    
  }
}

?>

<html>
<head>
  <title>PHP登录示例</title>
</head>
<body>
  <h1>PHP登录示例</h1>
  <p><?php echo $error; ?></p>
  <form method="POST">
    <p>
      <label for="username">用户名:</label>
      <input type="text" id="username" name="username" required>
    </p>
    <p>
      <label for="password">密码:</label>
      <input type="password" id="password" name="password" required>
    </p>
    <p>
      <button type="submit">登录</button>
    </p>
  </form>
</body>
</html>