加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
add.php 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
李韬 提交于 2016-08-18 22:52 . 实现私聊机器人
<?php
/**
* @功能 将表单插入数据库
*/
$link = require_once ('./config.php');
require_once ('./function.php');
mysqli_query($link, 'set names utf8');
//获取ip
if ($_SERVER['HTTP_X_REAL_IP']) {//nginx 代理模式下,获取客户端真实IP
$ip = $_SERVER['HTTP_X_REAL_IP'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos)
unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR']; //浏览当前页面的用户计算机的ip地址
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
//颜色组
$color = array('success', 'info', 'warning', 'danger');
if (!isset($_COOKIE['color'])) {
$colorno = rand(0, 3);
//一天一换颜色
setcookie("color", $color[$colorno], time() + 3600 * 24);
$selfcolor = $color[$colorno];
} else
$selfcolor = $_COOKIE['color'];
if (empty($_COOKIE['ip']))
setcookie("ip", $ip, time() + 3600 * 24 * 360);
if (!empty($content = $_POST['content'])) {
$time = date('Y-m-d H:i:s', time(0));
$content = htmlentities($content); //对html特殊字符编码
$selfcolor = empty($selfcolor) ? 'success' : $selfcolor;
$location = position($ip);
if ($_GET['talk'] == "selftalk") {
$table = "selftalk";
$replymsg = replay($content, $ip);
} else {
$table = "talk";
}
$sql = "INSERT INTO " . $table . " VALUES(NULL,'匿名者','$ip','$content','$time','$selfcolor','$location')";
$data = mysqli_query($link, $sql);
if ($_GET['talk'] == "selftalk") {
$sql = "INSERT INTO " . $table . " VALUES(NULL,'小木','$ip','$replymsg','$time','success','中国湖北武汉')";
$data = mysqli_query($link, $sql);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化