如何在php文件中设置utf-8字符编码

如何在php文件中设置utf-8字符编码,php,encoding,utf-8,Php,Encoding,Utf 8,我不擅长php。但是我可以阅读并理解这段代码的作用 请容忍并帮助我 这是我的HTML表单 <html> <?php header("Content-type: text/html; charset=utf-8"); ?> </head> <body style="background-color:#F9F0EB;"> <form action="contact.php" method="post" accept-charset="ut

我不擅长php。但是我可以阅读并理解这段代码的作用

请容忍并帮助我

这是我的HTML表单

<html>
<?php header("Content-type: text/html; charset=utf-8"); ?>
</head>
<body style="background-color:#F9F0EB;">
    <form action="contact.php" method="post" accept-charset="utf-8">
        <p>Ваше имя:<input type="text" name="name" /></p>
        <p>E-mail:<input type="text" name="email" /></p>
        <p>Тема:<input type="text" name="subject" /></p>
        <p>Сообщение:<br />
        <textarea name="message" rows="5" cols="45"> </textarea></p>
        <p><input type="submit" value="Отправить"></p>
    </form>
</body>
</html>

аааааааааМ:

电邮:

托卡尼亚:

Сбббббб:

我的php代码如下:

<?php
/* Set e-mail recipient */
$myemail  = "tang.adil@mail.ru";


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your comments");


/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email
Comments:
$message

End of message
";

/* Send the message using mail() function */
if(mail($myemail, $subject, $message))
{
    $status = '<html style="background-color:#F9F0EB;">
                    <meta charset="utf-8"></meta>
                     <p>Спасибо за ваше сообщение!</p> 
              </html>';
}
else
{
    $status = "There was a problem sending your feedback, please try again     later.<br><br>";
}

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
<?php print $status; ?>

首先:如果需要,在任何html之前使用php头代码。我仍然不知道为什么要使用header()函数来设置html编码。如果您输入元标记,就足够了:

<?php header("Content-type: text/html; charset=utf-8"); ?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="background-color:#F9F0EB;">
    <form action="contact.php" method="post" accept-charset="utf-8">
        <p>Ваше имя:<input type="text" name="name" /></p>
        <p>E-mail:<input type="text" name="email" /></p>
        <p>Тема:<input type="text" name="subject" /></p>
        <p>Сообщение:<br />
        <textarea name="message" rows="5" cols="45"> </textarea></p>
        <p><input type="submit" value="Отправить"></p>
    </form>
</body>
</html>

此处的更多信息:

可能需要声明两次字符集。看到这个链接,我不发送也不使用任何标题。拉胡斯,谢谢!成功了。是的,对不起。我不是程序员。只是学习!
// set header
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";

// Send email
mail($to, $subject, $message, $headers);