Http content negotiation

Hi guys,
this topic is sone questions I have on http content negotiation and I do not know if this is the right forum to post in , but if it is non than can an advisor or mentor or leader please move it to it’s proper forum in the sitepoint forums?

I have found a sudden interest in images in the svg file format and In reading how ie provides no support and how other browsers provide little support, I was introduced to the idea of http content negotation
here in this wikipedia article: http://en.m.wikipedia.org/wiki/Content_negotiation

I was wanting to use it to provide an svg as the background image of a div using css if that, svg, is supported and if not provide a jpg image.

I do not know what language I would write this in or what I would save it as or where I would save it,
but I was wondering if someone could be nice enough to answer these questions and or help me write an http content negotiation that does the above (provide svg when supported and otherwise provide jpg)

I would really appreictate and help with this as it seems quite interesting and worthy to learn about.

Thanks in Advance and Best regards,
Team 1504

PHP or Apache would be good solutions. However, you will have browsers that support SVG but not fully so might run into problems if the image is complex… Maybe this post should be moved, I’ll dial the Mods.

Sitepoint has an article on svg that might be useful and so does [URL=“http://www.alistapart.com/articles/using-svg-for-flexible-scalable-and-fun-backgrounds-part-i/”]listapart.

Or with raphaealjs which seems to be the simplest solution.

If you want to do the content negotiation route I’ll move this to the PHP or Apache forums.

Thank you for all your help.
I will explore the other options, but I still want to see how one can do content negotiation. In reading some information on http content negotiation byvthe W3C, I realised a language to implement thus would be php.
So could you move this to the Php / Apache forums Paul please?

Thank you,
team 1504

Done :slight_smile:

Hello,
Now that this topic is posted in the proper forum, I was wondering if someone Could help me with HTTP content negotiation.
In summary,
I want to provide an svg as the background-image of an element using css if it is supported and if not provide a JPEG/PNG and if those are not supported then provide a GIF.

I would really appreciate any help in achieving this as according to the W3C it is a good solution.

Thanks in advance & best regards,
Team 1504

When browsers make an HTTP request they send various headers.
One of these is the “Accept” header.

For example, if the browser supports PNG images it should include

image/png

in the Accept header.

The server would then send various HTTP return headers, in this case

Content-type image/png

AFAIK, it’s not “official” but the Content-type for SVG is

image/xml+svg

it is my understanding however, that many browsers do not include this in their Accept header even when they do support SVG images.

But it has been a while since I looked into this so hopefully this has changed and at the least the more modern browsers now do so.

If so you could use PHP code like

if (stristr($_SERVER["HTTP_ACCEPT"], "image/xml+svg"))

to determine if the browser is saying it does support SVG (or whatever else it is you want to test for) and serve the content accordingly.

Hmm well if I wanted to serve a background image if svg is supported and a png if not, then could I do this:


if (stristr($_SERVER["HTTP_ACCEPT"], "image/xml+svg"))
{
echo "<style type='text/css'>body{background-image:url(bgimage.svg); }</style>";
}
else{
echo "<style type='text/css'>body{background-image:url(bgimage.jpg); }</style>";
} *

or is that not valid ?

That looks good to me. I would put that in the code that generates the <head> content.

It might be more of a “future” thing that will be used more often as browsers catch up, but it’s worth a try if you have a newer browser and an older one to test it in.

I will and then I will get back to you wil the results.

Do you know if I can use http content negotiation to detect support for a certain language such as css? So if it supports css3 then do one thing, if any other version of css then do another?

Or would I have to resort to JavaScript for that :frowning:

Again, thanks for your help and Hope to hear from you soon

Regards,
Team 1504

AFAIK there aren’t HTTP headers for CSS or Javascript and the usual way is to use conditional comments to give IE different CSS and to use javascript to see if an object or property exists.

It would be nice if there were version accept headers for them though, it might even make things a lot easier.

If that happened then I would be overwhelmed with happiness.

edit: I do not know why there are asterisks/stars(*) instead of spaces in my salutation and php, it would not let me delete them, so please ignore them.

Hi,
I was reading this article, http://www.w3.org/QA/2006/02/content_negotiation.html, to see how to learn how to switch out a page’s language.
I believe, the original author was having some trouble understanding my questions and code that I put in the comments, but I was wondering if you could help me or at least check If this syntax seems proper / valid?

Page.php is the page that I have in Spanish or English. here is what page.php looks like:

<?php
include('php/choose_lang.php');
if ($chosenlang == "en") {
** * * *include 'languages/page/english.php';
}
else {
** * * *include 'languages/page/spanish.php';
}
?>

the variable chooselang comes from choose_lang.php, which is here:

<?php

// we first make sure the *language-negotiation variables we are about to use are unset
unset($chosenlang); // will ultimately hold our choice of language
unset ($lang_neg_reason); // at the end of the negotiation, this variable will store what part of the algorithm*
** * * * * * * * * * * * // *was used to find the best language variant.

// first, we check whether the client has sent us, in a cookie,
// recorded language preference from earlier interactions.
// this choice should have precedence over other mechanisms
unset($cookie_lang); *
if( $_COOKIE['lang'])
{
** * * *$cookie_lang = $_COOKIE['lang'];
** * * *if (($cookie_lang == "en") or ($cookie_lang == "es"))
** * * *{
** * * * * * * *$chosenlang = $cookie_lang;
** * * * * * * *$lang_neg_reason = "cookie";
** * * *}
}
// if the cookie has been found, then the variable $chosenlang has been filled. Our job is finished.
// if however no cookie was found and $chosenlang is still unset, we proceed with some basic negociation
// the following code parses the Accept-Language HTTP header sent by the client
// * and tries to see if either English or Spanish are accepted
if (! isset($chosenlang))
{
** * * *# Get the list of acceptable languages
** * * *# or use default
** * * *unset($acceptlang);
** * * *if ($_SERVER['HTTP_ACCEPT_LANGUAGE'])
** * * *{
** * * * * * * *$acceptlang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
** * * * * * * *for ($i = 0; $i < count($acceptlang); *$i++)
** * * * * * * *{
** * * * * * * * * * * *$Lang = explode(';', $acceptlang[$i]);
** * * * * * * * * * * *$acceptlang[$i] = trim($Lang[0]);
** * * * * * * *}
** * * *}
** * * *else $acceptlang = array('en', 'es');
** * * *for ($i = 0; $i < count($acceptlang); *$i++)
** * * *{
** * * * * * * *$Lang_split = explode('-', $acceptlang[$i]);
** * * * * * * *$Lang_pre = trim($Lang_split[0]);
** * * * * * * *if ($Lang_pre == "en" or $Lang_pre == "es")
** * * * * * * *{
** * * * * * * * * * * *$chosenlang = $Lang_pre;
** * * * * * * * * * * *$i = count($acceptlang)+1;
** * * * * * * * * * * *$lang_neg_reason = "http_nego";
** * * * * * * *}
** * * *}
}
/* At this point, we should have a $chosenlang
n some cases however (if the browser does not claim to accept either *Spanish *or English); therefore,*the "negociation" failed. We could send an HTTP error "406 Not acceptable", but we prefer the*default language to serve: in this case, english.*/
if (! isset($chosenlang))
{
** * * *# our default
** * * *$chosenlang = "en";
** * * *$lang_neg_reason = "default";
}

// we're done. Our script exits and will pass the (global) variable $chosenlang to the PHP-handled page.
?>

Here is english.php:


<?php if(! isset($chosenlang)) {setcookie("lang", "en", time()+606024*30, "/"); $chosenlang="en";} ?>
<!DOCTYPE HTML>
<HTML lang="en">
** * <head>
** * * * <meta charset="utf-8">
** * * * *<title> Page in English </title>
** * </head>
** * <body>
** * * * * <p> I am written in English </p>
** * </body>
</HTML>

And this in spanish.php


<?php if(! isset($chosenlang)) {setcookie("lang", "es", time()+606024*30, "/"); $chosenlang="es";} ?>
<!DOCTYPE HTML>
<HTML lang="es">
** * <head>
** * * * <meta charset="utf-8">
** * * * *<title> Pagina en Español</title>
** * </head>
** * <body>
** * * * * <p> se escritó en español </p>
** * </body>
</HTML>


I hope you can follow that. so what it is, is that page.php needs to have the content in the proper language. It refers to choose_lang.php to determine the language and if English , it includes the content in English and else, it includes the content in Spanish.
Lateron, I will have many more languages, but I just wanted to try these two first.

Thanks in Advance,
** * * * * * * * * * * * * *Team 1504