- PHP教程
- PHP-主页
- PHP - 简介
- PHP-环境设置
- PHP - 语法概述
- PHP - 变量类型
- PHP - 常量
- PHP - 运算符类型
- PHP - 决策
- PHP - 循环类型
- PHP-数组
- PHP-字符串
- PHP - 网络概念
- PHP - 获取和发布
- PHP - 文件包含
- PHP - 文件和 I/O
- PHP - 函数
- PHP-Cookie
- PHP-会话
- PHP - 发送电子邮件
- PHP-文件上传
- PHP - 编码标准
- 高级PHP
- PHP - 预定义变量
- PHP-正则表达式
- PHP - 错误处理
- PHP - 错误调试
- PHP - 日期和时间
- PHP 和 MySQL
- PHP 和 AJAX
- PHP 和 XML
- PHP——面向对象
- PHP - 面向 C 开发人员
- PHP - 适合 PERL 开发人员
- PHP 表单示例
- PHP-表单介绍
- PHP - 验证示例
- PHP - 完整表格
- PHP框架作品
- PHP-框架工程
- PHP - 核心 PHP 与 Frame Works
- PHP 设计模式
- PHP - 设计模式
- PHP 函数参考
- PHP - 内置函数
- PHP 有用资源
- PHP - 问题与解答
- PHP - 有用的资源
- PHP - 讨论
PHP - 登录示例
PHP 使用会话登录
PHP 登录脚本用于为我们的网页提供身份验证。该脚本在提交用户登录按钮后执行。
登录页面
登录页面应如下所示并且基于会话工作。如果用户关闭会话,则会删除会话数据。
<?php
ob_start();
session_start();
?>
<?
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
?>
<html lang = "en">
<head>
<title>Tutorialspoint.com</title>
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<style>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #ADABAB;
}
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
color: #017572;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
border-color:#017572;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-color:#017572;
}
h2{
text-align: center;
color: #017572;
}
</style>
</head>
<body>
<h2>Enter Username and Password</h2>
<div class = "container form-signin">
<?php
$msg = '';
if (isset($_POST['login']) && !empty($_POST['username'])
&& !empty($_POST['password'])) {
if ($_POST['username'] == 'tutorialspoint' &&
$_POST['password'] == '1234') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'tutorialspoint';
echo 'You have entered valid use name and password';
}else {
$msg = 'Wrong username or password';
}
}
?>
</div> <!-- /container -->
<div class = "container">
<form class = "form-signin" role = "form"
action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
?>" method = "post">
<h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
<input type = "text" class = "form-control"
name = "username" placeholder = "username = tutorialspoint"
required autofocus></br>
<input type = "password" class = "form-control"
name = "password" placeholder = "password = 1234" required>
<button class = "btn btn-lg btn-primary btn-block" type = "submit"
name = "login">Login</button>
</form>
Click here to clean <a href = "logout.php" tite = "Logout">Session.
</div>
</body>
</html>
注销.php
它将删除会话数据。
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
echo 'You have cleaned session';
header('Refresh: 2; URL = login.php');
?>
它将产生以下结果 -

