PHP CURL携Cookie请求论坛无响应,本地正常服务器异常求助
Hey there! Let's break down why your curl script works locally but hits a redirect loop when deployed to your virtual host—this is a common issue tied to environment differences and cookie handling.
Core Issue Analysis
The 47-Maximum (20) redirects followed error tells us that when you include the login cookies, the forum keeps redirecting your request (likely back to the login page), and curl follows those redirects until it hits the limit. This happens because the forum doesn't recognize your cookies as valid in the virtual host environment, even though they work locally.
Possible Fixes & Debug Steps
1. Proper Cookie Management with Cookie Files
Instead of hardcoding cookies via CURLOPT_COOKIE, use curl's built-in cookie jar/file system. This ensures curl handles cookie attributes like Domain and Path correctly, which many forums enforce for security. Here's a modified script:
<?php // Create a temporary cookie file $cookie_file = tempnam(sys_get_temp_dir(), 'forum_cookie'); chmod($cookie_file, 0600); // Ensure it's writable // Initialize curl $curl_connection = curl_init('http://external-forum.com/movies/movies.php'); // Set basic options curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"); // Use cookie jar/file instead of hardcoded cookie string curl_setopt($curl_connection, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookie_file); // Manually write your login cookies to the file with proper attributes file_put_contents($cookie_file, "userid=*myuserid*; pass=*mypassword*; domain=external-forum.com; path=/;\n"); // Additional settings to prevent loops and add context curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl_connection, CURLOPT_MAXREDIRS, 5); // Limit redirects to avoid infinite loops curl_setopt($curl_connection, CURLOPT_REFERER, 'http://external-forum.com/index.php'); // Add referer to mimic real browser behavior // Enable verbose logging to debug redirects curl_setopt($curl_connection, CURLOPT_VERBOSE, true); $verbose_log_stream = fopen('php://temp', 'w+'); curl_setopt($curl_connection, CURLOPT_STDERR, $verbose_log_stream); // Execute request $result = curl_exec($curl_connection); // Print verbose log to see what's happening with each redirect rewind($verbose_log_stream); $verbose_log = stream_get_contents($verbose_log_stream); echo "<pre>" . htmlspecialchars($verbose_log) . "</pre>"; // Cleanup curl_close($curl_connection); unlink($cookie_file); echo $result; ?>
2. Check Virtual Host Restrictions
Some virtual hosts restrict curl functionality:
- ModSecurity or Firewalls: Your host might have security rules that flag requests with hardcoded cookies as suspicious. Reach out to your host provider to check if any rules are blocking your request.
- Curl Version/Configuration: Verify that your virtual host's curl version supports all the options you're using (e.g.,
CURLOPT_MAXREDIRSis widely supported, but it's worth checking).
3. Verify Session Validation Checks
Forums often use additional checks beyond cookies:
- IP Locking: If the forum binds sessions to the user's IP, your local IP vs. virtual host IP will trigger a session invalidation, leading to redirects.
- User-Agent/Referer: Even though you set a User-Agent, some forums check for consistent Referer headers. The script above adds a Referer to mimic a real browser navigating from the login page.
Debugging Tip
The verbose log in the modified script will show you each redirect's response headers—look for Location headers to see where you're being sent, and Set-Cookie headers to check if the forum is rejecting your cookies and setting new ones.
内容的提问来源于stack exchange,提问作者maryland

