character encoding of the HTML document was not declared codeigniter - php

My CodeIgniter app has started to a freak error:
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.
In the first visit, the page appears fine, just a message "guimbu". If you refresh the page, the error appears until you clear the history of browser.
This happens only after second page-view. Take a look: http://guimbu.com/soon
I've just opened ALL FILES with Sublime and saved to UTF-8; twice.
I've just tried with all charset meta tags;
This happens with ALL pages, including this simple page;
My autoload.php has this:
$autoload['packages'] = array();
$autoload['libraries'] = array('session', 'database', 'firephp', 'auth');
$autoload['helper'] = array('url', 'log_helper', 'api_helper', 'string_helper');
$autoload['config'] = array('ci_opauth', 'rest', 'ci_auth');
$autoload['language'] = array();
$autoload['model'] = array();
How can I debug this problem?
UPDATE:
#sudhir, thanks for the answer, but I tried this many times. Actually the source code is just:
<!DOCTYPE html>
<head>
<title>guimbu</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
guimbu =)
</body>
</html>
And the error persists after second page-view =/
Maybe a problem with sessions?
UPDATE with possibility:
When I remove session and auth libraries from autoload, the problem doesn't happen. WTF? auth is a library I made for user authentication.

When you refresh that page you get a blank one, if you view source that page you'll see it's blank that is has a number count. My guess is that you're doing something when the page refreshes that sends a white space to the browser. I can only suggest not to close your files with ?> and let php handle how your scripts will exit/die. There's some whitespace in your code that's causing that.

try adding these in head section of your view:
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

FOUND SOLUTION
In the first lines of Session class constructor, it loads the String helper.
Yesterday I've created a helper called string_helper.php and this conflict was what crashing application.
I renamed my string_helper.php file and now everything works fine.
Thanks all!

Related

PHP seems to output HTML code incorrectly and with hidden characters

I have a php page. The code is:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<?php include("some_file.php"); ?>
<title>Calculadora dos Custos do Automóvel</title>
</head>
<body>
<p>aaa</p>
</body>
</html>
As you can see nothing is wrong.
Though, view source of Firefox gives red erros and w3 validator gives also errors.
I'm around this for two days, and it is so strange. I suppose it has to do with some strange characters php might be inputting into the HTML code but I'm not sure. Or perhaps the encoding considering that the code has Portuguese characters.
There are two BOMs just before the <title> tag. Remove them (e.g. delete everything between the second <meta> and the <title>) and everything should work fine.
From what I see here you are using Notepad++ and we already cleared that the problem is because of BOM of the php files that you include before title.
To solve this for each file that you use open in Notepad++ and from menu choose Encoding -> Encode in UTF-8 without BOM and save the file. Do this for all your files in the probject (php files).
You should not have anymore that special characters in front of title.
Also make sure you start the <?php tag at the beginning of the file (no white spaces or new lines before) and if the file contains only php you don't have to use closing ?> tag. This way you are sure that you will not have spaces at the end of the file.
As others have said BOMs are a problem when including PHP files, even if those files do not echo anything
PHP includes
At the time this article was written, if you include some external
file in a page using PHP and that file starts with a BOM, it may
create blank lines.
This is because the BOM is not stripped before inclusion into the
page, and acts like a character occupying a line of text.
You should ensure that the included files do not start with a BOM.
in: http://www.w3.org/International/questions/qa-byte-order-mark
Detecting BOMs
You can online detect BOMs within the content of your HTML page using the W3C Internationalization Checker
There is something before
<title>
remove everything before title tag
try to save your file with UTF-8 encoding
You certainly have a problem with the white space before in your tag. Please rewrite your code afresh using your replacing the spaces with spaces from your keyboard.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>Calculadora dos Custos do Automóvel</title>
</head>
<body>
<p>aaa</p>
</body>
</html>

Laravel Blade rendering <head> contents inside <body>

I need a little help here...
When I try to render a simple template in blade, I get a bad rendered html.
I have master.blade.php and home.blade.php inside views.admin directory.
<!-- master.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Test Title</title>
</head>
<body>
Content
</body>
</html>
<!-- home.blade.php -->
#extends('admin.master')
The rendered HTML when I call return view("admin.home"); looks like this:
<html lang="en">
<head></head>
<body>
<meta charset="utf-8">
<title>Test Title</title>
Content
</body></html>
Any ideas of what is happening?
Thanks.
For me, this was caused by adding a ; to end of #extends('layouts.app')
I just spent about an hour trying to solve this, before deciding to make a new laravel app to paste new files over until it was fixed. It turns out my issue was because of a random character inserted into one of my config files (probably when trying to type in a url before realising my browser wasn't the active window). So if anyone can't seem to figure out why it isn't working after fixing their blade templates, check this.
Usually the case for the "" character appearing is that it has been introduced by using a non UTF-8 encoded text editor or was introduced in a different way. My suggestion would be to use something like Sublime Text or Notepad++ to create fresh versions of each of these three documents. This usually happens because of a copy/paste without realizing it or using a text editor that has "extra formatting" that you cannot see (e.g. Word). The character is ZERO WIDTH NO-BREAK SPACE (you can see more information here). So, it's not a Laravel Blade issue, it's that extra whitespace-type characters are being introduced by whichever text editor you're using.
I know the thread is old, but I faced this issue today and removing BOM helped me to fix it. One hint - if you are using PHPStorm, there is option to remove BOM, without need to copy the file and save it with another file editor. Just right click on the file and press "Remove BOM". Works like charm!
I know this is an old question, but for the sake of someone as me that had the same problem and came up to this page, I want to share what solved my problem.
As you may find in laravel's layout documents, it is stated that a master template should be used like this:
#extends('layouts.master')
#section('title', 'Page Title')
#section('sidebar')
##parent
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop
My problem solved by adding #stop at the end of #section('content'). I hope this helps.

Dropdown is not listing French characters

I have a dropdown list, which lists things that has french characters in it. Although I see characters like this:
MySQL database is set to UTF-8. So, there is no problem in database. I've checked the database and characters are okay.
I also added these lines to my header.php but still doesn't work.
header('Content-Type: text/html; charset=utf-8');
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
What else should I do?
EDIT
I can print french letters everywhere in the site but still dropdown doesn't work.
SOLUTION
First of all, if you would like to use utf-8 in your site you should add this right after you connect to database:
mysqli_set_charset($connectionName, 'utf8mb4');
Second, you should also add this in your header tags:
<meta charset="utf-8">
That's all. After all these, my problem still wasn't fixed until I figure out my stupidity. I have header.php file, which is included on top of every page but there is another php file, which outputs dropbox options from database. So, this file don't need header.php at top. So, I had to add mysqli_set_charset($connectionName, 'utf8mb4'); specially for that page.

PHP does some things twice when this meta tag is in the file when browsed by Firefox

I am wondering why php does certain things twice, instead of once, when a certain meta tag is in the html portion of the file and the file is browsed by Firefox.
The code is like this:
<? /*...normal php code, including writing record to MySQL...*/
send('dan#example.com',$subject,$body);
?>
<!DOCTYPE html><html>
<!--PROBLEM on next line-->
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<title><?= $thisPage?></title>
<link href="<?= $cssURL?>css.freedom-and-purpose.css" rel="stylesheet" type="text/css" media="screen, projection" />
<?
include $dataPath . 'data.php';
?>
The result is TWO records written the database and TWO emails sent, whenever the page is called by FIREFOX. IE and Chrome not producing the problem.
There is a lot of other code in the program, but the reason I showed the portion above is that removing the line that starts with
<META...
solves the problem.
That meta tag is in there because one of the packages I run included it in their code sample.
So, what is that meta tag causes php to double do on DB-writes? And same thing on sending email?
Chances are this is actually a request for favicon.ico being caught by your main PHP file. Putting an empty file in favicon.ico or preventing your PHP from handling that URL should do the trick
enter code hereI would suggest you go through your code in some details and check its formatting.
Phil mentioned the meta tag about which I agree with. His suggestion of <meta charset="utf-8"> would be my preference.
Secondly the line sending the email looks odd. Single quotes aren't an option in php for data replacement, so the line send('dan#example.com','$subject','$body); would result in an email with the subject "$subject" and body just "$body".
Additionally send('dan#example.com','$subject','$body); appears to be missing a quote after $body.
I would advise you to move away from short php tags for opening and closing chunks of php <? ?> and get in the habit of <?php for clarity and to ensure the server you're using processes the code correctly.
Finally, I hope include $dataPath . 'data.php'; adds a </head> and a <body> to the html, as you're currently missing those too.

Advice with AJAX CRUD in php

I've got two sort of problems, first is that AJAX CRUD doesn't work properly and I don't know why. I've spend all weekend trying to solve that but nothing. The problem is that I can add something to the DB dynamicaly without refreshing website, but I can't delete it and edit and update without refreshing it. Next thing is that sign of my language doesn't show properly.
I've tried to add and still nothing.
Here is my website code
<?php
//demo the issue
require_once('preheader.php'); // <-- this include file MUST go first before any HTML/output
require_once('ajaxCRUD.class.php');
?>
<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-2">
<link rel="stylesheet" type="text/css" href="menugora.css" />
<link rel="stylesheet" type="text/css" href="login_success.css" />
</head>
<body>
<div id=menu>
<li> Item </li>
</div>
<?php
$arg = (int)$_GET['menu'];
switch ($arg)
{
case 1:
include('Item.php');
break;
}
?>
</body>
</html>
and here is my item.php
<html>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-2">
<link rel="stylesheet" href="format.css" type="text/css">
<body>
<?php
# this one line of code is how you implement the class
$tblCustomer = new ajaxCRUD("Towar","Towar", "Tow_id");
# don't show the primary key in the table
$tblCustomer->omitPrimaryKey();
# my db fields all have prefixes;
# display headers as reasonable titles
$tblCustomer->displayAs("Tow_nazwa", "Nazwa");
$tblCustomer->displayAs("Tow_opis", "Opis");
$tblCustomer->displayAs("Tow_producent", "Producent");
$tblCustomer->displayAs("Tow_nr_czesci", "Numer części");
$tblCustomer->displayAs("Tow_cena", "Cena");
$tblCustomer->displayAs("Tow_ilosc", "Ilość");
$tblCustomer->displayAs("Kat_id", "Numer kategorii");
# add the filter box (above the table)
$tblCustomer->addAjaxFilterBox("Tow_nazwa");
# add validation to certain fields (via jquery in validation.js)
$tblCustomer->modifyFieldWithClass("Tow_nazwa", "Nazwa produktu");
# actually show to the table
$tblCustomer->showTable();
?>
</body>
</html>
also link for ajaxcrud is here http://ajaxcrud.com/
I want to add that db charset is utf8_polish_ci. Thanks for all clues, advice and help generally.
Ajaxcrud does some dynamic requests behind the scenes to the same page url you'r viewing but might not always keep the part after ? intact. Seeing how you're handline the inclusion of item.php try some other way and check out the code of ajaxcrud. I've been working with it on large system and it has few kinks when used in this kind of way.
Also as to the encoding, there are encodings at least in the preheader.php, javascript file, your php.ini, mysql database and webpage itself. All should be set to UTF-8 for convenience.
Ok here is the trick to keep track of your filter. Ajaxcrud creates a bunch of $_SESSIONS variables so 1st load the where clause before using the class:
$_SESSION['ajaxcrud_where_clause']=$WhereClause;
You probably experienced some warning then after calling the display (showtable) add this short sequence..
$tblCustomer->showTable();
if(isset($_SESSION['ajaxcrud_where_clause']))
{
$WhereClause =$_SESSION['ajaxcrud_where_clause'];
}

Categories