Basic of the basic type blog

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bdiddymc
Forum Newbie
Posts: 3
Joined: Sun Jul 13, 2008 9:42 pm

Basic of the basic type blog

Post by bdiddymc »

Hi Everyone,

I am just new, so please be gentle.

I would like to create a site that allow a client to update only one page of the site. I have built it all in PHP except for this page.

So it is kind of like a blog bit it doesn't need any more information than the headings and the stories.

No dates, no comments, nothing more.

I have had a brief look at some php blogs such as simplephpblog and pivot.

But the admin areas look to complex for what the client will want to see.

Am I heading in the right direction for using something like this, or should I be approaching it from another angle?

Any help would be greatly appreciated.
-B
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: Basic of the basic type blog

Post by Dynamis »

Most pre-built code will be far more advanced than what you are looking for, so I would guess. The best way of doing this would probably be to create a db with 3 columns, id auto incrementing, heading, and story. Then your form would be handled by a page to error check and then add the heading and story to the db. Let me know if you need more clarification or help on how to make something of this nature.
bdiddymc
Forum Newbie
Posts: 3
Joined: Sun Jul 13, 2008 9:42 pm

Re: Basic of the basic type blog

Post by bdiddymc »

I am going to try make it without a Database, as it is really tiny and probably doesn't even need that. Hopefully it's not bad practice to use file based systems.

I have found a tutorial which should hopefully take me where I want to go, if it is worthy i'll post the link for others that may have the same issue.

Cheers
bdiddymc
Forum Newbie
Posts: 3
Joined: Sun Jul 13, 2008 9:42 pm

Re: Basic of the basic type blog

Post by bdiddymc »

I ended up finding this tutorial which allows for a very basic news system with no security:

http://www.phptoys.com/e107_plugins/con ... content.74

What I would now like to have is the ability to EDIT and DELET the news stories if the admin wishes... but I'm finding that a little tricky. I'm not a very well versed with PHP so i may be biting off more than I can chew at the moment. But I find jumping in is the best way for learning.

This could be code overboard. I have this as the

common.php:

Code: Select all

<?php
 
session_start();
 
function registerUser($user,$pass1,$pass2){
    $errorText = '';
    
    // Check passwords
    if ($pass1 != $pass2) $errorText = "Passwords are not identical!";
    elseif (strlen($pass1) < 5) $errorText = "Password is to short!";
    
    // Check user existance 
    $pfile = fopen("userpwd.txt","a+");
    rewind($pfile);
 
    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            $errorText = "The selected user name is taken!";
            break;
        }
    }
    
    // If everything is OK -> store user data
    if ($errorText == ''){
        // Secure password string
        $userpass = md5($pass1);
        
        fwrite($pfile, "\r\n$user:$userpass");
    }
    
    fclose($pfile);
    
    
    return $errorText;
}
 
function loginUser($user,$pass){
    $errorText = '';
    $validUser = false;
    
    // Check user existance 
    $pfile = fopen("userpwd.txt","r");
    rewind($pfile);
 
    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            // User exists, check password
            if (trim($tmp[1]) == trim(md5($pass))){
                $validUser= true;
                $_SESSION['userName'] = $user;
            }
            break;
        }
    }
    fclose($pfile);
 
    if ($validUser != true) $errorText = "Invalid username or password!";
    
    if ($validUser == true) $_SESSION['validUser'] = true;
    else $_SESSION['validUser'] = false;
    
    return $errorText;  
}
 
function logoutUser(){
    unset($_SESSION['validUser']);
    unset($_SESSION['userName']);
}
 
function checkUser(){
    if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
        header('Location: login.php');
    }
}
 
?>
This is the admin area:

Code: Select all

<?php
    require_once('common.php');
    checkUser();
?>
<?php
/*************************************************
 * Micro News
 *
 * Version: 1.0
 * Date: 2007-07-12
 *
 *
 ****************************************************/
 
if (!isset($_POST['submit'])) {
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Micro News</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
   <script language="javascript" type="text/javascript" src="js/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
    mode : "textareas",
    theme : "advanced",
    theme_advanced_buttons3 : "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
 
});
</script>
</head>
 
<body>
  <div id="main">
    <div id="caption">Micro News - Add news</div>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        News title:<br/>
        <input type="text" name="title" size="40"/><br/><br/>
        Content:<br/>
        <textarea name="newstext" rows="15" cols="67"></textarea><br/>
        <center><input type="submit" name="submit" value="Save" /></center>
     </form> 
     
  </div>
</body>   
 
<?php } else {
   $newsTitel   = isset($_POST['title']) ? $_POST['title'] : 'Untitled';
   $submitDate  = date('Y-m-d g:i:s A');
   $newsContent = isset($_POST['newstext']) ? $_POST['newstext'] : 'No content';
   
   $filename = date('YmdHis');
   $f = fopen('../news/'.$filename.".txt","w+");         
   fwrite($f,$newsTitel."\n");
   fwrite($f,$submitDate."\n");
   fwrite($f,$newsContent."\n");
   fclose($f);
 
   header('Location:../share_your_story.php');   
}
?>
 
<a href="logout.php"> To log out click here!</a></p>
And this page displays the news feed:

Code: Select all

<?php
/*************************************************
 * Micro News
 *
 * Version: 1.0
 * Date: 2007-07-12
 *
 * Usage:
 *
 ****************************************************/
 
// This function reads all available news
function getNewsList(){
    
   $fileList = array();
   
    // Open the actual directory
    if ($handle = opendir("news")) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            if (!is_dir($file)) {
               $fileList[] = $file;
        }
        }
    }   
    
    rsort($fileList);
    
    return $fileList;
}
 
?>
 
 
<?php
    
      $list = getNewsList();
      foreach ($list as $value) {
        $newsData = file("news/".$value);
        $newsTitle  = $newsData[0];
         $submitDate = $newsData[1];    
         unset ($newsData['0']);
         unset ($newsData['1']);
        
         $newsContent = "";
         foreach ($newsData as $value) {
               $newsContent .= $value;
         }
        
        echo "<h4>". $newsTitle . "</h4>";
        echo "<p>" .$newsContent . "</p>";
      }
 ?>
 
Any guidance would be a huge help.
Cheers
Post Reply