PDA

View Full Version : Language script failing validation


garydarling
10-23-2007, 07:34 AM
Hi folks,

I have an image gallery website that has multiple language files. I was able to get the site to validate using an FF add-on, but when I use the W3C validator it breaks. Here is the validation output:

<br />
<b>Notice</b>: Undefined index: HTTP_ACCEPT_LANGUAGE in <b>/home/garydarl/public_html/photos/libraries/general.init.php</b> on line <b>59</b><br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
minishowcase v09b87
by victor zambrano
http://minishowcase.frwrd.net/
-->
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

The html before the doctype is being added by the server, I believe. When I view the source live it is not there, all looks good. Here is the PHP from the referenced file:
/* discover locale */
$locale_string = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
$default_language = "en";

/* import language file */
$set_language = $settings['set_language'];

if ($settings['auto_set_language'] && file_exists(ROOT.'languages/'.$locale_string.'.php')) {
$set_language = $locale_string;
} else if (!file_exists(ROOT.'languages/'.$set_language.'.php')) {
if (file_exists(ROOT.'languages/'.$default_language.'.php')) {
$set_language = $default_language;
} else {
$alert_message[] = "There's a problem with the language files. Please check the /languages/ folder for suitable files, or reinstall if not found.";
}
}

if (isset($_GET["lang"])) $set_language = $_GET["lang"];
$langfile = ROOT.'languages/'.$set_language.'.php';
require_once($langfile);

Any ideas what the problem might be?

Here is the live site: http://www.garydarling.net/photos/

Thanks,

Gary

Fumigator
10-23-2007, 08:45 AM
From the manual:

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI 1.1 specification, so you should be able to expect those.

Looks like your web server does not pass the HTTP_ACCEPT_LANGUAGE header info. The fact that you don't see that notice msg in normal source code is kinda wierd; is error reporting turned on? Maybe the validator somehow forces PHP notices on? Dunno about that... but if you fix the missing index problem then the whole mess should get fixed along with it.

CFMaBiSmAd
10-23-2007, 01:44 PM
If an error message is output at a point where it won't be rendered by the browser, typically inside of an incomplete form element, then it will only appear in the "view source" of the page. Is before the doctype such a case? Anyone's guess. It may be that something else on the page is preventing the browser from producing any output?

garydarling
10-23-2007, 04:27 PM
From the manual:



Looks like your web server does not pass the HTTP_ACCEPT_LANGUAGE header info. The fact that you don't see that notice msg in normal source code is kinda wierd; is error reporting turned on? Maybe the validator somehow forces PHP notices on? Dunno about that... but if you fix the missing index problem then the whole mess should get fixed along with it.

I ran the site with error reporting on, just to see if anything showed up... nothing. The PHP Environment info gives the expected return in the HTTP_ACCEPT_LANGUAGE section, so I (wrongly?) assumed that meant my Linux server running Apache 4.xx knew what to do with that request.

I then wondered if I had inserted the doctype in the wrong location of index.php. There are the usual config files and includes being called prior to the introduction of the <html> tag; I chose to put the doctype in between those calls and <html>. Seems to produce the expected output, it's just that validation issue...
<?php ## error reporting ##
error_reporting(E_ALL);

## setting: debug flag ##
$debug = false;

## setting: include header/footer ##
$include_header = true;
$include_footer = false;


#### DO NOT CHANGE FROM HERE ####################################
#### UNLESS YOU KNOW WHAT YOU'RE DOING, OF COURSE ###############

## import init file
require_once("libraries/general.bootstrap.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
## DO NOT REMOVE THIS CODE ##
include("config/version.php");
print("<!-- \n\tminishowcase $version\n\tby victor zambrano\n\thttp://minishowcase.frwrd.net/\n -->\n");
?>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title><?=$settings['gallery_title']?></title>

etc...

Hmmmm.

CFMaBiSmAd
10-23-2007, 05:01 PM
The value in $_SERVER['HTTP_ACCEPT_LANGUAGE'] is also browser specific. I just visited the url you posted using IE7 and the "view source" did not contain the error, so that would indicate that the server is providing that information through to php.

The validator would not necessarily send a header with HTTP_ACCEPT_LANGUAGE set to anything.

Your code should use an isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) and take appropriate action if it is not, such as assuming English.

garydarling
10-23-2007, 05:56 PM
Your code should use an isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) and take appropriate action if it is not, such as assuming English.
Great idea on using isset ; since the validation error is showing line 59 of the general.init.php file to be the culprit,
$locale_string = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
should I insert the isset snippet prior to line 59? I'm still learning php and the exact syntax is just over my head.

Thanks, Gary

Fumigator
10-23-2007, 06:14 PM
The manual has a good example on using isset():

http://us.php.net/manual/en/function.isset.php

garydarling
10-24-2007, 06:51 AM
I fixed it. Thanks to Fumigator for suggesting isset; in reading the blog posts at the bottom of php.net/isset it led me to try empty instead. Here is the resulting code that validates:
/* discover locale */
if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { //new
$set_language = "en"; //new
} else //new
{ //new
$locale_string = (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
$default_language = "en";

/* import language file */
$set_language = $settings['set_language'];
if ($settings['auto_set_language'] && file_exists(ROOT.'languages/'.$locale_string.'.php')) {
$set_language = $locale_string;
} else if (!file_exists(ROOT.'languages/'.$set_language.'.php')) {
if (file_exists(ROOT.'languages/'.$default_language.'.php')) {
$set_language = $default_language;
} else {
$alert_message[] = "<p>There's a problem with the language files. Please check the /languages/ folder for suitable files, or reinstall if not found.</p>";
}
}
} //new closing bracket for the "discover locale" if/else statement


I also moved the doctype to the beginning of index.php, that way any error statements at least appear under the <html> tag. Not perfect, but getting closer to the <body> tag.

Gary