PHP 获取用户 IP 地址并插入数据库示例
这是一个获取用户 IP 地址并将其插入数据库的示例代码。请注意,获取用户 IP 地址的方法可能因服务器配置而有所不同,这里使用了$_SERVER['REMOTE_ADDR']来获取 IP 地址。
<?php
$title = $_POST['title'];
$content = $_POST['content'];
$describes = $_POST['describes'];
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
$con = mysqli_connect('localhost', 'efchp', '123456', 'wenzhang');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_set_charset($con, 'utf8mb4');
$stmt = mysqli_prepare($con, 'INSERT INTO `efchp_blog_essay` (`id`, `title`, `new_title`, `content`, `new_content`, `describes`, `new_describes`, `ip`, `new_ip`, `time`, `new_time`, `remarks`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '''');');
mysqli_stmt_bind_param($stmt, 'ssssssissi', $title, $title, $content, $content, $describes, $describes, $ip, $ip, $time, $time);
$result = mysqli_stmt_execute($stmt);
if ($result) {
echo '插入成功!<a href='/'>回到主页</a>';
} else {
echo '插入失败!错误信息:' . mysqli_error($con);
}
mysqli_close($con);
?>
请根据您的数据库配置和需求进行适当修改。
原文地址: https://www.cveoy.top/t/topic/phNa 著作权归作者所有。请勿转载和采集!