首页 > 代码库 > PHP SESSION 应用 邮件系统实例 高洛峰 细说PHP
PHP SESSION 应用 邮件系统实例 高洛峰 细说PHP
首页 index.php
<?php header(‘content-type:text/html;charset=utf-8‘); /* * * 邮件系统实例 * */ require ‘conn.inc.php‘;//加载连接数据库配置 //如果没有登录 就去登录页面 if(!(isset($_SESSION[‘islogin‘]) && $_SESSION[‘islogin‘]===1)){ header("Location:login.php"); } echo "你好:".$_SESSION[‘username‘]."<a href=http://www.mamicode.com/‘logout.php‘>退出"; if($_SESSION[‘allow_1‘] ==1 ){ echo ‘你有1这个权限!<br/>‘; } if($_SESSION[‘allow_2‘] ==1 ){ echo ‘你有2这个权限!<br/>‘; } if($_SESSION[‘allow_3‘] ==1 ){ echo ‘你有2这个权限!<br/>‘; } $query = "select id,uid,title,ptime,mbody from email where uid=?"; $stmt = $pdo->prepare($query); $stmt->execute(array($_SESSION[‘id‘])); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); echo "你有".$stmt->rowCount()."封邮件<br/>"; echo ‘<table border="1" width="800" align="center">‘; foreach ($data as $value){ echo ‘<tr align="center">‘; echo ‘<td>‘.$value[‘id‘].‘</td>‘; echo ‘<td>‘.$value[‘title‘].‘</td>‘; echo ‘<td>‘.$value[‘ptime‘].‘</td>‘; echo ‘<td>‘.$value[‘mbody‘].‘</td>‘; echo ‘</tr>‘; } echo ‘</table>‘;
登录页面login.php
<?php //处理登录 if(isset($_POST[‘dosubmit‘])){ include ‘conn.inc.php‘; //到数据库查找用户输入的是否正确 $query = "select id, username, allow_1,allow_2,allow_3 from user where username = ? and password =?"; $stmt = $pdo->prepare($query); $stmt ->execute(array($_POST[‘username‘],$_POST[‘password‘])); if($stmt->rowCount()>0){ //将用户信息一次性放到session中 $_SESSION=$stmt->fetch(PDO::FETCH_ASSOC); //加登录标记 $_SESSION[‘islogin‘] = 1; header("Location:index.php"); } } ?> <html> <head> <title>PHP study</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <script type="text/javascript" > <!-- //--> </script> </head> <body > <h1>邮件登录系统</h1> <form action="login.php" method="post"> 用户名:<input type="text" name="username" /><br /> 密码:<input type="password" name="password" /><br /> <input type="submit" name="dosubmit" value="http://www.mamicode.com/登录邮件" /> </form> </body> </html>
数据库连接配置文件conn.inc.php
<?php include ‘config.php‘; try{ $pdo = new PDO(DSN, DBUSER,DBPWD); }catch(PDOException $e){ echo "数据库连接失败:".$e->getMessage(); exit; }
配置文件config.php
<?php const DSN = ‘mysql:host=localhost;dbname=test‘; const DBUSER = ‘root‘; const DBPWD = ‘root‘; header(‘content-type:text/html;charset=utf-8‘); session_start();//开启会话
数据库设计 user表
create table user( id int(11) not null primary key auto_increment, username varchar(50) not null, password char(32) not null, email varchar(80) not null, allow_1 smallint(6) not null default 0, allow_2 smallint(6) not null default 0, allow_3 smallint(6) not null default 0 ); insert into user(username,password,email,allow_1,allow_2,allow_3) values(‘xiaowang1‘,‘1234‘,‘xiaowang@163.com‘,0,0,0); insert into user(username,password,email,allow_1,allow_2,allow_3) values(‘xiaowang2‘,‘1234‘,‘xiaowang@163.com‘,0,0,0); insert into user(username,password,email,allow_1,allow_2,allow_3) values(‘xiaowang3‘,‘1234‘,‘xiaowang@163.com‘,0,0,0); insert into user(username,password,email,allow_1,allow_2,allow_3) values(‘xiaowang4‘,‘1234‘,‘xiaowang@163.com‘,0,0,0); insert into user(username,password,email,allow_1,allow_2,allow_3) values(‘xiaowang5‘,‘1234‘,‘xiaowang@163.com‘,0,0,0); insert into user(username,password,email,allow_1,allow_2,allow_3) values(‘xiaowang6‘,‘1234‘,‘xiaowang@163.com‘,0,0,0);
数据库设计 email表
create table email( id int not null auto_increment , uid int not null default 0, title varchar(80) not null default ‘ ‘, ptime int not null default 0, mbody text, primary key(id) ); insert into email(uid,title,ptime,mbody) values(1,‘wo‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(1,‘wos‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(1,‘wo1‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(1,‘wo2‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(2,‘2wo‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(2,‘2wos‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(2,‘2wo1‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(2,‘2wo2‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(3,‘3wo‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(3,‘3wos‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(3,‘3wo1‘,1222333,‘I am a boy‘); insert into email(uid,title,ptime,mbody) values(3,‘3wo2‘,1222333,‘I am a boy‘);
本文出自 “津沙港湾” 博客,请务必保留此出处http://11410485.blog.51cto.com/11400485/1843070
PHP SESSION 应用 邮件系统实例 高洛峰 细说PHP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。