本学渣近期刚刚学习php写了个记事本来练手,为了让自己对php的理解运用更好特地把编写过程及代码写出来加深印象。首先从登陆判断开始。
<html>
<head>//头部标签
<title>This is my first diary entry interface</title> //这句话的意思是“这是我的第一个记事本程序”它是显示在地址栏的东西也就是说程序的title(标题)
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
//首先设定字符集本页面使用的是utf8编码,(http://kinglyhum.iteye.com/blog/827807)这里有对http-equiv的详细解释,感谢一下!
</head>//头部标签
<div><form action="index.php" >
<input type="submit" value="现在返回登录页面" /> </form> </div>//其实登陆判断只用php就可以了这块加了html主要的目的就是这句话,它的作用是当用户登陆失败的时候有个3秒跳转的时间为了更好的用户体验有这句话增加了一个跳转按钮,直接跳回登陆界面,使用了submit和action来支持跳转动作。
<?php//php标签<?php?>ob_start();
$username=($_POST['username']);//php变量约定:1.php变量名区分大小写2.php变量必须以$开头3.php变量不能以数字开头4.php变量可以以 _ 下划线开头
$password=($_POST['password']);
include('config.php');//引用数据库的连接文件
mysql_select_db($db,$conn) or die();//判断数据库是不是连接成功,如果不成功就杀死进程。
$sqlstr="SELECT username,password FROM user WHERE username='$username' and password='$password'";//从数据库中取出与前台传过来对应的用户名和密码相当于在做信息的匹配$result=mysql_query($sqlstr,$conn) or die();//如果信息不匹配就杀死进程显示“'登陆失败,你可能使用了错误的用户名或密码,请检查 3秒后跳回登录页面';”$row=mysql_fetch_array($result);
if($row){//如果匹配成功进行下面的操作
session_start();
$_SESSION["username"]="$username";//此时session的作用是存储username到本地的cookies中以备后面的页面使用
header("Location:bianji/index.html");//跳转到后续的编辑页面
exit;
}
else
{//本部分是使用邮箱做用户名登陆的时候做判断用的原理同上
$sqlst="SELECT email,password FROM user WHERE email='$username' and password='$password'";
$result=mysql_query($sqlst,$conn) or die();
$row=mysql_fetch_array($result);
if($row)
{
$res=mysql_query("select username from user where email='$username'");
$rows=mysql_fetch_array($res);
$name=$rows['username'];
session_start();
$_SESSION["username"]="$name";
header("Location:bianji/index.php");
exit;
}
else{
header("refresh:3;url=index.php");//refresh:3 三秒后刷新url的地址
echo '登陆失败,你可能使用了错误的用户名或密码,请检查
3秒后跳回登录页面';
}
}
ob_flush();
?>
</html>