本地主机与服务器运行LK域名查询脚本结果不一致问题
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

