Yahoo主机下www域名表单提交失败问题求助(附代码)
Hey, let's break down what's likely going on here—since your form works fine on non-www and other domains, the issue is almost certainly tied to domain-specific configuration on Yahoo Hosting or session/cookie inconsistencies between www and non-www versions of your site. Here's how to diagnose and fix it:
1. Fix Session/Cookie Domain Mismatch
Captcha validation relies on server-side sessions to store the correct code, and sessions use cookies to persist across requests. If your server is setting cookies only for yourdomain.com (non-www), the www.yourdomain.com subdomain can't access those cookies. This means the server can't match the captcha you entered to the one stored in the session, so it refreshes the page and generates a new captcha.
How to fix it:
Add this code at the very top of your form processing script (before session_start()):
// Make cookies accessible to both yourdomain.com and www.yourdomain.com ini_set('session.cookie_domain', '.yourdomain.com'); session_start();
The leading . ensures the cookie is shared across all subdomains, so sessions work consistently regardless of the www prefix.
2. Check Yahoo Hosting's Redirect Rules
Yahoo Hosting might have default redirects set up (e.g., forcing www to non-www or vice versa). If these redirects use 301/302 status codes, your POST request gets converted to a GET request, which loses form data and resets the session—leading to captcha validation failure and a refresh loop.
How to fix it:
- Log into your Yahoo Hosting control panel and check the domain redirect settings. Disable any forced redirects between www and non-www, or adjust them to preserve POST requests.
- Alternatively, hardcode your form's
actionto use a consistent domain (either www or non-www) instead of relying onGetSelfScript():
// Replace the dynamic action with a fixed URL $fixed_action = 'https://yourdomain.com/' . basename($_SERVER['PHP_SELF']); ?> <form id='contactus' action='<?php echo $fixed_action; ?>' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>
3. Verify Captcha Script's Domain Restrictions
Your captcha image is loaded from show-captcha.php—if this script has logic that restricts requests to only the non-www domain, the www version will generate a captcha that doesn't match the one stored in the session. This causes validation to fail every time, triggering a refresh.
How to fix it:
Open show-captcha.php and look for any code that checks the request's domain (e.g., $_SERVER['HTTP_HOST']). Modify it to allow both yourdomain.com and www.yourdomain.com, or remove the domain restriction entirely.
4. Debug the GetSelfScript() Logic
The $formproc->GetSelfScript() method might be returning inconsistent URLs between www and non-www (e.g., picking up the wrong host from Yahoo's server configuration). This could cause the form to submit to a mismatched domain, leading to the refresh loop.
How to fix it:
Test what GetSelfScript() outputs by adding this line temporarily above your form:
echo "Form action URL: " . $formproc->GetSelfScript();
If it's showing a different domain than the one you're accessing, replace it with a hardcoded absolute URL as shown in step 2.
内容的提问来源于stack exchange,提问作者unit

