PHP開發留言板教學之登入功能
登入功能:我們先來看以下html程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎登录</title>
<style type="text/css">
*{margin: 0px;padding: 0px;}
body{background:#eee;}
#div{width:300px;height:400px;background:#B1FEF9;margin:0 auto;margin-top:150px;
border-radius:20px;}
h3{margin-left:48px;padding-top:60px;}
h4{margin-left:120px;padding-top:60px;font-size: 18px;}
#cnt{width:280px;height:370px;margin-left:33px;padding-top:60px;}
.sub{width:70px;height:30px;border:1px solid #fff;background:#eee;
margin-left:28px;margin-top:20px;}
.sub1{
width:70px;height:30px;border:1px solid #fff;background:#eee;margin-left:150px;margin-top:20px;}
</style>
</head>
<body>
<div id="div">
<h3>欢迎登陆后台管理系统</h3>
<div id="cnt">
<form method="post" action="main.php">
用户名:<input type="text" placeholder="请输入用户名" name="username">
<br><br>
密 码:<input type="password" placeholder="请输入密码" name="password">
<br><br>
<input type="submit" value="登录" class="sub">
</form>
</div>
</div>
</body>
</html>表單提交到main.php 下面我們來分析一下main.php
當我們登入了之後,我們如果有很長一段事件沒有沒網頁進行操作,當你再次操作的時候需要去登錄,這個會用到我們的session的知識
首先我們要打開session
session_start();
然後我們要把連結資料庫的檔案conn.php 引進進來
require_once('conn.php');
取得表單的信息,然後把表單的資訊存入session
$name = $_POST['username'];
$pwd = md5($_POST['password']);
$_SESSION['name']=$name;
$_SESSION['pwd']=$pwd;
#下面我們去資料庫查詢,如果資料庫存在表單提交的信息,那麼我們應該是讓該表單提交的信息可以進行登入操作
$sql = "select * from user where username='$name' and password='$pwd'";
$info = mysql_query($sql);
info$row = mysql_fetch_row($sql);
if($row) {
echo "<script>alert('登錄成功');location.href='message.php';</script>";
} alert('登入失敗')</script>";
echo "<script>location.href='login.php';</script>"; //登入失敗,跳到另外一個頁面
}
main.php 完整程式碼如下:
<?php
session_start();
require_once('conn.php');
$name = $_POST['username'];
$pwd = md5($_POST['password']);
$_SESSION['name']=$name;
$_SESSION['pwd']=$pwd;
$sql = "select * from user where username='$name' and password='$pwd'";
$info = mysql_query($sql);
$row = mysql_fetch_row($info);
if($row){
echo "<script>alert('登录成功');location.href='message.php';</script>";
}else{
echo "<script>alert('登录失败')</script>";
echo "<script>location.href='login.php';</script>"; //登录失败,跳转到另外一个页面
}
?>- 課程推薦
- 課件下載
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~ 















