Saturday, December 27, 2014

Alternative ways to use PHP scripts.

Over the past 10 years I have used PHP quite extensively in my websites. Mostly I use php as a way to build each page pulling in files from around my server organising them and then displaying the finished result. It has worked very well most of the time.

There is a downside that I have noticed the more people visit my websites. The trouble revolves around the server hosting company where I host my websites. Occasionally the php will stop working, effectively leaving my website with blank or error pages. While the php scripts stop working the pure html pages that are stored on the server are still visible to visitors. Of course I could just create the html text files by hand and that would make sure this particular error doesn`t occur anymore, however with over 200000 pages to remake it is an impossible task. Impossible except of course I can use php to generate my webpages and store them as pure text files.

Usually a webpage made using php would look something like this in code

The below file would be saved onto your server with a name like helloworld.php



echo is the most basic/easy way of printing text to the browser window.

helloworld.php

An alternative way to use the php script would be to do something like this. I`ll save this as generatepage.php on my server.



generatepage.php
helloworld.html

So what we are doing here is making a string variable $str and we are assigning it with the same information as the helloworld.php program but this time we are saving it using the fopen, fwrite and fclose instructions to a file called helloworld.html Okay so you might be thinking that is just making one file to make another and that is true but the real power in this method is that I can create one php file that could generate millions of pages from hundreds of files.

When we are trying to make a website one of the aspects we want to replicate each page is the title bar, layout and page ending (faq links, about links).

This is quite easy by using either includes or file_get_contents

Save a file called mywebsitetitle.txt with the contents



and we shall save the following in the file generatepage2.php



generatepage2.php
helloworld2.html

And for the final example lets make a slightly more complex file that will generate multiple files. First lets make a list of files that we want to create minus the ending .html. We will call this mywebsitefiles.txt and will look like this

Now we will update our page generator to load this file and for each filename go through the same process effectively creating 3 new files with the same layout and title bar. We will call the generator generatepage3.php and it looks like this. generatepage3.php

mywebsiteindex.html
mywebsitefaq.html
mywebsiteaboutus.html

The key is the foreach loop which allows us run the same instructions many times. We can add titles, meta tags, descriptions, text files and images to create differenet pages but with the same layout we makes for a good user experience.

I hope this has been of use to you and if you have any questions don`t hesitate to ask!

No comments:

Post a Comment