Saturday, January 28, 2012

The Wonder of PHP

One of the sites that I manage is for the Civil Engineering Student Advisory Council, CESAC. I didn't design the site, but I have done improvements over the last couple years. None recently though. I lost motivation to do more, because A, I wasn't getting paid, and B, it seemed like no one was using the site anyway.

Fast forward to several days ago. I was showing one of the new CESAC members the site, since he'll be taking over when I leave in a year. And to put it frankly, he embarrassed me into starting work on it again. So I pulled out my list of things that needed work, and with a couple things that he added, began in on it yesterday and today.

One big thing was the footer. Every page has a footer, with copyright info, and a Page Last Update timestamp. But when the site was designed, the designer decided that it would be a good idea to have future webmasters scroll to the bottom of the webpage every time it was updated, and manually write in the time and date.

When one has to update multiple pages, that gets quite tedious. So first I created an iframe with the footer in it. I then opened every page, deleted the footer, and placed in it the iframe. Took a while, but simple enough. However, that left me with a problem. Unless I wanted it to be a Site Last Updated timestamp instead of page specific, it wouldn't work.

So I pulled out my PHP knowledge. I figured, there has to be a way that PHP can check the date that the current page was last updated. All files, html included, have Date Created and Date Modified information attached. And I was right, PHP can do that. Enter the second problem. I wanted the full page that users were viewing, not the iframe. So the PHP had to find the date modified of the parent document. Not possible.

I started looking for ways around, and finally checked what the referrer is on an iframe. Lo and behold, it's the parent document, every time. So all I had to do was put it together. PHP grabs the full URL of the parent page. Then it cuts off the top part down to just that file name, as well as any folder it's in, then grabs the date modified, and displays it. It won't have to ever be touched again, it's accurate for every page, and if I need to change it, I change one file, and it's fixed for the entire website.

PHP is wonderful.

No comments:

Post a Comment