Results 1 to 9 of 9

Thread: UTF-8 error

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default UTF-8 error

    Hi,

    I have a mail form in my site that sends mails to my e-mail. The problem is when I type greek characters I get something like "αααααα". I was advised to change my php file to UTF-8 but when I just save it with UTF-8 encoding I get an error when I try to submit my form

    “Warning: Cannot modify header information – headers already sent by (output started at /***/***/webform.php:1) in /*****/****/webform.php on line 214″.

    If I switch back to ANSI everything is working like before.

    Any suggestions?

    Thank you
    Last edited by ntin0s; 07-23-2012 at 05:51 PM.

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    this doesn't sound like a problem caused by UTF-8. This problem is often caused by unintentionall blank spaces in th php. Could you please show us either your code or a URL to the wbsite?
    Last edited by bernie1227; 07-23-2012 at 04:47 AM.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. #3
    Join Date
    Mar 2011
    Posts
    2,003
    Thanks
    59
    Thanked 107 Times in 105 Posts
    Blog Entries
    4

    Default

    Quote Originally Posted by bernie1227 View Post
    this problem is sometime caused by e3w3xde55chryi69
    I'm sorry, your answer makes no sense!!!
    Last edited by keyboard; 07-23-2012 at 04:46 AM.

  4. #4
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    sorry about that keyboard, I was distracted and it must have sent before I'd finished
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  5. #5
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    This is it. Encoded in ANSI. If saved in UTF-8 that problem occurs.
    Code:
    <?php
    
    if ( !isset($_SERVER['SPI'])) {
            die();
    }
    
    if (!isset($_SERVER['DOCUMENT_ROOT'])) {
            echo("CRITICAL: we seem to be running outside of the norm.\n");
            header("Location: http://".$_SERVER["HTTP_HOST"]."/");
            die("CRITICAL: Document root unavailable.\n");
    }
    
    $request_method = $_SERVER["REQUEST_METHOD"];
    if($request_method == "GET") {
            $query_vars = $_GET;
    }
    elseif ($request_method == "POST") {
            $query_vars = $_POST;
    }
    
    reset($query_vars);
    function customsort($a,$b) {
            // $a is array for form vars, $b is comma seperated case sensitive field order
            // this is case sensitive -- good idea to hrc that.
            $data = array();
            if ( strstr($b,',') == FALSE )  {
                    $b = $b.",";
            }
            $ordering = split(',',$b);
            foreach ($ordering as $orderitem) {
                    if ( ($orderitem != null) && ($orderitem != "") ) {
                            if (isset($a[$orderitem])) {
                                    $data[$orderitem] = $a[$orderitem];
                            }
                    }
            }
            foreach ($a as $key=>$val) {
                    $data[$key] = $a[$key];
            }
            return $data;
    }
    
    function xmlentities($string) {
            return str_replace ( array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $string);
    }
    
    $t = date("U");
    
    $formhomedir = preg_replace('/.*\/home\/content/','',$_SERVER['DOCUMENT_ROOT']);
    $formhomedir = explode('/',$formhomedir);
    if (count($formhomedir) <= 4) {
            $formhome="/home/content/".$formhomedir[1]."/".$formhomedir[2]."/data/";
    }
    else {
            $formhome="/home/content/".$formhomedir[1]."/".$formhomedir[2]."/".$formhomedir[3]."/".$formhomedir[4]."/data/";
    }
    
    $file_order = ".default";
    $file_format = ".text";
    $file_interval = ".15m";
    $field_order = "";
    
    if (isset($query_vars['form_order'])) {
            if ($query_vars['form_order'] != "alpha") {
                    $field_order=$query_vars['form_order'];
                    $file_order=".custom";
                    $query_vars = customsort($query_vars,$field_order);
            }
            else {
                    switch ($query_vars['form_order']) {
                            case "alpha":
                                    uksort($query_vars,'strnatcasecmp');
                                    $file_order=".alpha";
                            break;
                            default:
                                    $file_order=".default";
                            break;
                    }
            }
    }
    
    if (isset($query_vars['form_format'])) {
            switch ($query_vars['form_format']) {
                    case "csv":
                            $file_format = ".csv";
                    break;
                    case "html":
                            $file_format = ".html";
                    break;
                    case "xml":
                            $file_format = ".xml";
                    break;
                    case "text":
                    case "default":
                    default:
                            $file_format = ".text";
                    break;
            }
    }
    
    if (isset($query_vars['form_delivery'])) {
            switch ($query_vars['form_delivery']) {
                    case "hourly":
                            $file_interval = ".60m";
                    break;
                    case "hourly_digest":
                            $file_interval = ".60mc";
                    break;
                    case "daily":
                            $file_interval = ".24h";
                    break;
                    case "daily_digest":
                            $file_interval = ".24hc";
                    break;
                    case "digest":
                            $file_interval = ".15mc";
                    break;
                    case "default":
                    default:
                            $file_interval = ".15m";
                    break;
            }
    }
    
    $file = $formhome."form_".$t.$file_order.$file_format.$file_interval;
    $fp = fopen($file,"w");
    
    reset($query_vars);
    switch ($file_format) {
            case ".csv":
                    $csvkeys = "";
                    $csvvals= "";
                    $firsttime = "";
                    while (list ($key, $val) = each ($query_vars)) {
                            if ( ($key == "form_order") ||
                                    ($key == "form_format") ||
                                    ($key == "form_delivery") ||
                                    ($key == "redirect") ) {
                            }
                            else {
                                    if ($csvkeys != "") {
                                            $firsttime=",";
                                    }
                                    $tmpkey=escapeshellcmd($key);
                                    $csvkeys = $csvkeys.$firsttime."'".$tmpkey."'";
                                    $tmpval=escapeshellcmd($val);
                                    $csvvals = $csvvals.$firsttime."'".$tmpval."'";
                            }
                    }
                    fputs($fp,"$csvkeys\n");
                    fputs($fp,"$csvvals\n");
            break;
            case ".html":
                    fputs($fp,"<table border=\"1\" cellspacing=\"1\" cellpadding=\"2\">\n");
            break;
            case ".xml":
                    fputs($fp,"<form>\n");
            break;
    }
    
    reset($query_vars);
    while (list ($key, $val) = each ($query_vars)) {
            if ($key == "redirect") {
                    $landing_page = $val;
            }
            if ( ($key == "form_order") ||
                    ($key == "form_format") ||
                    ($key == "form_delivery") ||
                    ($key == "redirect") ) {
    
            }
            else {
                    switch ($file_format) {
                            case ".html":
                                    fputs($fp,"\t<tr>\n");
                                    fputs($fp,"\t\t<td><b>$key</b></td>\n");
                                    fputs($fp,"\t\t<td>$val</td>\n");
                                    fputs($fp,"\t</tr>\n");
    
                            break;
                            case ".csv":
                                    // content is already output
                            break;
                            case ".xml":
                                    fputs($fp,"\t<field>\n");
                                    fputs($fp,"\t\t<fieldname>".xmlentities($key)."</fieldname>\n");
                                    fputs($fp,"\t\t<fieldvalue>".xmlentities($val)."</fieldvalue>\n");
                                    fputs($fp,"\t</field>\n");
                            break;
                            case ".text":
                            default:
                                    fputs($fp,$key.": ".$val."\n");
                            break;
                    }
            }
    }
    
    switch ($file_format) {
            case ".html":
                    fputs($fp,"</table>\n");
            break;
            case ".xml":
                    fputs($fp,"</form>\n");
            break;
    }
    
    
    fclose($fp);
    
    if ($landing_page != "") {
            header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
    }
    else {
            header("Location: http://".$_SERVER["HTTP_HOST"]."/*******.html");
    }
    
    
    ?>
    Last edited by ntin0s; 07-23-2012 at 05:51 PM.

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    It's not the script that needs to be encoded differently, it's your email.

    You need to send a MIME header to tell the email to be encoded in UTF-8.
    - Josh

  7. The Following User Says Thank You to JShor For This Useful Post:

    ntin0s (07-23-2012)

  8. #7
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    It's not the script that needs to be encoded differently, it's your email.

    You need to send a MIME header to tell the email to be encoded in UTF-8.
    I use the Go Daddy's web e-mail service. Do you know if I can do this because I have contacted them and they said they did all the tests but its probably an issue with my code....


  9. #8
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    I added a button in the interface which says view source and it shows the greek characters and also found this interesting line "Content-Type: text/plain; charset="iso-8859-1".

    Anyway thank you very much for your advice I will just click view source each time.

  10. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    It's much simpler than you think. Sending mail in UTF-8 just requires modification of the headers. This should work for you:

    PHP Code:

    $headers 
    "From: $from_user <$from_email>\r\n"
                   
    "MIME-Version: 1.0" "\r\n" 
                   
    "Content-type: text/html; charset=UTF-8" "\r\n"

         return 
    mail($to$subject$message$headers); 
    - Josh

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •