1
0
Fork 0
This commit is contained in:
Chenx221 2024-01-16 14:52:51 +08:00
parent d22843d66a
commit 7d81489376
10 changed files with 281 additions and 0 deletions

12
.idea/dataSources.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="db_database11@localhost" uuid="7b1c894b-03c5-4e7c-a1d6-e45cc1023c83">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:3306/db_database11</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

7
.idea/sqldialects.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/11.test4.php" dialect="MySQL" />
<file url="PROJECT" dialect="MySQL" />
</component>
</project>

11
11.test1.php Normal file
View File

@ -0,0 +1,11 @@
<?php
setcookie('TMCookie','www.mrbccd.com');
setcookie('testcookie','helloworld',time()+300);
if(!isset($_COOKIE['last_visit_time'])){
echo '你是第一次访问该站点';
}else{
echo '上次访问该站点的时间为:'.date('Y-m-d H:i:s',$_COOKIE['last_visit_time']);
}
setcookie('last_visit_time',time(),time()+60);
session_start();
$_SESSION['admin']=null;

41
11.test2.default.php Normal file
View File

@ -0,0 +1,41 @@
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['user'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['role'] = null;
if ($_SESSION['user'] == 'user' && $_SESSION['password'] == 'user') {
$_SESSION['role'] = 'user';
} elseif ($_SESSION['user'] == 'admin' && $_SESSION['password'] == 'admin') {
$_SESSION['role'] = 'admin';
} else {
echo <<<EOL
<script>
alert('用户名或密码错误');
history.back();
</script>
<noscript>
用户名或密码错误
</noscript>
EOL;
exit();
}
}
if (empty($_SESSION['role'])) {
echo <<<EOL
<script>
alert('非法访问!');
location.href = "11.test2.php";
</script>
<noscript>
非法访问
</noscript>
EOL;
exit();
}
echo '你好,' . $_SESSION['user'] . '<br>你的身份是' . $_SESSION['role'];
echo '<br>' . '<a href="11.test2.logout.php">退出登录</a>';

7
11.test2.logout.php Normal file
View File

@ -0,0 +1,7 @@
<?php
session_start();
unset($_SESSION['user']);
unset($_SESSION['password']);
unset($_SESSION['role']);
session_destroy();
header('location:11.test2.php');

55
11.test2.php Normal file
View File

@ -0,0 +1,55 @@
<?php
session_start();
if (isset($_SESSION['role'])) {
echo '<script>alert("你已经登录,不需要再次登录");location.href="11.test2.default.php";</script>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="11.test2.default.php" method="post" id="login_form1">
<table>
<tr>
<td>
<label for="username1">用户名</label>
</td>
<td>
<input type="text" name="username" id="username1" required>
</td>
</tr>
<tr>
<td>
<label for="password1">密码</label>
</td>
<td>
<input type="password" name="password" id="password1" required>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" name="submit" id="submit1" value="登录" oninput="return check(form)">
<input type="reset" name="reset" id="reset1" value="重置">
</td>
</tr>
</table>
</form>
<script>
function check(login) {
if (login.username.value === "") {
alert("用户名不能为空");
form.username.focus();
return false;
}
if (login.password.value === "") {
alert("密码不能为空");
form.paasword.focus();
return false;
}
form.submit();
}
</script>
</body>

15
11.test3.php Normal file
View File

@ -0,0 +1,15 @@
<?php
$path = './tmp/';
// 判断给定的session存储位置是否存在,不存在则创建对应文件夹
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
session_save_path($path);
session_cache_limiter('private');
$cache_limit = session_cache_limiter();
session_cache_expire(30);
$cache_expire = session_cache_expire();
echo 'The cache limit is ' . $cache_limit . '<br>';
echo 'The cache expire is ' . $cache_expire . '<br>';
session_start();
$_SESSION['username'] = true;

97
11.test4.php Normal file
View File

@ -0,0 +1,97 @@
<?php
class MySessionHandler implements SessionHandlerInterface
{
public function close(): bool
{
global $handle;
mysqli_close($handle);
return true;
}
public function destroy(string $id): bool
{
global $handle;
$stmt = $handle->prepare("delete from tb_session where session_key = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->affected_rows;
$stmt->close();
return $result;
}
public function gc(int $max_lifetime): int|false
{
global $handle;
$lapse_time = time();
return $handle->query("delete from tb_session where session_time < ($lapse_time-$max_lifetime)");
}
public function open(string $path, string $name): bool
{
global $handle;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$handle = new mysqli('localhost:3306', 'root', 'chenx221', 'db_database11');
$handle->set_charset('utf8mb4');
return true;
} catch (Exception $e) {
error_log("Failed to connect to database: " . $e->getMessage());
return false;
}
}
public function read(string $id): string|false
{
global $handle;
$stmt = $handle->prepare("select session_data from tb_session where session_key = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$session_data = null;
$stmt->bind_result($session_data);
$stmt->fetch();
$stmt->close();
if ($session_data != null) {
return $session_data;
} else {
return '';
}
}
public function write(string $id, string $data): bool
{
$id='pvqdqck4rurolsi7qaha4n4ul1';
global $handle;
$lapse_time = time();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$handle = new mysqli('localhost', 'root', 'chenx221', 'db_database11');
$handle->set_charset('utf8mb4');
$stmt = $handle->prepare("select session_data from tb_session where session_key = ?");
$stmt->bind_param("s", $id, );
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows() == 0) {
$stmt2 = $handle->prepare("insert into tb_session values(?,?,?)");
$stmt2->bind_param("ssi", $id, $data, $lapse_time);
} else {
$stmt2 = $handle->prepare("update tb_session set session_data = ? , session_time = ? where session_key = ? ");
$stmt2->bind_param("sis", $data, $lapse_time, $id);
}
$stmt2->execute();
$result = $stmt2->affected_rows;
$stmt2->close();
$stmt->close();
return $result;
}
}
session_set_save_handler(new MySessionHandler());
session_start();
$_SESSION['user'] = 'mr';
$_SESSION['pwd'] = 'mrsoft';

View File

@ -0,0 +1,35 @@
/*
Navicat Premium Data Transfer
Source Server : local
Source Server Type : MySQL
Source Server Version : 80200 (8.2.0)
Source Host : localhost:3306
Source Schema : db_database11
Target Server Type : MySQL
Target Server Version : 80200 (8.2.0)
File Encoding : 65001
Date: 16/01/2024 14:52:09
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_session
-- ----------------------------
DROP TABLE IF EXISTS `tb_session`;
CREATE TABLE `tb_session` (
`session_key` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`session_data` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
`session_time` int NULL DEFAULT NULL,
PRIMARY KEY (`session_key`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_session
-- ----------------------------
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -0,0 +1 @@
username|b:1;