Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Creating a News System with PHP - Part 1
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Creating a News System with PHP - Part 1
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2002-02-23

    Table of Contents:
  • Creating a News System with PHP - Part 1
  • Validating the Form
  • Storing the Form Data
  • Displaying the News

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a News System with PHP - Part 1 - Storing the Form Data


    (Page 3 of 4 )

    At this point, we have checked the form to make sure the data looks like we want it. Now, it's time to put the data into our text file. In order for this to work, you will need to make sure that the webserver process has write access to the directory you plan to store the file in. If the webserver is unable to write to the directory, you will not be able to store any information.

    The first thing we need to do in order to write to a file is open it up. To do this, we will use the fopen function.

    <?php
    $fp 
    fopen('news.txt','a');
    if(!
    $fp) {
        echo 
    "Error opening file!";
        exit;
    }
    ?>

    Ok, what we did there is open up a file called news.txt that is in the same directory as the script we are running. The 'a' specifies that we want to append information to the end of this file. If the file doesn't exist, it will attempt to create it. I also added a bit of error checking. If the script is unable to open to file for whatever reason, it will spit out an error and halt.

    Next, let's build the line we want to put into this file. We want to store the date and the information inputted on the form. Remember that we also want to keep everything on one line, so we are going to turn any newlines into a BR tag with the str_replace function. Then we need to go back and add a newline to the end of the data so that the next item we put in starts on the next line of the file.

    <?php
    $line 
    date("m.d.y") . "|" $HTTP_POST_VARS['name'];
    $line .= "|" $HTTP_POST_VARS['news'];
    $line str_replace("\r\n","&lt;BR&gt;",$line);
    $line .= "\r\n";
    ?>

    Good, now that should do it for getting our news into a text file. Let's put it all together and see how it looks.

    &lt;?
    //this should all go into one file. I would name it addnews.php
    if($HTTP_POST_VARS['submit']) {
        if($HTTP_POST_VARS['password'] == 'pass') {
            if(!$HTTP_POST_VARS['name']) {
                echo "You must enter a name";
                exit;
            }
            if(!$HTTP_POST_VARS['news']) {
                echo "You must enter some news";
                exit;
            }
            if(strstr($HTTP_POST_VARS['name'],"|")) {
                echo "Name cannot contain the pipe symbol - |";
                exit;
            }
            if(strstr($HTTP_POST_VARS['news'],"|")) {
                echo "News cannot contain the pipe symbol - |";
                exit;
            }
            $fp = fopen('news.txt','a');
            if(!$fp) {
                echo "Error opening file!";
                exit;
            }
            $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
            $line .= "|" . $HTTP_POST_VARS['news'];
            $line = str_replace("\r\n","&lt;BR&gt;",$line);
            $line .= "\r\n";
            fwrite($fp, $line);
            if(!fclose($fp)) {
                echo "Error closing file!";
                exit;
            }        
        } else {
            echo "Bad Password";
        }
    }

    ?&gt;
    &lt;FORM ACTION="&lt;?=$PHP_SELF?&gt;" METHOD="POST" NAME="newsentry"&gt;
    Your name:&lt;BR&gt;
    &lt;INPUT TYPE="text" SIZE="30" NAME="name"&gt;&lt;BR&gt;
    The News:&lt;BR&gt;
    &lt;TEXTAREA NAME="news" COLS="40" ROWS="5"&gt;&lt;/TEXTAREA&gt;&lt;BR&gt;&lt;BR&gt;
    News Password:&lt;BR&gt;
    &lt;INPUT TYPE="password" SIZE="30" NAME="password"&gt;&lt;BR&gt;
    &lt;INPUT TYPE="submit" NAME="submit" VALUE="Post it!"&gt;&lt;BR&gt;
    &lt;/FORM&gt;

    More Miscellaneous Articles
    More By Matt Wade


       · Denne artikel er nu ret interessandt, mon ikke der er noget der kan bruges her ?
       · im new on php, all this code goes embedded on another html file or in the same...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway