您的位置:首页 > 编程语言 > PHP开发

一天一篇之php学习篇2

2014-01-11 09:39 363 查看
今天简单的写了下php后台登录,所使用的方法为 $_post,$_SESSION,及数据库连接,刚开始对session的定义不是很清楚,查看了帮助文档,里面的定义为'当前脚本可用 SESSION 变量的数组',解释的很抽象,其实session就是客户端与服务端会话,当session_start()运行的时候,在服务器产生了一个session文件,随之也产生了唯一一个对应的session id,$_SESSION['client']客户端注册一把钥匙来打开服务端的锁。

以下代码检测可用

<?php
if(isset($_POST["submit"]))
{
$username = $_POST['user'];//用户名
$pwd = $_POST['pwd'];//密码

/*数据库连接方法*/
$hostname = 'localhost';
$user = "root";
$password = "root";
$link = mysql_connect($hostname,$user,$password);
//mysql_query("set names utf-8");
mysql_select_db('cms',$link);//选择库名
$query = "select * from admin where username = '$username' and password = '$pwd'";
$rs = mysql_query($query);//执行查询
if(mysql_num_rows($rs) == 1)
{
$_SESSION['pwd'] = $_POST['pwd'];//数据匹配
$_SERVER['admin'] = session_id();
echo "<script>alert('登录成功');window.location = 'admin_index.php';</script>";
}
}
?>

<?php
/*退出系统*/
if(!empty($_GET['tj']))
{
session_start();
session_destroy();
echo "<script>alert('退出成功');window.location = 'admin.php';</script>";

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cms管理系统</title>
<style>
body{background:#ccc;}
input{display:block; margin:10px 0;}
</style>
</head>

<body>
<form action="" method="post" name="frm" id="frm">
<label>用户名:<input type="text" size="20" maxlength="20" name="user" /></label>
<label>密  码:<input type="password" name="pwd" /></label>
<input type="submit" value="登录" name="submit" />
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script>
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$('#frm').center();
</script>

</body>
</html>


数据库片段



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: