Solved

htmlentities not working properly on live server, only localhost

Posted on 2016-11-09
7
40 Views
Last Modified: 2016-11-10
For some reason things that work on my localhost are not working on a live server. For instance:

$prod_name = htmlentities($row['prod_name'], ENT_QUOTES, "UTF-8");

Open in new window


works on my localhost but on the live server the title is showing funny characters like this � )

Does anybody have a clue why this might be?
0
Comment
Question by:Black Sulfur
  • 3
  • 3
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41881019
Your character encoding has to be 100% consistent across all parts of the platform.  Check your HTML and PHP declarations and make sure they match.
https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html
0
 

Author Comment

by:Black Sulfur
ID: 41881089
A very interesting read, thanks!

Funny enough, I found the problem was actually caused by minifying my page. I used the GTmetrix minifier and that caused the problem. So, since I can't use that one, I don't suppose you know of a good one that minifies html and PHP. I suspect that is what broke it, the fact that I have PHP in there as well.

I tried this one:

https://htmlcompressor.com/compressor/

but I still get notified that I need to minify my HTML so obviously it isn't good enough.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41881165
I'm never sure of the value you get from minifying PHP or HTML.  I can see minifying JavaScript, especially for big libraries like jQuery, and maybe CSS.  But the debugging problems you get from minification of PHP and HTML are just not worth the savings.  And in related matters, if the minifier changes something as basic as your character set encoding, you are doing the right thing to discard it completely.

Where / how do you get the notification that you need to minify your HTML?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Black Sulfur
ID: 41881184
yeah, it isn't something I particularly want to do but I am trying to get a good score. I have managed to minify it without breaking it but Google and GTmetrix still say it isn't minified enough.

https://developers.google.com/speed/pagespeed/insights/

It says:

Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up download and parse times.
Minify HTML for the following resources to reduce their size by 811B (14% reduction).

Minifying http://mysite.com could save 811B (14% reduction) after compression.
0
 

Author Comment

by:Black Sulfur
ID: 41881187
P.S. I even minified my minify and I still get the message.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41881293
811B
Really?  You could transfer that a million times and not use as much time as we've spent on this question!  This is one of those things like milking a mouse -- no matter how much effort you put into the process, you just don't get much out.  I would recommend that you move on to something more important :-)  Check Peter Norvig's article (look for "Answers") and you can see where the center-of-gravity rests in some computer applications.  TL;DR there are a lot of things not worth optimizing!
http://norvig.com/21-days.html

FWIW, here is my hastily created example showing the places you probably want to use UTF-8.  
<?php // demo/utf8_example.php
/**
 * Making things work with UTF-8
 *
 * http://php.net/manual/en/function.mb-internal-encoding.php
 * http://php.net/manual/en/function.mb-regex-encoding.php
 * http://php.net/manual/en/function.mb-http-output.php#114843

 * http://php.net/manual/en/function.mysql-set-charset.php
 * http://php.net/manual/en/mysqli.set-charset.php
 * http://php.net/manual/en/ref.pdo-mysql.connection.php
 *
 * http://www.w3schools.com/TAgs/att_meta_charset.asp
 *
 * https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html
 * https://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html
 */
error_reporting(E_ALL);


// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
mb_http_output('UTF-8');


// MAKE SURE THAT MySQL WORKS WITH UTF-8 (DEPRECATED)
mysql_set_charset('utf8mb4');

// MAKE SURE THAT MySQLi WORKS WITH UTF-8
$mysqli = new mysqli('localhost', 'username', 'password', 'test');
$mysqli->set_charset("utf8mb4");

// MAKE SURE THAT PDO WORKS WITH UTF-8 (RELEASE-DEPENDENT)
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'username', 'password');


// END OF PHP, LOOK AT HTML
?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>

<!-- MAKE SURE THAT HTML WORKS WITH UTF-8 -->
<meta charset="utf-8" />

</head>
<!-- etc etc ... -->

Open in new window

1
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41882980
We've gotten a lot of questions about Google Pagespeed.  Some of their suggestions are worth following, some are not, and some are doable only when you own your own servers and wrote your own server code.  I rarely if ever minify any code.  

On sites where a lot of images are downloaded, I may enable 'gzip' compression.  That is a negotiation between the browser and the server.  Other than enabling it, it is not something that you do.  The server estimates whether it will take longer to compress it than to just send it.  A file with only 5800 bytes is not likely to be compressed.

Another thing I almost never do is set expiration dates for image, CSS, and JavaScript files.  My customers change those files way too often for that to be a good idea.
1

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now