You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于PHP的A/B分流测试问题:HTML页面中PHP调用HTML文件显示空白

Hey there! Let's work through why your PHP code is serving up a blank page instead of rotating your ads. Here are the most likely culprits and fixes to try:

Common Issues & Solutions

1. PHP isn't running on your server

If your web host or local environment doesn't have PHP enabled or configured correctly, the server won't process your PHP code—this often leads to a blank page.

Test this quickly: create a file named test.php with this content:

<?php phpinfo(); ?>

If accessing this file in your browser shows nothing but the raw code or a blank screen, you need to enable PHP. For local setups (like XAMPP/WAMP), make sure the Apache/PHP service is running. For shared hosting, reach out to your provider to confirm PHP is enabled for your site.

2. Incorrect file paths for your ad HTML

The include() function uses relative paths, so ad1.html and ad2.html need to be in the same folder as your PHP file. If they're in a subdirectory (like /ads/), update the path to something like include('./ads/ad1.html').

To debug path issues temporarily enable error reporting at the top of your script—this will show you if PHP can't find the files:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (rand(0,1) == 1) { 
    include ('ad1.html'); 
} else { 
    include ('ad2.html'); 
}
?>

3. Tiny syntax error in your code

You have an extra semicolon at the end of your else block (}; ?>). While this might not always break everything, it's good practice to fix it:

<?php
if (rand(0,1) == 1) { 
    include ('ad1.html'); 
} else { 
    include ('ad2.html'); 
}
?>

4. Empty or broken ad HTML files

Double-check that ad1.html and ad2.html actually have valid content. Try accessing them directly in your browser—if they show a blank page too, the issue is with those files, not your PHP logic.

Optimized Code with Error Handling

Here's a more robust version of your script that checks if ad files exist before including them, plus debugging tools:

<?php
// Enable error reporting for debugging (remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Define your ad file paths
$ad1 = 'ad1.html';
$ad2 = 'ad2.html';

// Randomly pick an ad and include it (with error checks)
if (rand(0, 1) === 1) {
    file_exists($ad1) ? include($ad1) : echo "Error: Ad 1 file missing";
} else {
    file_exists($ad2) ? include($ad2) : echo "Error: Ad 2 file missing";
}
?>

内容的提问来源于stack exchange,提问作者Zoox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:37:41