PHP签到功能通常需要数据库来记录用户签到状态。下面是一个简单的示例代码:,,“`php,
1、创建一个名为config.php
的文件,用于存储数据库连接信息:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn>connect_error) { die("连接失败: " . $conn>connect_error); } ?>
2、创建一个名为register.php
的文件,用于用户注册:
<?php include 'config.php'; if (isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; if ($conn>query($sql) === TRUE) { echo "注册成功"; } else { echo "Error: " . $sql . "<br>" . $conn>error; } } ?> <!DOCTYPE html> <html> <head> <title>用户注册</title> </head> <body> <form method="post" action=""> 用户名: <input type="text" name="username"><br> 密码: <input type="password" name="password"><br> <input type="submit" name="submit" value="注册"> </form> </body> </html>
3、创建一个名为login.php
的文件,用于用户登录:
<?php session_start(); include 'config.php'; if (isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = $conn>query($sql); if ($result>num_rows > 0) { $_SESSION['username'] = $username; header("Location: index.php"); } else { echo "用户名或密码错误"; } } ?> <!DOCTYPE html> <html> <head> <title>用户登录</title> </head> <body> <form method="post" action=""> 用户名: <input type="text" name="username"><br> 密码: <input type="password" name="password"><br> <input type="submit" name="submit" value="登录"> </form> </body> </html>
4、创建一个名为index.php
的文件,用于显示签到页面:
<?php session_start(); include 'config.php'; if (!isset($_SESSION['username'])) { header("Location: login.php"); } if (isset($_POST['signin'])) { $date = date("Ymd"); $username = $_SESSION['username']; $sql = "INSERT INTO signin (username, date) VALUES ('$username', '$date')"; if ($conn>query($sql) === TRUE) { echo "签到成功"; } else { echo "Error: " . $sql . "<br>" . $conn>error; } } ?> <!DOCTYPE html> <html> <head> <title>签到</title> </head> <body> <h1>欢迎 <?php echo $_SESSION['username']; ?></h1> <form method="post" action=""> <input type="submit" name="signin" value="签到"> </form> <a href="logout.php">退出登录</a> <a href="records.php">查看签到记录</a> </body> </html>
5、创建一个名为logout.php
的文件,用于退出登录:
<?php session_start(); session_destroy(); header("Location: login.php"); ?>
6、创建一个名为records.php
的文件,用于查看签到记录:
<?php session_start(); include 'config.php'; if (!isset($_SESSION['username'])) { header("Location: login.php"); } $username = $_SESSION['username']; $sql = "SELECT * FROM signin WHERE username='$username'"; $result = $conn>query($sql); ?> <!DOCTYPE html> <html> <head> <title>签到记录</title> </head> <body> <h1>签到记录</h1> <table border="1"> <tr> <th>用户名</th> <th>日期</th> </tr> <?php while ($row = $result>fetch_assoc()): ?> <tr> <td><?php echo $row['username']; ?></td> <td><?php echo $row['date']; ?></td> </tr> <?php endwhile; ?> </table> <a href="index.php">返回首页</a> </body> </html>
注意:这个示例代码没有包含数据表的创建,你需要在MySQL数据库中创建相应的数据表,可以使用以下SQL语句创建:
CREATE TABLE users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL ); CREATE TABLE signin ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, date DATE NOT NULL );
各位小伙伴们,我刚刚为大家分享了有关php签到 源码的知识,希望对你们有所帮助。如果您还有其他相关问题需要解决,欢迎随时提出哦!
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1093246.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复