-1

I've some problems with charset encode/decode probably. Will try to describe as much detailed as I can (btw - there's custom cms with some kind GWC framework - using php), so when user registering in CMS and filling all required fields (like First name, Last name and etc.), he's inserting symbols like "ĄČĘĮŠĘŠŲŽŪĖ", in database inserting value like: "Äesnakas", but that should be inserted like "Česnakas". But that isn't everything - when administrator (on CMS) see values which are got from database - it shows correctly (Česnakas).

Tried to look how it's converting back from wrong charset to right one, didn't found anything (was looking by iconv function and etc.), also, my searching was much more harder because half of cms files was encrypted and unreadable.

In some places was inserted header with charset UTF-8 and Windows-1257, but that didn't helped me a lot.

I tried these ways to convert all characters to correct ones:

Changed in DB row collation (from latin1_swedish.. to utf8_general_ci; utf8_unicode_520 (and _ci)) - didn't helped. Exported from DB and tried to convert with excel all data in UTF-8 charset - still got these wrong characters. (Tried few ways, and few different charsets)

The main question - is it possible (if it is - how?) to convert these characters in DB? Otherwise if it's not, how to recognize how framework decoding all characters and showing right characters in case to build function which one would get all data from database - decode it (to right symbols), store it into database in right charset (like: get -> convert -> send it back where it came from -> save).

Code in registration (with insert into function):

function RegistrationAdd () {
if (($GLOBALS[fmMemberFirstName] != "") && ($GLOBALS[fmMemberLastName] != "") && ($GLOBALS[fmMemberPhone1] != "")) {
    if (CheckCaptcha ($GLOBALS[captcha])) {
        if (checkemail($GLOBALS[fmMemberMail])) {

        mysql_select_db($GLOBALS["DBName"]);

        $new_pass = GenerateKey(8);

        $strQuery = "INSERT INTO gwc_members VALUES ('','".$GLOBALS[fmMemberLogin]."','".md5($new_pass)."','".$GLOBALS[fmMemberMail]."','".$GLOBALS[fmMemberFirstName]."','".$GLOBALS[fmMemberLastName]."','".$GLOBALS[fmMemberPhone1]."','".$GLOBALS[fmMemberPhone2]."','".$GLOBALS[fmMemberAddress1]."','".$GLOBALS[fmMemberAddress2]."','".$GLOBALS[fmMemberZipCode]."','".$GLOBALS[fmMemberCity]."','".$GLOBALS[fmMemberCountry]."','".$GLOBALS[fmMemberJob]."','".$GLOBALS[fmMemberImone]."','".$GLOBALS[fmMemberKodas]."','".$GLOBALS[fmMemberPvm]."','".$GLOBALS[fmMemberAdresas1]."','".$GLOBALS[fmMemberAdresas2]."','".$GLOBALS[fmMemberTelefonas]."','4', '".$GLOBALS[fmMemberValiuta]."', '".date ('Y-m-d H:i:s')."','1','0')";


        mysql_query($strQuery);

        echo "<br>
        <table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"1\" cellpadding=\"2\">
          <tr>
            <td width=\"100%\" valign=\"top\" align=\"center\">Welcome message<br />E-mail  $GLOBALS[fmMemberMail]</td>
          </tr>
        </table>
        <META HTTP-EQUIV=\"REFRESH\" CONTENT=\"3;URL=index.php?langid=$GLOBALS[GP_langid]&stepto=memberarea&page=login\">
        ";
        } else {
            echo "<br>
            <table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"1\" cellpadding=\"2\">
              <tr>
                <td width=\"100%\" valign=\"top\" align=\"center\"><font color=\"red\">".lang_2070."</font><br /><br />
            <a href=\"javascript:history.back()\">".lang_2071."</a></td>
              </tr>
            </table>
            ";
        }
    } else {
        echo "<br>
        <table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"1\" cellpadding=\"2\">
          <tr>
            <td width=\"100%\" valign=\"top\" align=\"center\"><font color=\"red\">".lang_2080."</font><br>
            <a href=\"javascript:history.back()\">".lang_2071."</a>
            </td>
          </tr>
        </table>
        ";
    }
} else {
echo "<br>
<table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"1\" cellpadding=\"2\">
  <tr>
    <td width=\"100%\" valign=\"top\" align=\"center\"><font color=\"red\">".lang_2072."</font><br /><br />
    <a href=\"javascript:history.back()\">".lang_2071."</a>
    </td>
  </tr>
</table>
";
}
}

Page where member info can be viewed (and show's characters correctly):

function MemberView ()
{
echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">
  <tr>
    <td width=\"100%\" valign=\"top\" align=\"center\" class=\"ActionNameTitle\">[ Kliento informacija ]</td>
  </tr>
</table>
";

$strQuery = "SELECT * FROM gwc_members where member_id='".$GLOBALS[GP_memberid]."'";

mysql_select_db($GLOBALS["DBName"]);
$result = mysql_query($strQuery);

while ($rs = mysql_fetch_array ($result)) {

$usergroup = $rs["usergroup"];

echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" class=\"RecordListBox\">
  <tr>
    <td width=\"200\" valign=\"top\">Vardas, pavardė</td>
    <td width=\"*\" valign=\"top\">$rs[member_firstname] $rs[member_lastname]</td>
  </tr>
</table>
<br>
";
}
mysql_free_result ($result);
} 

Some kind name generation:

function GenerateName($count) {
mt_srand((double)microtime()*1000000);
 $key = "";
 for ($i=0; $i<$count; $i++) {
   $c = mt_rand(0,2);
   if ($c==0) {
     $key .= mt_rand(0,9);
   } elseif ($c==1) {
     $key .= mt_rand(0,9);
   } else {
     $key .= mt_rand(0,9);
   }
 }
 return $key;
}

Appreciate for reading (if you read all this long, boring story), and for answers (if somebody bother to answer at least something). Thank you!

Tom Dee
  • 1
  • 4
  • UTF-8 is the way to go but you need to use it **everywhere**. You seem to be using a very old PHP version so relying on defaults will definitively won't work. – Álvaro González Apr 04 '20 at 11:20
  • **Warning:** `mysql_*` extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the [mysqli](https://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](https://www.php.net/manual/en/book.pdo.php) extension should be used. See also the [MySQL API Overview](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API. – Dharman Apr 04 '20 at 11:42
  • Do _not_ use the deprecated `mysql_*` interface; switch to `mysqli_*` or PDO. See this for tips on what might be going wrong. In particular, note that `Č` should be hex `C48C`: https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Apr 09 '20 at 23:50

1 Answers1

0

Every character is essentially only a representation of zero and ones and how it is interpreted doesn't matter, as long your gui shows the "right" one and you can search for it.

Here are some interpretations of Č

Letter    Č 
UTF8      U+010C 
Binary    11000100:10001100    
DECIMAL   268

As long as the character set can hold the information and doesn't "loose" some information you are in the clear.

When you save the information in the database you send basically 2 Bytes, each representing a number, in it and that is saved in the database and of cours.

nbk
  • 45,398
  • 8
  • 30
  • 47