This question already has answers here:
The character encoding of the HTML document was not declared
(6 answers)
Closed 9 years ago.
When I visit my page I get a blank display and the following message:
"The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol."
Here's the relevant code:
<?php
$errors = array();
$missing = array();
//check if the form has been submitted
if (isset($_POST['send']))
{
//email processing script
$to = '$_POST['email']';
$subject = 'Your Quote';
$expected = array('email',);
$required = array('email');
$headers .= 'Content-Type: text/plain; charset=utf-8';
require('./includes/processmail.inc.php');
if ($mailSent) {
header('Location: http://www.dailyspiro.com/email.php');
exit;
}
}
?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Use following PHP header at the top of the PHP Code
header('Content-type: text/html; charset=utf-8');
ie.
<?php
header('Content-type: text/html; charset=utf-8');
$errors = array();
$missing = array();
//check if the form has been submitted
if (isset($_POST['send']))
{
//email processing script
$to = '$_POST['email']';
Instead of using a meta tag to declare encoding, try using a standard doctype like
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Related
I've written text in Finnish language in html and it worked fine. but when i'd placed all html in php then it didn't worked.
here is my code
<?php
$html='<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!--> <html lang="fi" xml:lang="fi" xmlns="http://www.w3.org/1999/xhtml"> <!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
<meta charset="UTF-8" />
<title>Login Page</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<meta content="" name="description" />
<meta content="" name="author" />
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body >';
$html=$html."<h4 style='font-family: Calibri, sans-serif;'><span lang='fi'>pitää pääosassa</span><h4>";
$html=$html."</body></html>";
echo $html;
?>
I solved it by just adding all text in utf8_encode() function like that
<?php
$html="<h4 style='font-family: Calibri, sans-serif;'><span lang='fi'>pitää pääosassa</span><h4>";
$html=utf8_encode($html);
echo $html;
?>
I have a full HTML page:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>
I'm trying to save it in a variable like so:
$template = htmlentities("<!DOCTYPE HTML><html lang="en-US">...", ENT_HTML5, "UTF-8" );
.. but it chokes at just the first HTML tag.
That's because the first HTML tag has double quotes, just like you use for delimiting your string literal.
$template = <<<EOD
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>
EOD;
You are not escaping the string propertly Try to:
Replace
htmlentities("//whatever your html code is//");
with
htmlentities('//whatever your html code is//');
user addslashes function..it will not truncate your string in between.
This function can be used to prepare a string for storage in a database and database queries.
Before storing into database or for any purpose
$final_string = addslashes('<!DOCTYPE HTML>
..........');
Before rendering that output on browser
$normal_string = stripslashes($database_retrived_string);
$data = '<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>';
base64_encode($data);
Try this:
$temp = addslashes('<!DOCTYPE HTML><html lang="en-US">...', ENT_HTML5, "UTF-8" );
$template = htmlentities($temp);
I have two PHP files, both of them are contact forms. One file has HTML 4 doctype and the other has HTLM 5 doctype.
When I open the first file it renders properly i.e. completely loads web page with full colors, graphics etc. but when I render the second file it just displays the HTML code in the browser in the text format.
the content of the first file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Discover Brazil</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="layout.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/cufon-yui.js" type="text/javascript"></script>
<script src="js/cufon-replace.js" type="text/javascript"></script>
<script src="js/Myriad_Pro_400.font.js" type="text/javascript"></script>
<script src="js/Myriad_Pro_600.font.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#comp').focus();
});
</script>
<style type="text/css">
</head>
<body id="page1">
<?php
if(isset($_POST['sent'])){
// Contact subject
$comp= $_POST['comp'];
$ur_name = $_POST['ur_name'];
$position = $_POST['position'];
$add = $_POST['add'];
$city = $_POST['city'];
$pin =$_POST['pin'];
$state = $_POST['state'];
$tel =$_POST['tel'];
$Cell =$_POST['cell'];
$fax = $_POST['fax'];
$email =$_POST['email'];
$website = $_POST['website'];
$pro = $_POST['pro'];
$busi= $_POST['busi'];
$target = $_POST['target'];
// Details
$message="Name of the Company :- " . trim($_POST['comp']) .
"\n\nName :- ". trim($_POST['ur_name']) .
"\n\nPosition :- ". trim($_POST['position']) .
"\n\nAddress :- ". stripslashes(trim($_POST['add'])) .
"\n\nCity :- ". trim($_POST['city']) .
"\n\nPin Code :- ". trim($_POST['pin']) .
"\n\nState :- ". trim($_POST['state']) .
"\n\nTelephone No :- ". trim($_POST['tel']) .
"\n\nCell No :- ". trim($_POST['cell']) .
"\n\nFax No :- ". trim($_POST['fax']) .
"\n\nEmail ID :- ". trim($_POST['email']) .
"\n\nWebsite :- ". trim($_POST['website']) .
"\n\nCompany Profile :- ". trim($_POST['pro']) .
"\n\nBusiness With Brazil for Last 3 years :- ". trim($_POST['busi']) .
"\n\nTarget for Brazil for 2014 :- ". trim($_POST['target']);
$to = "email#domain.com";
$subject = "This is subject";
$header = "From:$email \r\n";
$sendmail = mail ($to,$subject,$message,$header);
}
?>
<!-- header -->
THe content of second file
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sl</title>
<meta name="description" content="Place your description here">
<meta name="keywords" content="put, your, keyword, here">
<meta name="author" content="Templates.com - website templates provider">
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript" src="js/maxheight.js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_300.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_400.font.js"></script>
<script type="text/javascript" src="js/jquery.faded.js"></script>
<script type="text/javascript" src="js/jquery.jqtransform.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript">
$(function(){
$("#faded").faded({
speed: 200,
crossfade: true,
autoplay: 3000,
autopagination:false
});
$('#domain-form').jqTransform({imgPath:'jqtransformplugin/img/'});
});
</script>
<!--[if lt IE 7]>
<script type="text/javascript" src="http://info.template- help.com/files/ie6_warning/ie6_script_other.js"></script>
<![endif]-->
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5.js"></script>
<![endif]-->
<style type="text/css">
<!--
.style1 {font-size: 16px}
-->
</style>
</head>
<body id="page1" onLoad="new ElementMaxHeight();">
<?php
if(isset($_POST['sent'])){
// Contact subject
$author= $_POST['author'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$details = stripslashes($_POST['details']);
// Details
$message="Name :- " . trim($_POST['name']) .
"\n\nEmail :- ". trim($_POST['email']) .
"\n\nSubject :- ". trim($_POST['subject']) .
"\n\nMessage :- ". stripslashes(trim($_POST['details']));
$to = "email#domain.com";
$subject = "This is subject";
$header = "From:$email \r\n";
$sendmail = mail ($to,$subject,$message,$header);
}
?>
<div class="tail-top">
Make sure the extension of the second file is a recognized document extension.
So if its, secondFile.txt or secondFile.php.bk , rename it to secondFile.php
edit: just a small guess, but have you tried closing the body and html tags with:
</body>
</html>
at the bottom?
Your problem definition is not very clear, your question/description could also describe several different problems.. So chances am naming potential causes that you already are aware of, but for completeness sake:
First of all, to see a HTML file as a rendered HTML page, you need to open it in a browser. This works local (from your own disk/computer) and from websites (via http connection)
Second, to have PHP code actually run you need to have some computer execute it before sending it to you. This usually happens on the computer that accepted your http connection, if it recognizes and can execute your *.PHP file.
Then there are things that can also complicate stuff, like the content-type in the header that PHP can alter before returning a webpage.
Update: Thanks for attaching source.
The reason a browser sometimes displays text instead of html, is related to the content-type. Mostly this can be derived by file extention, the doctype or other html properties.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
It could help to add this to your 2nd file, or it may be even better to properly fix the file so that the browser can correctly guess that its html to begin with. (unroot the problem at its cause)
I have this code on my header.php. Its for creating title on the fly. The problem is that it only creates a title when user is on the article.php(page for reading the article) but produce a 'undefined variable i' error on other pages. What should I change or who know a better code for the title of web page? i is id.
if(isset($i=$_GET['i']) && $i = true ) {
$i=$_GET['i'];
$tbl_name="yote";
$sql=mysql_query("SELECT * FROM $tbl_name WHERE i='$i'") or die(mysql_error());
while($rows=mysql_fetch_array($sql)){
$title = $rows['title']. "";
}
mysql_close();
} else {
$title = 'Malloooo - News, Reviews, Analysis and Market Watch From Around The World';
}
?>
<!-- Header
================================================== -->
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
<!-- Basic Page Needs
================================================== -->
<meta charset="utf-8">
<title><?php echo $title; ?></title>
Honestly, there's quite a bit wrong with your code.
if (isset($i=$_GET['i']) ... : Not sure what you're trying to do with that, but it's not valid.
... && $i = true) : This is setting a value, not checking it.
You're horribly vulnerable to SQL injection. Look into PDO: http://php.net/PDO
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
EDIT** I have now fixed the headers already sent up to the point where, line 53 ie. Last line in my common.php, which starts the session, is causing headers already sent. So now where am I supposed to put my session_start?
My index.php -
<?php
require("common.php");
if(empty($_SESSION['user']))
{
header("Location: login.php");
die("Redirecting to login.php");
}
?>
<!DOCTYPE html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8" />
<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width" />
<title>nCMS | Simplicity, Reimagined.</title>
<!-- Included CSS Files (Uncompressed) -->
<!--
<link rel="stylesheet" href="stylesheets/foundation.css">
-->
<!-- Included CSS Files (Compressed) -->
<link rel="stylesheet" href="stylesheets/foundation.min.css">
<link rel="stylesheet" href="stylesheets/app.css">
<script src="javascripts/modernizr.foundation.js"></script>
<!-- IE Fix for HTML5 Tags -->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<!--INCLUDE HEADER -->
<?PHP include('header.php'); ?>
<!--/INCLUDE HEADER -->
<!--INCLUDE PAGE -->
<div class="row">
Hello <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>, secret content!<br />
<h3>Hi, I'm Index!</h3>
</div>
<!--/INCLUDE PAGE-->
<!--INCLUDE FOOTER-->
<?PHP include('footer.php'); ?>
<!--/INCLUDE FOOTER-->
<!-- Included JS Files (Compressed) -->
<script src="javascripts/jquery.js"></script>
<script src="javascripts/foundation.min.js"></script>
<!-- Initialize JS Plugins -->
<script src="javascripts/app.js"></script>
</body>
</html>
This is the live page - http://www.cogameservers.com/ncms
Thank you for any help - Necro
common.php -
<?php
$username = "";
$password = "";
$host = "";
$dbname = "";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
header('Content-Type: text/html; charset=utf-8');
session_start();
I know that you resolved your issue from my comments.but here it is as answer.
As you asked where you put session_start to remove header already sent error.
As general practice, put session_start() at the starting of your php file. also need to clear previous bufffer.
ob_start(); // this will clear output buffer
session_start();
It appears the error is caused by header("Location: login.php"); - the error says common.php is sending headers already on line 77. It will keep doing this as long as common.php is sending the headers.
Warning: Cannot modify header information - headers already sent by (output started at /home/megatron/public_html/ncms/index.php:9) in /home/megatron/public_html/ncms/common.php on line 77
Without knowing the contents of that file I can't help you. I'd suggest just looking through that file for something sending some headers and see if it necessary or approach this problem differently.