Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - 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 - Validating the Form


    (Page 2 of 4 )

    Ok, so we have a form where we can enter in our news. Now, we need to take the news and put it into our text file. Before we do that though, we need to check the password that was entered and check if it is correct. Now, when I deal with forms, I always access the form variables via the $HTTP_POST_VARS array. Some people don't do this, but you will see throughout all my examples that I do. First, let's check if the form has been submitted:

    <?php
    if($HTTP_POST_VARS['submit']) {
        echo 
    "The form has been submitted";
    }
    ?>

    Ok, obviously that doesn't actually do anything with the submitted form, but it does check to see if it was submitted. Now, we can add some other code to actually process the form. First thing we need to do is check that password and see if it is correct.

    <?php
    if($HTTP_POST_VARS['submit']) {
        if(
    $HTTP_POST_VARS['password'] == 'mysecretpassword') {
            echo 
    "The form has been submitted";
        } else {
            echo 
    "Bad Password";
        }
    }
    ?>

    Now, this isn't the most secure way to do things. But, for simplicity that is the way we are going to do it for now. The next step is to validate that we actually have data in the other two fields. Another couple of if statements should take care of that bit.

    <?php
    if($HTTP_POST_VARS['submit']) {
        if(
    $HTTP_POST_VARS['password'] == 'mysecretpassword') {
            if(!
    $HTTP_POST_VARS['name']) {
                echo 
    "You must enter a name";
                exit;
            }
            if(!
    $HTTP_POST_VARS['news']) {
                echo 
    "You must enter some news";
                exit;
            }
            echo 
    "The form has been submitted";
        } else {
            echo 
    "Bad Password";
        }
    }
    ?>

    Ok, one last thing we need to do to validate the form. When we store the data in the file, we are going to store each entry on one line. I plan to separate each piece of information with a pipe symbol ( | ). We need to make sure that none of the data already contains a pipe symbol, or our whole storage system will fail. In order to do that, we will use the strstr function. This function will return false if what we are looking for is not found in the string. If it returns anything, it will evaluate as true and we will know that a pipe symbol exists in the data we are checking.

    <?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;
            }
        } else {
            echo 
    "Bad Password";
        }
    }
    ?>

    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 3 hosted by Hostway