一,安装宝塔面板(在这里我安装的是Windows版本)
{个人觉得宝塔面板比php好用一点}
二,在宝塔面板的软件商店下载好
Apache 2.4.54
MySQL 5.5.62
phpMyAdmin 5.0
PHP-7.0
三,自行添加数据库
{数据库的名字不能是root,自己想一个就行}
四,在网站根目录中写代码便可
把原先的index.html文件内容删除后,直接写自己的代码就可以了,这里我就不赘述了,因为我也是上网找的
!!!!windows 11 的系统自动占用了80端口,会使得你无法运行网站,按照下图的方式将系统关掉,便可
原因:联想有自带一个winspace.exe,这个东西会占用80端口,把这个服务关掉后,就没有问题了
2,打开“服务和应用程序》服务”,找到winSpace.exe,右键关闭,启动状态改为“手动”(或者关掉)就可以了
我现在的页面是这样的,还有一些问题,但不知道,出在哪里,还在研究中。。。
代码如下(如果知道是什么问题的,可以告诉我,谢谢!)
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<link href=../css/login.css rel="stylesheet"/>
</head>
<body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">
<div class=box>
<div class=title>用户登录</div>
<form action="../login.php" method="post">
<table class=login>
<tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>
<tr><th>密码:</th><td><input type="password" name="password"/></td></tr>
<tr><th>验证码:</th><td><input type="text" name="captcha"/></td></tr>
<tr><th></th><td><img src="../code.php"/></td></tr>
<tr><th></th><td><input type="submit" value="登录"/><a href="register.php"><input type="button" value='前往注册'></a></td>
</tr>
</table>
</form>
</div>
</body>
</html>
login.php
<?php
header('content-type:text/html;charset=utf-8');
//登录界面
require 'login_db_connect.php';//连接数据库
//判断表单是否提交,用户名密码是否提交
if (isset($_POST['username'])&&isset($_POST['password'])){//登录表单已提交
//获取用户输入的验证码
$captcha = isset($_POST['captcha']) ? trim($_POST['captcha']) : '';
//获取Session中的验证码
session_start();
if(empty($_SESSION['captcha'])){ //如果Session中不存在验证码,则退出
exit('验证码已经过期,请返回并刷新页面重试。');
}
//获取验证码并清除Session中的验证码
$true_captcha = $_SESSION['captcha'];
unset($_SESSION['captcha']); //限制验证码只能验证一次,防止重复利用
//忽略字符串的大小写,进行比较
if(strtolower($captcha) !== strtolower($true_captcha)){
exit('您输入的验证码不正确!请返回并刷新页面重试。');
}
//验证码验证通过,继续判断用户名和密码
//获取用户输入的用户名密码
$username=$_POST["username"];
$pwd=$_POST["password"];
$sql="select id,username,password from user where username='$username' and password='$pwd';";
$result=mysqli_query($con, $sql);//执行sql语句
$row=mysqli_num_rows($result);//返回值条目
if (!$row){//若返回条目不存在则证明该账号不存在或者密码输入错误
echo "<script>alert('账号不存在或密码错误,点击前往注册');location='./register.php'</script>";
//exit('账号或密码错误');
}else{//存在返回条目证明用户账号密码匹配,进入主页面
session_start();
$_SESSION['username']=$_POST['username'];
echo "<script>alert('欢迎');location='./index.php'</script>";
}
}
require './view/index.html';
login_db_connect.php
<?php
//用于登录界面数据库连接
//设置字符集
header('Content-type:text/html;charset=utf8');
//连接数据库
$con=mysqli_connect("localhost","aaa","root","aaa");//此处root为登录mysql的用户名,其后依此是密码与数据库名称
if (mysqli_connect_errno($con))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
form
{
width:100%;
height:100%;
margin-top: 100px;
margin-bottom: 100px;
background:#008B8B;
}
div
{
display:inline-block;
padding-top: 255px;
padding-bottom: 255px;
padding-left: 1px;
padding-right: 1px;
}
h2
{
font-family: "微软雅黑";
font-size: 40px;
color:black;
}
#log
{
color:blue;
}
</style>
</head>
<body>
<form>
<center>
<div>
<h3>
注册
</h3>
<p>
用户名:<input type="text"/>
</p>
<p>
密 码:<input type="password"/>
</p>
<p>
手机号:<input type="text"/>
</p>
<p>
<input id=reg type="submit" value="立即注册" />
</p>
<p>
已有账号?<a href="login.html">请登录</a>
</p>
</div>
</center>
</form>
</body>
</html>
register.php
<?php
header('content-type:text/html;charset=utf-8');
//注册页面
require 'login_db_connect.php';//连接数据库
//判断表单是否提交,用户名密码是否提交
if (isset($_POST['username'])&&isset($_POST['pwd'])){//登录表单已提交
//获取用户输入的用户名密码
$user=$_POST["username"];
$pwd=$_POST["pwd"];
//判断提交账号密码是否为空
if ($user=='' || $pwd==''){
exit('账号或密码不能为空');
}else {
$sql="insert into user(username,password) values ('$user','$pwd');";//添加账户sql语句
$select="select username from user where username='$user'";
$result=mysqli_query($con, $select);//执行sql语句
$row=mysqli_num_rows($result);//返回记录数
if(!$row){//记录数不存在则说明该账户没有被注册过
if (mysqli_query($con,$sql)){//查询insert语句是否成功执行,成功将返回 TRUE。如果失败,则返回 FALSE。
//跳转登录页面
echo "<script>alert('注册成功,请登录');location='./login.php'</script>";
}else{//失败则重新跳转注册页面
echo "<script>alert('注册失败,请重新注册');location='./regsiter.php'</script>";
}
}else{//存在记录数则说明注册的用户已存在
echo "<script>alert('该用户已经存在,请直接登录');location='./login.php'</script>";
}
}
}
require './view/register.html';