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

本地主机与服务器运行LK域名查询脚本结果不一致问题

Troubleshooting Your LK Domain Lookup Script (Local Works, Server Doesn’t)

Hey there, let’s break down why your script works on localhost but fails when deployed to your server. These are the most common issues and how to fix them:

1. PHP file_get_contents is Disabled or Restricted

Many web hosts disable allow_url_fopen (the setting that lets file_get_contents fetch external URLs) for security reasons. Local environments usually have this enabled by default, which is why it works there.

Fix: Replace file_get_contents with cURL, which is more widely allowed and flexible for external requests.

2. Target Site Blocks Server-Side Requests

The domains.lk service might be blocking requests coming from your server’s IP, or detecting that the request isn’t from a browser (missing User-Agent header). Local requests might slip through because they’re coming from your personal machine with a browser-like context.

Fix: Add a User-Agent header to mimic a browser request when fetching the content.

3. Variable Scope & Undefined Variable Errors

Your $res variable is only defined inside the if (isset($_GET["dm"])) block. If someone loads the page without passing the dm parameter, the script tries to echo an undefined $res variable. Local PHP might have error reporting turned off, so you don’t see the issue, but servers often have stricter error settings that break the script.

Fix: Initialize $res as an empty string outside the conditional block.

4. Invalid JavaScript Syntax from Raw HTML Output

When you dump $res directly into the JavaScript string with var data = '<?php echo $res ?>';, any single quotes, newlines, or special characters in the fetched HTML will break the JS syntax. This might not happen locally if the test domain you used had clean output, but server responses could have these characters.

Fix: Use json_encode to safely pass the HTML content to JavaScript—it automatically escapes special characters.

Updated Working Script

Here’s a revised version of your script that addresses all these issues:

<form action="" method="GET">
    <input type="text" name="dm" placeholder="tx">
</form>
<?php
$res = '';
if (isset($_GET["dm"])) {
    $domain = $_GET["dm"];
    $url = "https://www.domains.lk/domainsearch/doDomainSearch?domainname=$domain";
    
    // Initialize cURL session
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Add a browser-like User-Agent
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');
    // Follow redirects if needed
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    $res = curl_exec($ch);
    curl_close($ch);
    
    echo htmlspecialchars($domain); // Sanitize output to prevent XSS
}
?>
<script type="text/javascript">
    var data = <?php echo json_encode($res); ?>;
    document.write(data);
</script>

Additional Checks

  • Make sure your server has cURL enabled (most do, but you can confirm with phpinfo()).
  • Check your server’s error logs (usually in cPanel or your hosting dashboard) for specific error messages—they’ll give you exact details if something’s still wrong.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:39:13