以下是一个简单的PHP实现的多用户在线投票系统的示例代码:
index.php(首页):
<?php
session_start();
if(isset($_SESSION['username'])){
header("Location: home.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>多用户在线投票系统</title>
</head>
<body>
<h1>多用户在线投票系统</h1>
<h2>登录</h2>
<form action="login.php" method="POST">
<label>用户名:</label>
<input type="text" name="username" required><br>
<label>密码:</label>
<input type="password" name="password" required><br>
<input type="submit" value="登录">
</form>
<br>
<h2>注册</h2>
<form action="register.php" method="POST">
<label>用户名:</label>
<input type="text" name="username" required><br>
<label>密码:</label>
<input type="password" name="password" required><br>
<input type="submit" value="注册">
</form>
</body>
</html>
login.php(登录处理):
<?php
session_start();
// 模拟数据库中的用户数据
$users = [
['username' => 'user1', 'password' => 'password1'],
['username' => 'user2', 'password' => 'password2'],
['username' => 'user3', 'password' => 'password3']
];
$username = $_POST['username'];
$password = $_POST['password'];
foreach($users as $user){
if($user['username'] == $username && $user['password'] == $password){
$_SESSION['username'] = $username;
header("Location: home.php");
exit();
}
}
echo "登录失败,请检查用户名和密码。";
?>
register.php(注册处理):
<?php
session_start();
// 模拟数据库中的用户数据
$users = [
['username' => 'user1', 'password' => 'password1'],
['username' => 'user2', 'password' => 'password2'],
['username' => 'user3', 'password' => 'password3']
];
$username = $_POST['username'];
$password = $_POST['password'];
// 检查用户名是否已存在
foreach($users as $user){
if($user['username'] == $username){
echo "用户名已存在,请选择其他用户名。";
exit();
}
}
// 将新用户添加到用户列表
$users[] = ['username' => $username, 'password' => $password];
$_SESSION['username'] = $username;
header("Location: home.php");
?>
home.php(用户主页):
<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>多用户在线投票系统 - 主页</title>
</head>
<body>
<h1>欢迎, <?php echo $_SESSION['username']; ?></h1>
<h2>投票列表</h2>
<ul>
<li>选项1</li>
<li>选项2</li>
<li>选项3</li>
</ul>
<h2>发起投票</h2>
<form action="vote.php" method="POST">
<label>选项:</label>
<input type="text" name="option" required><br>
<input type="submit" value="发起投票">
</form>
<br>
<a href="logout.php">退出登录</a>
</body>
</html>
vote.php(投票处理):
<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location: index.php");
}
$option = $_POST['option'];
// 处理投票逻辑
// ...
header("Location: home.php");
?>
logout.php(退出登录):
<?php
session_start();
session_destroy();
header("Location: index.php");
?>
请注意,这只是一个简单的示例,实际的多用户在线投票系统可能需要更多的功能和复杂的实现。
上一篇:如何处理PHP表单中的特殊字符
下一篇:php arsort() 函数对数组进行逆向排序并保持索引关系。主要用于对那些单元顺序很重要的结合数组进行排序。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站