您好,登錄后才能下訂單哦!
首頁 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='logout.php'>退出</a>"; 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 > <h2>郵件登錄系統</h2> <form action="login.php" method="post"> 用戶名:<input type="text" name="username" /><br /> 密碼:<input type="password" name="password" /><br /> <input type="submit" name="dosubmit" value="登錄郵件" /> </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');
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。