954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unicode problem on a localhost server

I am using Wampserver for my Windows Apache-PHP-MySQL stack. The file accessed has a header that defines it as utf-8, meta tag in HTML also defines it as utf-8.

My best guess so far is that it has something to do with Apache. Searching through Google was of little help thanks to rather common keywords used. When I enter:

[protocol]://localhost/originnode/?test=õäöü

.. the browser converts the URL to ..

[protocol]://localhost/originnode/?test=%F5%E4%F6%FC

.. and same applies to $_GET string returned to the document. Now I know that a hosting service I am using has no such problem, the same file functions as expected on one of the virtualhosts I am renting. This only happens on localhost.

Any solution to this problem? Where to look?

One of the threads I found from Google mentioned the value 'AddDefaultCharset', which is not defined in my install. Language configuration does have AddCharsets (which does include utf-8) as well as languages, but that value was not defined. When I defined it myself as "AddDefaultCharset utf-8" in the end and restarted, nothing changed and the URL was still convered.

Any help would be appreciated, thanks!

Waher
Newbie Poster
5 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Take a look here: http://nl3.php.net/manual/en/ref.url.php . It is all about url's.

colweb
Posting Whiz
318 posts since Nov 2007
Reputation Points: 34
Solved Threads: 52
 
Take a look here: http://nl3.php.net/manual/en/ref.url.php . It is all about url's.

Thanks, but could you be more specific? That makes it sound like it's something I need to add to the script that is executed. However the thing is that the same code works on the installation server without requiring any additions. I suppose I need to do some additional trial&error testing then.

EDIT: I found that the URL will be correct, if it is submitted with a method="get" form, but will be converted incorrect when accessed straight from URL.

Waher
Newbie Poster
5 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

To be more specific, your installation server is probably running Linux. UTF-8 and Linux are more or less friends. Most of the time no problems. But your local system runs on Windows. And Windows can handle UTF-8 but always with a lot of problems. To solve it insert this

function url_encode($string){
        return urlencode(utf8_encode($string));
    }
   
    function url_decode($string){
        return utf8_decode(urldecode($string));
    }


in your php file(s) on the local machine and use these to get the right url. When you upload it to your installation server change the functions like so:

function url_encode($string){
        // return urlencode(utf8_encode($string));
        return $string;
    }
   
    function url_decode($string){
        // return utf8_decode(urldecode($string));
       return $string;
    }


You could also insert some code to test if the php file is executed on a Windows machine and do the encode or decode only if so.

colweb
Posting Whiz
318 posts since Nov 2007
Reputation Points: 34
Solved Threads: 52
 

Much appreciated, I feared that the difference might be because of Linux-Windows. I might have to go with an automatic detection for this, to keep the thing multi-platform.

Waher
Newbie Poster
5 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
Much appreciated, I feared that the difference might be because of Linux-Windows. I might have to go with an automatic detection for this, to keep the thing multi-platform.

You can test for a Windows environment this way:

function url_encode($string){
     if (strtoupper(substr(PHP_OS,0,3)=='WIN')) $string =  urlencode(utf8_encode($string));
     return $string;
}
   
function url_decode($string){
     if (strtoupper(substr(PHP_OS,0,3)=='WIN')) $string = utf8_decode(urldecode($string));
     return $string;
}

If it runs on Windows the string will be converted, if not it will simply return as it is.

colweb
Posting Whiz
318 posts since Nov 2007
Reputation Points: 34
Solved Threads: 52
 

Online02 will you please read the rules before posting!!

colweb
Posting Whiz
318 posts since Nov 2007
Reputation Points: 34
Solved Threads: 52
 
Online02 will you please read the rules before posting!!

Online02 has been warned. Please report spammers to keep Daniweb clean :) Thanks.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: