- PHP教程
- PHP-主页
- PHP - 简介
- PHP-环境设置
- PHP - 语法概述
- PHP - 变量类型
- PHP - 常量
- PHP - 运算符类型
- PHP - 决策
- PHP - 循环类型
- PHP-数组
- PHP-字符串
- PHP - 网络概念
- PHP - 获取和发布
- PHP - 文件包含
- PHP - 文件和 I/O
- PHP - 函数
- PHP-Cookie
- PHP-会话
- PHP - 发送电子邮件
- PHP-文件上传
- PHP - 编码标准
- 高级PHP
- PHP - 预定义变量
- PHP-正则表达式
- PHP - 错误处理
- PHP - 错误调试
- PHP - 日期和时间
- PHP 和 MySQL
- PHP 和 AJAX
- PHP 和 XML
- PHP——面向对象
- PHP - 面向 C 开发人员
- PHP - 适合 PERL 开发人员
- PHP 表单示例
- PHP-表单介绍
- PHP - 验证示例
- PHP - 完整表格
- PHP框架作品
- PHP-框架工程
- PHP - 核心 PHP 与 Frame Works
- PHP 设计模式
- PHP - 设计模式
- PHP 函数参考
- PHP - 内置函数
- PHP 有用资源
- PHP - 问题与解答
- PHP - 有用的资源
- PHP - 讨论
PHP-MySQL 登录
本教程演示如何使用 MySQL 数据库创建登录页面。在进入代码部分之前,您需要特殊权限才能创建或删除 MySQL 数据库。因此,假设您有权访问 root 用户,您可以使用 mysql mysqladmin 二进制文件创建任何数据库。
配置文件
Config.php 文件包含有关 MySQL 数据库配置的信息。
<?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);
?>
登录.php
登录 PHP 拥有有关 php 脚本和 HTML 脚本的信息来进行登录。
<?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>
<head>
<title>Login Page</title>
<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label {
font-weight:bold;
width:100px;
font-size:14px;
}
.box {
border:#666666 solid 1px;
}
</style>
</head>
<body bgcolor = "#FFFFFF">
<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
<div style = "margin:30px">
<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</div>
</div>
</body>
</html>
欢迎.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>
注销页面
注销页面包含有关如何从登录会话注销的信息。
<?php
session_start();
if(session_destroy()) {
header("Location: login.php");
}
?>
会话.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");
die();
}
?>
