gpt4 book ai didi

php - 跟踪像素插入数据库时​​生成 500(内部服务器错误)

转载 作者:行者123 更新时间:2023-11-30 22:06:33 25 4
gpt4 key购买 nike

我正在创建一个跟踪像素并使用了 this answer作为起点。但是,在我尝试将数据插入数据库之前,一切正常。那时我收到 500(内部服务器错误)。

这是我的代码:

<?php
$im=imagecreate(1,1);
$white=imagecolorallocate($im,255,255,255);

imagesetpixel($im,1,1,$white);
header("content-type:image/jpg");
imagejpeg($im);
imagedestroy($im);

$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];

function getUserIP() {
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];

if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
} else if (filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $forward;
} else {
$ip = $remote;
}

return $ip;
}

$user_ip = getUserIP();
$ip_integer = ip2long($user_ip);
$web_url = 'myurl.com';
$sqlWebLeads = "INSERT INTO ip_details (ip_address, web_url)
VALUES ('$ip_integer','$web_url')";
$wpdb->query($sqlWebLeads);
?>

有什么想法吗?

最佳答案

It's running on a wordpress site so $wpdb->query is a global function and does not usually need to be defined.

这不是魔术,所以,如果您制作一个独立的 php 文件,它不会有 wordpress 全局定义对象,作为您的 $wpdb 数据库链接。

您需要在页面中设置该对象才能使用它,请查看 Using WPDB in standalone script?

$path = $_SERVER['DOCUMENT_ROOT'];

include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

// $wpdb is available, do stuff

关于php - 跟踪像素插入数据库时​​生成 500(内部服务器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com