属于简易版,比较适合没有后台基础的初学者入门,使用Ajax响应php对mysql数据库进行操作,将网站搭建在xampp中,开启Apache服务器利用公网ip进行对网页的访问
登录界面
T-main.html文件
HTML
<div id="T-main-box">
<form id="login" action="" method="post">
<div id="T-main-login">
<div class="T-main-login-inp">
<h2>用户名</h2>
<input type="text" name="username" id="username">
<p id="spanName"></p>
</div>
<div class="T-main-login-inp">
<h2>密码</h2>
<input type="Password" name="userpassword" id="userpassword">
</div>
<p id="hint"></p>
<div class="T-main-login-inp">
input class="T-main-but" type="button" onclick="sign()"value="登 录">
input class="T-main-but" type="button" onclick="register()" value="注 册">
</div>
</div>
</from>
JS
function sign(){
var lg=document.getElementById('login');
var name=document.getElementById('username');
var password=document.getElementById('userpassword');
if(name.value!=""&&password.value!=""){
lg.action='sign.php';
lg.submit();}
else{document.getElementById('hint').innerHTML="用户名或密码不能为空!";}
};
function register(){
window.location="register.html";
}
CSS
html body div span applet object iframe h1 h2 p a{margin:0px;padding:0px;}
body {
background: url(img/sakura.jpg)no-repeat center 0px;
background-attachment: fixed;
background-size: cover;
}
#T-main-box{
width: 100%;
/*color:rgba();*/
}
#T-main-login{
font-family:"微软雅黑" ;
padding: 2em;
width: 65%;
margin: 3.5em auto;
/*height: 100px;*/
background-color: rgba(0,3,6,0.4);
font-family: "arial";
color: #ffc;
}
.T-main-login-inp{
/*width: 100%;*/
/*margin: 2em;*/
margin-bottom:1em;
color: #ffc;
}
#spanName{
color: #f90;
}
#T-main-login input[type='text'],#T-main-login input[type='password']{
font-family: "微软雅黑";
cursor: pointer;
font-size: 1.5em;
color: #fff;
width: 100%;
border:none;
outline: none;
border-bottom: 2px solid #999;
background-color: inherit;
}
.T-main-but{
-webkit-appearance:none;
/*display: block;*/
margin: auto;
margin-top:15px;
border:none;
width: 100%;
height:3.5em;
background-color: rgba(3,3,6,0.6);
color: #ccc;
font-size: 1em;
cursor: pointer;
}
.T-main-but:hover{
background-color: #fff;
color:#000;
}
登陆成功页面
Tmainpage.html文件
HTML
<div id="Tpage">
<div id="top-fiexd">
<span class="item">
<img src="img/logo.png">
</span>
<span class="item"></span>
<span class="item" id="item3">
</span>
</div>
<div id="Tpage-top">
<div id="Mesbox">
<form id="Messageinput" action="message.php" method="POST">
<textarea id="Metext" name="Metext"></textare>
<input id="messinp" type="submit" onclick="message()" value="发表">
</form>
</div>
</div>
<div id="Tpage-main">
<div id="message">
</div>
</div>
dir id="Tpage-foot">
</dir>
</div>
JS
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return "";
}
var usersname=getCookie('usersname');
document.getElementById('item3').innerHTML="welcome,"+usersname;
var xmlHttp
function message()
{
var str=document.getElementById("Metext");
if (str.length==0)
{
// document.getElementById("spanName").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="refresh.php"
url=url+"?uname="+retval
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("POST",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("message").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
CSS
html,body,div,span,applet,object,iframe,h1,h2,p,a{outline: none;margin:0;padding:0;}
body{
}
#Tpage{
width:100%;
height:100%;
/*padding: 20px;*/
/*width: inherit;*/
/*background-color: rgba(200,230,100,0.8);*/
font-family: "微软雅黑";
background: url(img/sakura.jpg) ;
background-size:100%;
background-attachment: fixed;
background-size: cover;
}
#Tpage #logo{
height: 3.5em;
width: 7em;
}
#top-fiexd{
position: fixed;
width: 100%;
height: 60px;
background-color: #633;
opacity: 0.9;
display: -webkit-flex;
}
.item{
height: inherit;;
}
#item3{
text-align: center;
line-height: 60px;
font-size: "微软雅黑";
color: #fff;
}#Tpage-body{
text-align: center;
width: 100%;
/*background-color:#fff; */
}
#Tpage-top{
padding-top:100px;
width: 100%;
display: -webkit-flex;
text-align: center;
/*background-color: #ccc;*/
}
#Tpage-main{
width: 100%;
text-align: center;
/*background-color: #000;*/
padding-bottom:20px;
}
#Mesbox{
width: 80%;
margin: auto;
height: 200px;
flex: 1;
text-align: center;
}
#Metext{
border:none;
box-shadow:2px 2px 15px #933;
-moz-box-shadow:inset 0 0 15 #993;
-webkit-box-shadow:inset 2px 2px 15px #933;
outline: none;
font-family: "微软雅黑";
border-radius: 10px;
padding: 10px;
resize:none;
display: block;
width: 80%;
height: 100px;
margin: auto;
background-color: #ccc;
opacity: 0.9;
font-size: 1.5em;
color: #333;
margin-bottom: 10px;
}
#messinp{
-webkit-appearance:none;
border-radius: 10px;
margin: auto;
border-radius: 10px;
padding: 10px;
width: 80%;
height: 40px;
background-color: #ccc;
opacity: 0.9;
margin: auto;
border:none;
font-size: 1em;
cursor: pointer;
letter-spacing:20px;
font-family: "微软雅黑";
}
#messinp:hover{
background-color: #9cc;
color: #fff;
}
#message{
margin: auto;
width: 80%;
/*background-color: #deb887;*/
opacity: 0.8;
}
#message table{
text-align: center;
width:100%;
/*font-family: Arial, "微软雅黑";*/
}
#message table td{
/*display: block;*/
/*height: 10em;*/
font-size: 1em;
word-break: break-all;
word-wrap: break-word;
}
#message tr:nth-child(odd) td{
border-radius: 10px;
background: #9c6;
color: #036;
}
#message tr:nth-child(even) td{
border-radius: 10px;
background: #ffc;
color: #c66;
}
#message sub{
display: block;
width: inherit;
}
sup{color:#933;}
sign.php文件-处理登录
<?php
header("Content-type:text/html;charset=utf-8");
$uname=$_POST['username'];
$upassword=$_POST['userpassword'];
$con = mysql_connect("localhost:3306","root","sa123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM user
WHERE uname='$uname' and upassword='$upassword'");
if($row = mysql_fetch_array($result))
{
setCookie("usersname","$uname",time()+2*7*24*3600);
//设置cookies
// echo $row['FirstName'] . " " . $row['LastName'];
// echo "
";
echo "";
}else{
echo "登录失败,该用户不存在...请重新登录";
}
?>
register.html文件
<div id="T-main-box">
<form id="login" action="" method="post">
<div id="T-main-login">
<div class="T-main-login-inp">
<h2>请输入用户名</h2>
<input type="text" name="username" id="username" onblur="showHint(this.value)">
<p id="spanName"></p>
</div>
div class="T-main-login-inp">
<h2>请输入密码</h2>
<input type="Password" name="userpassword" id="userpassword">
</div>
<div class="T-main-login-inp">
input class="T-main-but" type="button" onclick="register()" value="注 册">
</div>
</div>
</form>
//Ajax
var xmlHttp
function showHint(str)
{
if (str.length==0)
{
document.getElementById("spanName").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="ajaxregister.php"
url=url+"?username="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("spanName").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function register(){
var lg=document.getElementById('login');
lg.action='register.php';
if(xmlHttp.responseText=="该用户名可用"){
lg.submit();}
}
ajaxregister.php文件-检测用户输入的用户名是否已存在数据库
<?php
error_reporting(E_ERROR);
ini_set("display_errors","Off");
// //屏蔽php警告
header("Content-type:text/html;charset=utf-8");
$uname=$_GET['username'];
$con = mysql_connect("localhost:3306","root","sa123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM user
WHERE uname='$uname'");
// if(mysql_fetch_array($result))
// {
//$response="用户名已存在";
// }else{
//$response="该用户名可用";
// }
if($rows=mysql_num_rows($result))
{
$response="用户名已存在";
}
else
{
$response="该用户名可用";
}
echo $response;
?>
register.php文件-处理注册信息
<?php
header("Content-type:text/html;charset=utf-8");
$uname=$_POST['username'];
$upassword=$_POST['userpassword'];
$con=mysql_connect("127.0.0.1:3306","root","sa123");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if(mysql_query("INSERT INTO user (uname, upassword)
VALUES ('$uname', '$upassword')")){
echo "注册成功!请重新登录..";
// echo "111";
}
else{
echo "注册失败!再试一遍呗";
}
// while($row = mysql_fetch_array($result))
// {
// echo "
// echo $row['uname'] . "
";
// echo ""; //排版代码
// }
mysql_close($con);
?>
message.php文件-处理提交的留言信息
<?php
error_reporting(E_ERROR);
ini_set("display_errors","Off");
header("Content-type:text/html;charset=utf-8");
$Metext=$_POST['Metext'];
$uname=$_COOKIE['usersname'];
// echo "$uname";
// echo "$Metext";
date_default_timezone_set('Asia/Shanghai');//设置中国时间
$time=date('y-m-d H:i:s',time());//h表示12小时制,H表示24小时制
// echo "$time";
$con=mysql_connect("localhost:3306","root","sa123");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if(mysql_query("INSERT INTO metexts VALUES ('$uname', '$Metext','$time')")){
echo "";
// echo "111";
}
else{
echo "留言失败!";
}
?>
messagesuccess.php-提取留言信息页面
<div id="Tpage">
<div id="top-fiexd">
<span class="item">
<img id="logo" src="img/logo.png">
</span>
<span class="item">
<span class="item" id="item3">
<?php echo "welcome,";echo$_COOKIE["usersname"]; ?>
</span>
</div>
<div id="Tpage-body">
<div id="Tpage-top">
<div id="Mesbox">
<form id="Messageinput" action="message.php" method="POST">
textarea id="Metext" name="Metext"></textarea>
</form>
</div>
</div>
<div id="Tpage-main">
<div id="message">
<span style="font-family:Comic Sans MS;font-size:14px;">
<table border="0" align="center" cellpadding="5" cellspacing="1" >
<?php
error_reporting(E_ERROR);
ini_set("display_errors","Off");
// //屏蔽php警告
header("Content-type:text/html;charset=utf-8");
$con=mysql_connect("localhost:3306","root","sa123");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql = "SELECT * FROM metexts order by mdate desc";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){
?>
<tr bgcolor="#eff3ff">
<td><p>
<big>
<?= $row['message']?>
</big>
<sup>
<?=$row['uname']?>
</sup></p>
<sub>
<?= $row['mdate']?>
</sub>
</td>
</tr>
</table>
</span>
</div>
</div>
</div>
<dir id="Tpage-foot">
</dir>
</div>
2016.10.10