我在aws-rds-mysql服务器上有一个数据库。我想在我的网站上创建一个网页,可以给一个登录id和密码,让用户以一种干净的方式访问数据库。
ikfrs5lh1#
以下是连接到数据库并在网页上提供登录功能所需的基本脚本。config.php-全局配置
<?php define('DB_SERVER', 'localhost:3036'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', 'rootpassword'); define('DB_DATABASE', 'database'); $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); ?>
login.php-登录函数
<?php include("config.php"); session_start(); if($_SERVER["REQUEST_METHOD"] == "POST") { // username and password sent from form $myusername = mysqli_real_escape_string($db,$_POST['username']); $mypassword = mysqli_real_escape_string($db,$_POST['password']); $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'"; $result = mysqli_query($db,$sql); $row = mysqli_fetch_array($result,MYSQLI_ASSOC); $active = $row['active']; $count = mysqli_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count == 1) { session_register("myusername"); $_SESSION['login_user'] = $myusername; header("location: welcome.php"); }else { $error = "Your Login Name or Password is invalid"; } } ?> <html>
welcome.php-成功登录后的欢迎页面
<?php include('session.php'); ?> <html"> <head> <title>Welcome </title> </head> <body> <h1>Welcome <?php echo $login_session; ?></h1> <h2><a href = "logout.php">Sign Out</a></h2> </body> </html>
logout.php-成功注销后重定向到登录
<?php session_start(); if(session_destroy()) { header("Location: login.php"); } ?>
session.php-验证会话
<?php include('config.php'); session_start(); $user_check = $_SESSION['login_user']; $ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' "); $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); $login_session = $row['username']; if(!isset($_SESSION['login_user'])){ header("location:login.php"); } ?>
1条答案
按热度按时间ikfrs5lh1#
以下是连接到数据库并在网页上提供登录功能所需的基本脚本。
config.php-全局配置
login.php-登录函数
welcome.php-成功登录后的欢迎页面
logout.php-成功注销后重定向到登录
session.php-验证会话