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.

open or close right navigation

How to create editable web sites in PHP

 
written by:Christian Heilmann on 26.04.2002
Jump to page: 1  2  3  4  5  6  or read all on one page
 

Adding secured interactivity

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:


0    if ($_POST['pw']!="") {$pw=$_POST['pw'];}else{$pw=$_GET['pw'];}
1    $newcontent=$_POST['newcontent'];
2    $filelocation = "mytext.txt";
3    if (!file_exists($filelocation)) {
4    echo "Couldn't find datafile, please contact administrator!";
5    }
6    else {
7    $newfile = fopen($filelocation,"r");
8    $content = fread($newfile, filesize($filelocation));
9    fclose($newfile);
10    }
11    $content = stripslashes($content);
12    $content = htmlentities($content);
13    $pass="password";
14    if (!$pw || $pw != $pass){
15    $content = nl2br($content);
16    echo $content;
17    }
18    else {
19        if ($newcontent){
20            $newcontent = stripslashes($newcontent);
21            $newfile = fopen($filelocation,"w");
22            fwrite($newfile, $newcontent);
23            fclose($newfile);
24            echo 'Text was edited.<form><input type="submit"
            value="see changes" /></form>';    
25            }
26                else{
27                echo '<form method="post">
28         <textarea name="newcontent" cols="50"
                rows="15" wrap="virtual">';
29                echo $content;
30                echo "</textarea>
31                <input type="hidden" name="pw"
                value="'.$pass.'" />
                <br /><input type="submit" value="edit" />
32 </form>';
33                }
34        }    

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.

more»
Table of contents:

Introduction, what is this about?
Separation is necessary
The template with the script
What the script does
Adding secured interactivity
Goodies and limits

Back to top

Powered by EasyCMS