Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Unicode in php

  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    121
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Unicode in php

    I am newer to php and hope to be helped for 2 problems
    I have a simple formail
    Code:
    <?php
    $email = $_POST["email"];
    $to  = 'my_email@vnn.vn' . ', '; 
    $to .= $email;
    $subject = "submit form";
    $message = $_POST["the_contain"];
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    if(mail($to, $subject, $message, $headers)) 
    	{ 
    	echo "<script type='text/javascript'>\n";
    	echo "alert('...........some UNICODE here.........');\n";
    	echo "self.close();\n";
    	echo "</script>";
    	exit;
    	} 
    else{ 
    	echo "send error"; 
    	}  
    ?>
    The above simple formail applied the same for 2 hosts, in one host is OK but in another host there are 2 problems
    1. It inserts \ before ", e.g.
    <p id="_name" align="left"> is changed to <p id=\"_name\" align=\"left\">

    I do not know why one host not inserts and one host does?

    2. How to echo UNICODE message after mail OK? In case I use some js functions here, how can I declare the .js file?
    Thanks
    Last edited by docco; 07-16-2013 at 05:06 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Since you don't describe much here, 1 is *probably* from input GET or POST. Indicating that your host has magic quotes gpc enabled. Simply strip it:
    PHP Code:
    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
    {
        
    $_POST['myinput'] = stripslashes($_POST['myinput']);

    There are many ways to remove them. This step is required pre-5.4.0 if you ever use a database storage since you cannot trust that magic quotes will be disabled.

    PHP does not handle unicode natively, nor does it do it well. If you are running output in unicode, you can usually just push the necessary content type from the header: header('Content-type: text/html; charset=utf-8'); or whatever charset you need. That of course only applies if you have unusual characters where extended glyphs are used. In either case, I've never had great luck with PHP's charset handling, but rarely have a problem when I store unicode in the db and select it out. Don't know why its such a pain, but hopefully PHP 6 will fix that since I believe it's unicode native.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    121
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Since you don't describe much here, 1 is *probably* from input GET or POST. Indicating that your host has magic quotes gpc enabled. Simply strip it:
    PHP Code:
    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
    {
        
    $_POST['myinput'] = stripslashes($_POST['myinput']);

    There are many ways to remove them. This step is required pre-5.4.0 if you ever use a database storage since you cannot trust that magic quotes will be disabled.

    PHP does not handle unicode natively, nor does it do it well. If you are running output in unicode, you can usually just push the necessary content type from the header: header('Content-type: text/html; charset=utf-8'); or whatever charset you need. That of course only applies if you have unusual characters where extended glyphs are used. In either case, I've never had great luck with PHP's charset handling, but rarely have a problem when I store unicode in the db and select it out. Don't know why its such a pain, but hopefully PHP 6 will fix that since I believe it's unicode native.
    Thank you. For problem 2, I can control and it ok now
    But in problem 1, I do this test:
    - In submit form, set action point to file "simplemail.php" in host A
    Test and it works OK
    - Copy "simplemail.php" (no change) to host B, and set action of submit form point to it (in host B)
    Test and it dose wrong.

    It indicates that the problem is due to thehost, because the code is the same. Is something in host B needed to change?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yeah, if you can control it than simply disable the magic quotes gpc ini directive. Otherwise, simply strip your input as I've shown above.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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