tags:

views:

349

answers:

8

I'm going to build a site for a client that consists of only one page. The page has only one div with editable content; the rest can be hard-coded in a template file.

The client wants CMS-like behavior: logging in on the site and editing that single piece of text (preferably inline). I usually build larger sites with Drupal, but that would be overkill for something simple like this.

Does anybody know of a good (open source) solution for a site like this?

+9  A: 

It shouldn't be a large job to code this from scratch. All you need is admin.php with some kind of authentication and one form. I timed myself and made this in 7 minutes:

Login and logout

if(isset($_GET['login'])) {    
    // Check user credentials from db or hardcoded variables
    if($_POST['username'] == 'user123' && $_POST['password'] == 'pass123') {
        $_SESSION['logged'] = true;
    } else {
        $loginerror = 'Invalid credentials';
    }
}

if(isset($_GET['logout'])) {
    $_SESSION = array();
    session_destroy();
}

Login form

if(!isset($_SESSION['logged']) || $_SESSION['logged'] !== true): ?>
    <form method="post" action="admin.php?login">
        <?php if(isset($loginerror)) echo '<p>'.$loginerror.'</p>'; ?>
        <input type="username" name="username" value="<?php isset($_POST['username']) echo $_POST['username']; ?>" />
        <input type="password" name="password" />
        <input type="submit" value="Login" />
    </form>
<?php endif;

Actual admin area

if(isset($_SESSION['logged']) && $_SESSION['logged'] === true):
    // Save contents
    if(isset($_GET['save'])) {
        file_put_contents('contents.txt', $_POST['contents']);
    }    
    // Get contents from db or file
    $contents = file_get_contents('contents.txt');
    ?>
    <a href="admin.php?logout">Logout</a>
    <form method="post" action="admin.php?save">
        <textarea name="contents"><?php echo $contents; ?></textarea>
        <input type="submit" value="Save" />
    </form>
<?php endif;

Just combine those segments to get the full code. This code snippet has authentication, logout functionality and saves the contents of a textarea in a file. Alternatively you could change this so that users and content resides in database.

Personally, it would have taken longer for me to find an appropriate lightweight CMS and configure it to work.

Tatu Ulmanen
Of course, that's a possibility. Still, there's no need to code it myself if it exists already. That's why I decided to ask you guys and girls.
marcvangend
Now it exists, you're welcome :)
Tatu Ulmanen
Tatu, that's great, thanks. It's my first question here on Stack Overflow - I didn't expect that it would be this fast!
marcvangend
@marcvangend, don't forget to mark the answer as accepted if you found it useful.
Tatu Ulmanen
+1  A: 

Have a look at TiddlyWiki. I'm not sure if this have login facilities etc, but It has all the major features of a small CMS.

Christy John
+1  A: 

Try CushyCMS.

Geert
wow! CMS' site in Russian! :)
myfreeweb
Thanks for the tip. Cushy does look nice, but it's an online service rather than a downloadable CMS, if I understand correctly.
marcvangend
A: 

I like Wordpress. It's techincally "blogging" software, but it's very easy to extend into a nice small CMS.

Kyle
A: 

It's easy. You can code one in 10 minutes (or ask me).

Just a page with and admin file for editing text.txt.

myfreeweb
A: 

Use Wordpress and create a simple theme. Wordpress has article history too so it's easy to go back if a mistake is made and so on.

Gazzer
+2  A: 

Ok, here is my version of the CMS. You can find all my files here in a zip archive: http://chechi.be/midas/simple-cms.zip.

This is the admin page:

<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>CMS</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="main">
<h1>CMS</h1>
<?php
if (empty($_POST) && isset($_GET['action'])) {
        $action = $_GET['action'];
        switch ($action) {
            case 'logout':
                session_unset();
                session_destroy();
                break;
    }
}
if (!isset($_SESSION['user'])) {
    $user = '';
    $pass = '';
    if (isset($_POST['login'])) {
        $user = strtolower(trim($_POST['user']));
        $pass = $_POST['pass'];
        $errors = array();
        if ($user == '' || $user != 'admin') {
            $errors['user'] = '';
        }
        if ($pass == '' || $pass != '123456') {
            $errors['pass'] = '';
        }
        if (empty($errors)) {
            $_SESSION['user'] = $user;
        } else {
            echo '<p class="error">Please fill in your correct ';
            if (isset($errors['user']))
                echo 'username';
            if (count($errors) == 2)
                echo ' and ';
            if (isset($errors['pass']))
                echo 'password';
            echo '.</p>', "\n";
        }
    }
}
if (isset($_SESSION['user'])) {
    $user = $_SESSION['user'];
?>
<div id="headertext">
    <p class="l">You are logged in as <strong><?php echo $user?></strong>.</p>
    <p class="r"><a href="?action=logout">Logout</a></p>
</div>
<?php
    if (isset($_POST['edit'])) {
        if (file_put_contents('homecontent.txt', $_POST['homecontent']))
            echo '<p class="succes">Your changes are saved.</p>', "\n";
    }
    $homecontent = file_get_contents('homecontent.txt');
?>
<form method="post" action="">
    <p>Here you can edit your homepage text:</p>
    <textarea name="homecontent" id="homecontent" rows="20" cols="55"><?php echo $homecontent?></textarea>
    <p><button type="submit" name="edit">Save changes</button></p>
</form>
<?php } else {?>
<form method="post" action="" id="login">
    <p>
        <label for="user">Username:</label><input type="text" name="user" id="user" value="<?php echo $user?>" />
    </p>
    <p>
        <label for="pass">Password:</label><input type="password" name="pass" id="pass" value="<?php echo $pass?>" />
    </p>
    <p>
        <button type="submit" name="login">Login</button>
    </p>
</form>
<?php }?>
</div>
</body>
</html>
Midas
Thank you Midas, I really appreciate your help. Your script comes even closer to what I was looking for than Tatu's. I wish I could mark both as accepted answers, but since I can't, I'm marking yours as accepted now.
marcvangend
+2  A: 

I really love Zimplit for projects that consist of a "a very few pages". They have a brilliant concept of a really minimalistic WYSIWG-editor that edits the entire page (not just the contents). And no database, or other cruft.

I found that many clients can really grok the concept immediately.

berkes
Great, Zimplit looks very easy indeed. In its use it's closer to a Word Processor than to a traditional full-fledged CMS, so I can understand that clients would love this for a simple site.
marcvangend