Saturday, January 29, 2011

PHP, create a simple dynamic edit content without using MySQL


Put your dynamic web content in a file such as page1.txt and make sure  it can be written.
In this case, I put:
<h1><center>Welcome to My Homepage</centeer></h1><p /> This is the content
A better and complete version is "fast edit". You can download it for free from:
http://fast-edit.co.uk/ 
Fast Edit is o a Content Management System (CMS) where you can manage files, create and delete pages, change style, make and restore backups, and decide what to display in the automatic menu.
Below is to display the basic idea for editing and how to use login, logout and session.
Code:


<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
$admin_name = 'admin';
$admin_password = 'admin';
?>
<?php
$page1 = "page1.txt";
$fh = fopen($page1, 'r');
$page1Data = fread($fh, filesize($page1));
fclose($fh);

if(isset($_POST['submit'])){
if( (($_POST['name'])==$admin_name) && (($_POST['pass'])==$admin_password) ) {
                   //  echo "correct combination of username and password";
                     $_SESSION['username'] = $admin_name;
$PHP_SELF=$_SERVER['PHP_SELF'];
                     echo "<h3>Edit Contents of File</h3>
<form action=$PHP_SELF?action=save method= 'post' >
<textarea name='newd' cols='100%' rows='30'> $page1Data </textarea>
<input type='submit' name='editContent' value='save'>
</form><BR />";
 }
               else echo "incorrect combination of username and password";
}
?>
<?php

if(isset($_SESSION['username'])){
// end login session with logout
if($_GET['action']=='logout')
        {
        session_destroy();
        unset($_SESSION['username']);
        }
elseif($_GET['action']=='save'){
$fhw = fopen($page1, 'w') or die("can't open file");
fwrite($fhw, $page1Data);
fclose($fhw);

}
     }
?>
<?php if(!isset($_SESSION['username'])):?>
<form method="post" name="edit" action="<?php echo $_SERVER['PHP_SELF'];?>?action=login">
                Username:<br/><input type="text" name="name" size="15"/><br/>
                Password:<br/><input type="password" name="pass" size="15"/><br/>
<INPUT TYPE="submit" name="submit" VALUE="edit!">
</form>
<?php
echo $page1Data;
?>
<?php else: ?>
<form method="post" name="edit" action="<?php echo $_SERVER['PHP_SELF'];?>?action=logout">
<INPUT TYPE="submit" name="logout" VALUE="logout!">
</form>
<?php endif; ?>

Result: edit will not work due to no php in the blog
Username:


Password:



Welcome to My Homepage

This is the content

No comments:

Post a Comment