Posting from a UTF-8 site to a ISO-8859-1 site (PHP)
-
29-09-2019 - |
Question
Concerning the following situation:
www.foo.com is a page delivered in UTF-8. It features a login form to www.bar.com/Servlet. www.bar.com is a ISO-8859-1 capable application.
I have the following problem:
whenever someone is using special chars like German umlauts for his password the login fails because the password is posted in UTF-8, but the application expects it to be in ISO-8859-1.
Of course I could add accept-charset="ISO-8859-1"
attribute to the html-form, but this (of course) does not work in IE.
I now thought of a proxy script, that is converting the UTF8 to ISO-8859-1 and posting that data to www.bar.com/Servlet. The conversion does work, also the posting (done by CURL). But as I'm echoing the returned html of www.bar.com/Servlet directly (and there is still www.foo.com in the address bar) the relative links in the html don't work. I managed to add a base tag to the output with an ugly replacement and it seems to work, but it's not a very elegant solution.
Is there a more simple way to post UTF-8-data to a ISO-8859-1 application?
My CURL code looks like this:
define('POSTURL', 'https://www.bar.com/Servlet');
array_map('utf8_decode', $_POST);
$ch = curl_init(POSTURL);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$response = curl_exec($ch);
curl_close ($ch);
$response = preg_replace('/(<head(.*)>)/U', "$1<base href=\"https://www.bar.com\" />", $response);
echo $response;
exit;
Solution
I did not find a simple working solution. My solution is now to move the UTF-8-form that gets posted into an iframe on the same page and setting the charset to ISO-8859-1 within this iframe. The iframe-form posts to target="_top" then.