我在PHP下面的工作代码
<?php
include('../../lib/qrlib/qrlib.php');
QRcode::png('PHP QR Codep :)');
?>
奇怪的是,如果我在前面放一个空格
<?php
然后相同的代码不起作用&amp;错误日志也没有显示任何细节。此外,如果我在此代码之前的顶部代码中放置任何其他函数,则不会生成QR代码。日志中也没有错误。
我在这里缺少什么?
更新
这是我的代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$myErr = "";
$myid = "";
function generateRandomCode() {
// returns random code
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["myid"])) {
$myidErr = "myID is required";
}
$code = generateRandomCode();
}
?>
<h2>My Project</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
My ID: <input type="text" name="myid" value="">
<span class="error">* <?php echo $myidErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>QR Code:</h2>";
$tem = '"myid":"' . $myid . '","code":"' . $code . '"}';
include('../../lib/qrlib.php');
QRcode::png($tem);
?>
</body>
</html>
答案 0 :(得分:3)
查看QRcode::png()
的源代码,我可以看到它在显示PNG图像数据之前发送了Content-Type
标头。这是通知接收浏览器或设备数据是PNG图像所必需的。
// Excerpted from source:
if ($filename === false) {
Header("Content-type: image/png");
ImagePng($image);
// etc...
https://github.com/t0k4rt/phpqrcode/blob/f0567ce717fa1172cb66c48ebae017a094de64b1/qrimage.php#L30
如果在调用该函数之前在任何类型的打开<?php
或任何输出之前有前导空格,PHP将无法发送必要的标题。
有关此问题及其所有潜在原因的详细信息,请参阅How to fix Headers already sent errors in PHP。
始终在开发和测试代码时,请确保已启用PHP的错误显示。如果它已经打开,你会看到PHP发出与已经发送的标题相关的警告。
警告:无法修改标题信息 - 已由......等发送的标题...
// At the very top of your script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
或者在php.ini中设置error_reporting
和display_errors
。您在日志中看到没有错误的事实表明您在php.ini中禁用了log_errors
,或者error_reporting
的保守设置未报告E_WARNING
错误。最好使用E_ALL
。
您试图在当前生成HTML页面的同一脚本中调用QRcode::png()
。您实际上无法做到这一点,因为必须生成QR码并将其插入<img>
标签。尽管它是在运行时由PHP生成的,但从浏览器的角度来看,它与从磁盘上的文件读取的真实图像没有任何不同,因此您必须在HTML标记中使用与从磁盘文件中相同的方式。
正确处理此问题的最简单方法是将QR代码生成移动到不同的PHP文件,这是仅操作发生的地方。然后在<img>
代码src
中引用该文件。
此PHP脚本旨在引用为example.com/generate_qrcode.php?myid=abcdefg
。如果您从浏览器中这样调用它,它应该只显示裸QR码。
// Contains only QR generation
// Move the $code here, and pass the myid in the query string
// Also move the function definition here
function generateRandomCode() {
// whatever...
}
$code = generateRandomCode();
// This value was originally POSTed to the main script, so
// it needs to be passed from the main script to this one via the query string
$myid = $_GET['myid'];
$tem = '"myid":"' . $myid . '","code":"' . $code . '"}';
include('../../lib/qrlib.php');
QRcode::png($tem);
要真的在HTML页面的上下文中以您想要的方式使用它,但需要<img>
标记。
在其查询字符串中加入<img>
代码,该代码会将QR代码和传递给$myid
。 PHP / HTML应该不调用QRcode::png()
本身。
<img src="generate_qrcode.php?myid=<?php echo htmlspecialchars($myid); ?>" alt="QR code" />
这会产生类似<img src="generate_qrcode.php?myid=abcdefg" alt="QR code" />
对于完整上下文,您的主脚本现在看起来像:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$myErr = "";
$myid = "";
// This function is defined instead in the QR code script...
//function generateRandomCode() {
// returns random code
//}
// POST handling is the same!
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["myid"])) {
$myidErr = "myID is required";
}
// But this is done in the other QR code script
//$code = generateRandomCode();
}
?>
<h2>My Project</h2>
<!-- The form is the same! -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
My ID: <input type="text" name="myid" value="">
<span class="error">* <?php echo $myidErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($myid) {
echo "<h2>QR Code:</h2>";
// Now include an <img> tag
echo "<img src='generate_qrcode.php?myid=$myid' alt='QR code' />";
}
?>