PHP-MySQL登录
PHP-MySQL登录 本教程演示了如何使用MySQL数据库创建登录页面。在进入代码部分之前,您将需要特殊权限…
PHP-MySQL登录
本教程演示了如何使用MySQL数据库创建登录页面。在进入代码部分之前,您将需要特殊权限才能创建或删除MySQL数据库。因此,假设您有权访问root用户,则可以使用mysql mysqladmin binary创建任何数据库。
Config.php
Config.php文件包含有关MySQL数据库配置的信息。
|
1
2
3
4
5
6
|
<?php define(‘DB_SERVER’, ‘localhost:3036’);
define(‘DB_USERNAME’, ‘root’);
define(‘DB_PASSWORD’, ‘rootpassword’);
define(‘DB_DATABASE’, ‘database’);
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
|
Login.php
登录PHP正在获取有关php脚本和HTML脚本的信息以进行登录。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php include(“config.php”);
session_start();
if($_SERVER[“REQUEST_METHOD”] == “POST”) {
// username and password sent from form
$myusername = mysqli_real_escape_string($db,$_POST[‘username’]);
$mypassword = mysqli_real_escape_string($db,$_POST[‘password’]);
$sql = “SELECT id FROM admin WHERE username = ‘$myusername’ and passcode = ‘$mypassword’”;
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row[‘active’];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
session_register(“myusername”);
$_SESSION[‘login_user’] = $myusername;
header(“location: welcome.php”);
}else {
$error = “Your Login Name or Password is invalid”;
}
}
??>
<title>Login Page</title>
<style type=”text/css”>
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label {
font-weight:bold;
width:100px;
font-size:14px;
}
.box {
border:#666666 solid 1px;
}
</style>
<div align=“center”>
<div style=“width:300px; border: solid 1px #333333; “ align=“left”>
<div style=“background-color:#333333; color:#FFFFFF; padding:3px;”><b>Login</b></div>
<div style=“margin:30px”>
<form action=“” method=“post”>
<label>UserName :</label><input type=“text” name=“username”><br><br>
<label>Password :</label><input type=“password” name=“password”><br><br>
<input type=“submit” value=” Submit “><br>
</form>
<div style=“font-size:11px; color:#cc0000; margin-top:10px”><?php echo $error; ??></div>
</div>
</div>
</div>
|
welcome.php
成功登录后,将显示欢迎页面。
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php include(‘session.php’);
??>
<title>Welcome </title>
<h2>Welcome <?php echo $login_session; ??></h2>
<h2><a href=“logout.php”>Sign Out</a></h2>
|
登出页面
注销页面包含有关如何从登录会话中注销的信息。
|
1
2
3
4
5
6
|
<?php session_start();
if(session_destroy()) {
header(“Location: login.php”);
}
??>
|
session.php
Session.php将验证会话,如果没有会话,它将重定向到登录页面。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php include(‘config.php’);
session_start();
$user_check = $_SESSION[‘login_user’];
$ses_sql = mysqli_query($db,“select username from admin where username = ‘$user_check’ “);
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_session = $row[‘username’];
if(!isset($_SESSION[‘login_user’])){
header(“location:login.php”);
die();
}
??>
|
类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!