All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

And Breathe Out

30 June 2004

PHP has been giving me the run-around. Just when you think you’ve got it all sussed out it springs another surprise. It likes to play with me that way – show me who’s boss.

This afternoon Nathan and I launched a site we have been working on for months. More accurately, we launched a site based on the content management system we’ve been working on for months. She flies! Hurrah and all that. Up until the site going live today I’d been validating pages using one or other of two methods. The first thing I tried was installing the W3C validator on my dev server. That almost worked, but no matter what input I gave it seemed to be resolving paths to the wrong site. I don’t think it liked by VirtualHosts set up. Being too pressed for time to really fiddle with it, I resorted to saving the output of a view source to disc and uploading the file to the W3C service. This worked fine.

On enlivening the site this afternoon, I took the opportunity to run the validator across the site for real. You can guess what happened. Errors galore – and all down to PHP’s automatic handling of sessions. For those not familiar with its behaviour, PHP uses cookies to store a session id on the client in the usual way. However, when cookies aren’t enabled PHP steps in and saves your bacon by automatically rewriting all the links on a page to append the session id to the query string. I’ve written similar functionality myself for ASP systems and it’s no small task, so having this feature built in is a real bonus. It’s just a shame it doesn’t encode its ampersands when it does it.

If you’ve not got access to the php.ini, whack this in your page:

ini_set("arg_separator.output", "&amp;");

So we’re all cool now, right? Wrong – to maintain the session through HTTP POSTs, PHP also jimmies a hidden input into any forms, right after the opening form tag. This is just fine and dandy, but for the fact that XHTML 1.0 Strict requires that all input elements (including those which are not visually represented) are contained inside a block-level element.

PHP handles this by … invalidating your page. Thanks guys. I see this bug was opening in September 2001. I got the work-around from the notes on that bug, but it’s not pretty. The first thing to do, again if you don’t have access to the php.ini, is to place this on your page:

ini_set("url_rewriter.tags", "a=href,area=href,frame=src,input=src");

That’s basically just reconfiguring the URL rewriter to not attempt to fix up forms – no hidden field will be written. That means, however, that you’ve got to write the field yourself.

<input type="hidden" name="PHPSESSID" value="<?php echo session_id(); ?>" />

The above assumes you’re using the default session name of PHPSESSID, but you know the drill if you’re not. So, w00t, it validates. Crisis averted.

Oh, and the site? Introducing grownupgreen – a UK based site with news and features on environmental issues for the environmentally aware. It’s a hard working, real world XHTML and CSS site with a graphical feel. We had to be a little liberal with extra, otherwise unnecessary markup to hook the CSS to, but that’s what the design required. I think we took a good route for what we needed to achieve. Designed by Chris Ruffles and implemented by Nathan and myself. Nathan and I designed the CMS together, then he took on markup and CSS duties and started at the front end, whilst I got dirty with the database and started at the back. We met somewhere around the middle.

So there you have it.

- Drew McLellan

Comments

  1. § chris: just wanted to give you the heads up – your graphics don’t extend all the way for 1600×1200 resolution. otherwise, great job!
  2. § Drew McLellan: If you run your browser maximized at 1600×1200 then I reckon you deserve everything you get ;)

    But thanks for the headsup .. I’ll nag Nathan.
  3. § Nathan Pitman: Oh, go on then, nag away. :)
  4. § Hans: Wow. Who knew big resolutions were bad? I’ve also got a big bunch of pixels—1680×1050. Usually the ugliness goes away when I also have my RSS Reader Panel sidebar open in Firefox, but this time it still don’t look pretty. I should start a campaign for Big Res Friendliness.

    Good job on the site, other than said issue. Green seems to be prettier than blue on an LCD.
  5. § Krzysztof: Great article! I just figured it out: if you don’t have access to php.ini file, you can add that code to your .htaccess file:

    <IfModule mod_php4.c>
    php_value url_rewriter.tags "a=href,area=href,frame=src,input=src"
    </IfModule>
  6. § Matt: I fixed the form thing breaking XHTML 1.1 thing by running a regular expression over the page that moved the hidden input inside the inside block element. I can send you the code if you like. P.S. Fix your taborder on this form!
  7. § Anne: Nice someone documentated this. Thanks.
  8. § Jonas: I can imagine there went some work into that site. Very nice indeed!
  9. § Mike P.: Nice workaround for the sessid in forms. We’ve had the same trouble (of course). We usually avoid giving sessions to bots (as googlebot doesn’t like sessid’s) and so we do the same for the validator. It validates, but a client could wind up with invalid code, though they’d never no unless they hand checked ;-]
  10. § Terrence Wood: add “fieldset=fakeentry” to your rewriter tags and you won’t need to add the SID input yourself.
  11. § Suzanne Phillips: Hey, have any of you actually joined Grownupgreen? Your support would be very greatly appreciated!
  12. § Martin: Thanks for that work around about the session id. Its been driving me crazy that my page wasn’t 100% strict.
  13. § Andy Yeates: Thanks mate, been ripping my hair out trying to get my forms to validate.

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.