一天一篇之php学习篇2

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

以下代码检测可用

 1 <?php
 2 if(isset($_POST["submit"]))
 3 {
 4     $username = $_POST['user'];//用户名
 5     $pwd = $_POST['pwd'];//密码
 6     
 7     /*数据库连接方法*/
 8     $hostname = 'localhost';
 9     $user = "root";
10     $password = "root";
11     $link = mysql_connect($hostname,$user,$password);
12     //mysql_query("set names utf-8");
13     mysql_select_db('cms',$link);//选择库名
14     $query = "select * from admin where username = '$username' and password = '$pwd'";
15     $rs = mysql_query($query);//执行查询
16     if(mysql_num_rows($rs) == 1)
17     {
18         $_SESSION['pwd'] = $_POST['pwd'];//数据匹配
19         $_SERVER['admin'] = session_id();
20         echo "<script>alert('登录成功');window.location = 'admin_index.php';</script>";
21     }
22 }
23 ?>
24 
25 <?php
26 /*退出系统*/
27 if(!empty($_GET['tj']))
28 {    
29     session_start();
30     session_destroy();
31     echo "<script>alert('退出成功');window.location = 'admin.php';</script>";
32 
33 }
34 ?>
35 
36 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
37 <html xmlns="http://www.w3.org/1999/xhtml">
38 <head>
39 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
40 <title>cms管理系统</title>
41 <style>
42 body{background:#ccc;}
43 input{display:block; margin:10px 0;}
44 </style>
45 </head>
46 
47 <body>
48 <form action="" method="post" name="frm" id="frm">
49     <label>用户名:<input type="text" size="20" maxlength="20" name="user" /></label>
50     <label>密  码:<input type="password" name="pwd" /></label>
51     <input type="submit" value="登录" name="submit" />
52 </form>
53 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
54 <script>
55 jQuery.fn.center = function () {
56     this.css("position","absolute");
57     this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
58     this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
59     return this;
60 }
61 $('#frm').center();
62 </script>
63 
64 </body>
65 </html>

数据库片段

 

posted @ 2014-01-11 09:39  独步寻花  阅读(110)  评论(0编辑  收藏  举报