You are seeing this very light version of the page, as you are using an outdated
browser!
Onlinetools.org is done in XHTML 1.0 and supports CSS 2.0, please upgrade your
browser if you want a better version of this site.
Thank you, please help us developers keep up with the industry rather than patching our code for outdated applications.
Introduction, what is this about?
Separation is necessary
The template with the script
What the script does
Adding secured interactivity
Goodies and limits
This short tutorial explains how to create webpages that can be edited by someone directly on the server. This person does not need to have any scripting knowledge. To create a page like this let's use PHP, although the same stunt is possible in Perl or ASP.
But first of all, what is the neat thing about editable pages? Well, whoever worked freelance for others, knows how tiring it can be to get an email or a call, just because there is a full stop in the page that should not be there. Creating a site for a customer also means caring for him, and by providing him with an editable page, you took a big step to help you and him:
Enough of the chit chat. How to do it?
The key to create editable websites is to seperate content from display. This is an absolute must, here or when you create pages from a database or from XML.
What you do is create an HTML document with all the design definitions, like tables, fonts and colors. Where the copy text is supposed to be, you add a small php script.
You put the copy text in a ".txt" file with a catchy name, and save your HTML document as a php file for the time being.
Now, what our script will do is to read the .txt document and print it in the HTML Template.
Ok, that displays it, but it does not make it editable yet, right? Well, wait on, you'll be surprised how easy it is.
Let's take this as a demotext:
mytext.txt
This is our demo template.
mypage.php
Now save both to your server and set the file attributes of the .txt file to 666, to allow script to manipulate it.
To see it in action, click here.
Now, what does all this mean?
Line 1 defines the name of the text file, in line 2-4 the script checks if the file is there and displays a message, when there is no datafile.
If there is a datafile, then the lines 6-8 read it's content into the variable $content.
Lines 10-12 manipulate the content, the backlslashes php adds to quotationsmarks are removed, umlauts and special characters are replaced by their html equivalent and every linebreak becomes a <br />.
Now, we're halfway there, we only need to add the tool that changes the content of the textfile.
What we need to add some security is a password-check so not anyone can edit the file, and we need a form to display the text.
The trick is just to change line 12 and 13, and write a <textarea> tag around the display:
HUH??? Calm down, it looks complicated, but it isn't...
In line 13 we define the password, line 14 checks if there was a password submitted, and if it was the correct one.
If there is no password or a wrong one, the text is displayed in lines 15-16 (that was the old script).
Line 18 requires the correct password.
Line 19 checks for a new content to be stored in the text file. If there is a new content, lines 20-24 overwrite the old textfile, display a message that the text was edited and show a button to go back to the page.
If there is no new content yet, the lines 27-32 display a form with the textfile in a editable textarea. When this form is submitted, it submits the password and the new content to trigger the change in lines 18 and 19.
That's all you need! 35 lines of code and editable pages are not a myth anymore. To edit the page you simply add a ?pw=password to the url and you will get your form. Try it here.
Now all that is left to know is the pros and cons of this trick.
Ok, now you know how to create a page that can be edited in PHP. This trick is no rocket science but is has some advantages:
Limits:
This trick only allows people to enter copy that is plain text and do linebreaks. Any other layout functionality has to be coded seperately, or if you remove the "$content = htmlentities($content);" line you can allow people to use HTML in their text.
Furthermore it can get nasty when more than one user writes in this editor at the same time, the text can get truncated or storing it might not be possible.
To download the files of this example as a zip click here.