Pig Work

Pig Work skip to main content

Weblog of Freelance Designer Steven Clark


PHP’s Location and Refresh

Filed under: — 11:06 am

Programming with PHP to create dynamic pages is a necessary skill for any web developer and I’ve long believed the day of the HTML static page is numbered in the very small zeros. People want dynamic websites that actually allow them to fulfil functions and not the plain ad-hoc pages put up by rote as budget solutions. The designer should be identifying stuff users will want to do on the site, assessing how transactions and interactions can be streamlined and made more usable and interesting for them. Of course there’s other technologies like ASP for developing dynamic content too but for this article I’ll stick to mentioning PHP.

One of the most important things to understand is the HTTP protocol even if its only on a basic level. Its a stateless protocol meaning it has no memory once it leaves the page, and HTTP works by sending requests and responses to and from the server for content. These HTTP transactions therefore contain request and response headers. PHP scripts can send HTTP response headers using the header() function.

It should be noted at this point that header must be called before any other output is sent to the browser or you’ll get a world full of kick arse error in your face. That’s strictly not a DTD or an invisible character or any output whatsoever and its one of the major forum questions for new PHP developers wanting to know why their page isn’t working.

There are a number of headers you can send but the one’s I’m concerned with today are quite useful to note. The first is the location header which allows you to redirect the page in your programming conditionals, such as if the user does this action send them to that page but if they do another then send them to the alternate page. Or you can simply use the location header to redirect users as a control mechanism. The format for this is in the following example:

header("location: pagetogoto.php");

Similarly the refresh header forces the browser to reload a new page after a set interval of time. The format for this is in the following example:

header("refresh: 5; url=pagetogoto.php");

Its really pretty simple stuff but also quite useful, just remember its kind of pointless putting HTML on a page with the location header but kind of useful with the refresh header. I tend to use the refresh header to create a graceful thankyou message after contact form submissions, for example. And the location header is handy to peel off different pages to successful submissions and failed submissions, each of which redirect the user to separate destinations - success to the home page and failure back to the contact form.


Some Beginner Coding Tips

Filed under: — 11:01 am

One of the first tips I can give is if you’re doing CSS layouts then either put borders or different background colours on your containers so you can see what’s pushing where because its not always obvious. Its simple and I’ve been called a ‘Duh’ but 2 seconds of putting it in can make you slap your forehead.

Document your work either internally with comments, externally or both. This will save you so much trouble when you realise last week’s pages are falling over and the form has scroll bars (don’t laugh that was me yesterday).

Get your hands dirty on the coal-face and don’t rely on WYSIWYG editors to do it all. You need to understand what the code means and how it reacts to different browsers. Try HTML Kit which is what I use or something similar. If you’re interested in Java try Eclipse.

Note that no matter what or who you do anything for you have to keep backups or previous versions if you don’t want to eventually have to go back and fix the unfixable.

If you cut and paste code (particularly with PHP in my case) either put it underneath and rewrite it by hand or paste it onto MS Notepad first and then onto your page to get rid of the invisible characters. Many a PHP line fails simply because of this.

Another PHP tip is never leave spaces before your PHP opening tag. Often simply backspacing the tag to the beginning of a line gets code working.

Do one thing at a time and then check it in several browsers. This sounds like its going to take more time than just hacking it all better at the Fantastic Code Hospital but in the long run it nearly always works out quicker to test after each change of your code.

Go to forums, good ones like Webdeveloper.com, PHP Freaks or Codewalkers. Active forums mean more likelihood of responses to your questions faster.

Use DOCTYPES and the correct validators to check your (X)HTML and CSS. Get the relevant specs for your code and keep them handy. A short list (for example of simple XHTML no-no’s) pasted to your wall will save you a lot of time.

Understand separation of content from display from behaviour (XHTML, CSS and Unobtrusive JavaScript respectively). Try not to use inline styles for example as this mixes the display within the content.

I guess that’s all I can fit in here off the top of my head while avoiding my current email issues - yet again. If you’re new to coding of any sort maybe these short tips could help you save an hour or so of work later on. Write good clean code, comment, test and be sure to backup your work. Finally, ask questions and be annoying until you get the right answers!


Never Did Hear from ALA

Filed under: — 12:29 pm

Admittedly I’m just a poor bugger from the penguin infested legume growing from Australia’s buttock but I’d kind of hoped a trickle of my question regarding A List Apart’s article on Dynamic Text Replacement with PHP written by Stewart Rosenberger would make it back to them and they’d eventually know - problemo - there is an issue for quite a few people with getting it to work.

So I’m kind of yell-beg-hairpulling it into the blogosphere as an open question still?

Why does IE show the alts but only red X’s and why does FF display the words in the original font and bunched together without spaces between them? I figure there has to be a logical technical fixable answer to that one that maybe someone’s discovered since it was posted.

My frustration comes from having read the entire 28 pages of comments a number of times and having no way to contribute to that closed thread. So if you know Stewart’s number or have your best mate at ALA you can tell them I think they’re legends but I’m absolutely out of my depth here. Is it my server, as I’d suspect? If so, what needs to be done to fix this one identifiable problem…

Ball of knowledge thrown into the blogosphere to be caught (hopefully) by someone who knows how to fill it and send it back to me. If you do you’re a legend because it’d be sooo bloody handy.


RIP Out Those Mailto Links

Filed under: — 12:50 pm

When I look at a site nowdays one of the first things I seek out is their mail functionality - do they use the HTML mailto in a simple link to make a default popup email client do their hard work? If they do I tend to be a bit biased and unfairly think stuff like shoddy, ineffective and not enough money put into effectively creating the site functionality. In other words mailto is and always will be the easy way out particularly for those who aren’t confident programming.

Why is mailto a bad thing then when it’s legitimate HTML? For one I’d suggest there are better and more elegant ways to provide the same functionality without making something popup and steal focus from the user. But really my greatest concern is it doesn’t work effectively for a rather large number of users who haven’t configured their Outlook Express or whatever email client properly. So they fill in the popup form and press submit - then it says it couldn’t send the message! Now being that user would you go to your hotmail and zap up another email? I doubt it. Finally I’m not really too keen on sitting client email addresses all over the site for bots to pick up and create havoc with either.

So lets go server side for the answer. By using the PHP email functions you can process the contact in much broader ways and without the negatives involved with HTML mailto links. Do this simple tutorial and you won’t look back. Lets consign mailto to the grave of ‘no longer working as expected’. And if you decide to keep mailto’s on your site ask this - how many users can’t use your contact form and don’t know how to configure their email client? Its up to you as a developer to assess the level of accessibility you want to provide. And if you just must use mailto then think about keeping them as supplemental links and provide a PHP main contact as well.


URL Encoding and ASCII

Filed under: — 10:30 pm

If you ever have the validators pass you back that nasty XML error page then this article might be a bit of a clue in debugging. Say you are using url encoding, that’s where you are using PHP and passing a variable to another page simply by putting in your anchor link’s href attribute href="?name=value” for example. What this does is put the ?name=value at the end of the url when it calls the next page. Its pretty basic. Now look again. Say for some mad and sad reason you decide to pass href="?name=Sally Brown” what do you think happens? Well it’ll put that “%20″ character into the url to represent a space character. Of course, you could also put the ASCII character for a space into your url encoding to be geeky.

So that could just be why your page fails the validator and returns an XML error - if you wrote “Sally Brown” as a value in a url encoded link? The values will pass correctly and PHP doesn’t give a damn but it can actually trip the validator so keep an eye out.

There are a number of other reasons for this error page but this example will give you an idea of the stuff to look for in your XHTML page if it does.



Filed under: — 6:39 am

I’ve blogged about this one before but with all of the issues that you go through just for a personal development machine the way to go is WAMP5. Double click and it installs PHP, MySQL, PHPMyAdmin, MySQLiteManager and the Apache Server. No trolling the directories for days trying to figure out what setting needs adjustment, its out of the box and away.

Why mention WAMP? There are others too of course. Its just so many times people are turned off installing a local development server cos its too esoteric. This makes it damn easier for sure.