gpt4 book ai didi

php - 谁能告诉我如何在使用跟踪像素时消除跨源读取阻塞 (CORB) 错误?

转载 作者:行者123 更新时间:2023-12-03 13:42:03 25 4
gpt4 key购买 nike

我不确定为什么在尝试使用跟踪像素时会出现 CORB 错误。
我认为如果返回图像并且内容类型是图像(gif 或 jpeg),则不会发生这些错误。确切的错误是:

跨源读取阻塞 (CORB) 阻止了跨源响应 http://analytics.epizy.com/pixel.php?a=5&ip=198.91.81.4&pge=/tracktester.php使用 MIME 类型 text/html。

我的代码如下:

<?php
$ip = $_SERVER['SERVER_ADDR'];
$pge = $_SERVER['REQUEST_URI'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Testing of Tracking Pixel</title>
</head>
<body>
<div>
<p>Testing of Tracking Pixel</p>
<img src="http://analytics.epizy.com/pixel.php?a=5&ip=<?php echo $ip ?>&pge=<?php echo $pge ?>">
</div>
</body>
</html>

Pixel.php 如下:
<?php

// Create an image, 1x1 pixel in size
$im=imagecreate(1,1);

// Set the background colour
$white=imagecolorallocate($im,255,255,255);

// Allocate the background colour
imagesetpixel($im,1,1,$white);

// Set the image type
header("content-type:image/jpg");

// Create a JPEG file from the image
imagejpeg($im);

// Free memory associated with the image
imagedestroy($im);



if (isset($_GET['a'])) {
// (Do|log) act on a
//echo "Hope to see a value here = " . $_GET['a'];

$servername = "sql207.epizy.com";
$username = "epiz_22387889";
$password = "";
$db = "epiz_22387889_analytics";

$conn = mysqli_connect($servername,$username, $password,$db);

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = $conn->prepare("INSERT INTO testAnalyse (hitA, hitIP, hitPage, hitWhen) VALUES (?,?, ?,now())");
$stmt->bind_param("iss", $_GET['a'], $_GET['ip'], $_GET['pge']);

$stmt->execute();
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$stmt->close();
$conn->close();




}
?>

任何帮助将不胜感激。

谢谢,
特里

最佳答案

I thought that these errors do not occur if images are served back and the content type is an image (gif or jpeg).


你是对的。问题是你的像素给出了 500 错误代码:
enter image description here
我也测试了你的 pixel.php本地代码,它不会抛出任何错误。但是在崩溃之前,它会写入输出图像,因此客户端会收到它。
问题出在这段代码的某个地方
if (isset($_GET['a'])) {
// (Do|log) act on a
//echo "Hope to see a value here = " . $_GET['a'];

$servername = "sql207.epizy.com";
$username = "epiz_22387889";
$password = "";
$db = "epiz_22387889_analytics";

$conn = mysqli_connect($servername,$username, $password,$db);

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = $conn->prepare("INSERT INTO testAnalyse (hitA, hitIP, hitPage, hitWhen) VALUES (?,?, ?,now())");
$stmt->bind_param("iss", $_GET['a'], $_GET['ip'], $_GET['pge']);

$stmt->execute();
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$stmt->close();
$conn->close();




}
如果我猜错了,我会感到惊讶,但请看这里:
$pge =  $_SERVER['REQUEST_URI'];
...
&pge=<?php echo $pge ?>
你逃不掉 $_SERVER['REQUEST_URI'] ,所以任何东西都可能出现在这里并破坏请求。改为这样做:
$ip = urlencode($_SERVER['SERVER_ADDR']);
$pge = urlencode($_SERVER['REQUEST_URI']);
更新:正如@Martin 所说, bind_param 也存在问题。 ,因为您将第一个参数设置为 i (整数),它也必须接受整数:
$stmt->bind_param("iss", (int)$_GET['a'], $_GET['ip'], $_GET['pge']);

关于php - 谁能告诉我如何在使用跟踪像素时消除跨源读取阻塞 (CORB) 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345195/

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