Solved

htmlentities not working properly on live server, only localhost

Posted on 2016-11-09
7
53 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 109

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 109

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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 109

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 83

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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