提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
本人正在准备期末考试,更新可能会迟一点
提示:以下是本篇文章正文内容,下面案例可供参考
一、表的框架的创建
一个简单的博客网站应该包含系统表(config),文章表(article),分类表(cate),友链表(flink),用户表(users)
二、SQL数据库的配置
1.打开MySQL,Front
2按照表的内容在网站上创建对应的表格
在表格中添加对应的字段:
最后的创建结果:
下面写出每个字段的对应格式:
下来开始在Phpstorm中配置对应的文件,记得在上一节中项目一定要搭建在创建网站的根目录下(如下图)
然后在各个文件中建议对应的网站源文件:
下面就可以开始写对应代码了
三 代码篇
1 首先应该完成数据库的链接:
代码如下(示例):
<?php
$username = "www_yourweb_com";
$password = "yourpassword";
$host = "localhost";
$dbname = "www_yourweb_com";
// 连接到 MySQL 数据库
$conn = mysqli_connect($host, $username, $password, $dbname);
// 检查连接是否成功
if(mysqli_connect_errno()){
die("连接失败:" . mysqli_connect_error());
}
// 设置字符集为 UTF-8
$conn->query("set names utf8");
检查一下是否成功连接:
如果出现这种界面则说明连接成功(记得一定要打开Phpstudy的数据库连接)
2.完成代码之间的连通
代码如下(示例):在两个init.php文件中加入代码
include_once "../configs/config.php";
3 登陆页面的设计:
基于HTML去设计一个简单的登陆界面
<!DOCTYPE html>
<html>
<head>
<title>管理员登录</title>
<style>
.login{
width: 400px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="login">
<form method="post">
<table>
<tr>
<td><label for="username">用户名</label></td>
<td><input type="text" name="username" id="username" /></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录" name="sub" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
4 登陆页面与数据库的链接:
参考代码如下:
<?php
session_start();
include_once "init1.php";
if($_POST['sub']){
$username = filterstr($_POST['username']);
$password = filterstr($_POST['password']);
$result = $conn->query("select * from users where username = '$username' and password = '$password'");
if (!$result) {
die("查询失败:" . mysqli_error($conn));
}
if($result->num_rows>0){
$row = $result->fetch_assoc();
if($row['password']==$password){
echo "登陆成功";
}else{
echo 'error';
}
}else{
echo "登录失败";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>管理员登录</title>
<style>
.login{
width: 400px;
margin: 0px auto;
}
</style>
</head>
<body
<div class="login">
<form method="post">
<table>
<tr>
<td><label for="username">用户名</label></td>
<td><input type="text" name="username" id="username" /></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录" name="sub" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
登陆的验证记得数据库中一定得有对应的用户成员
5 简单的防SQL注入:
经实验网页存在SQL恶意注入的风险,在function.php中添加下列代码进行过滤:
<?php
function filterstr($value){
if(!get_magic_quotes_gpc()){
$value=addslashes(trim($value));
return $value;
}
return trim($value);
}
PS:由于只是为了学习,密码登陆并没有加密处理,感兴趣的朋友可以实验MD5或者自己开发加密去实现
总结
以上就是今天要讲的内容,全部更新完成之后会上传所有源代码,我i们下次再见。