A "CMS" for a one-page site?

marcvangend picture marcvangend · Dec 31, 2009 · Viewed 21.1k times · Source

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?

Answer

Tatu Ulmanen picture Tatu Ulmanen · Dec 31, 2009

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.