PHP
  Home arrow PHP arrow Page 2 - Private Pages with PHP and Text Files
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
PHP

Private Pages with PHP and Text Files
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-02-27


    Table of Contents:
  • Private Pages with PHP and Text Files
  • Creating the main PHP page
  • Using the Password
  • Encryption

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Private Pages with PHP and Text Files - Creating the main PHP page
    ( Page 2 of 4 )

    Next, you need to create the main PHP page that will do the real work.  With a blank page in a text editor, open a PHP block in the standard way:

    <?

    As I mentioned before, PHP has a standard set of functions and methods used for working with files.  The main ones that we will need are the fopen(), fread() and fclose() functions.  To do anything with a file, we need to open it, and clearly, this is done using the fopen() function.  We need to specify what we intend to do the file; read it, read and write to it are the most common tasks, but additional flags can be used to tell the program whether to place the file pointer at the beginning or end of the file and whether to create the file if it does not already exist.  All we are going to need to do for this example, however, is open the text file containing the password for reading. 

    First then, create a variable that specifies the path to the text file:

    $fileloc = "/apachesite/docs/pass.txt"

    Next, create a variable to hold the file pointer:

    $filetoread = fopen($fileloc, "r") or die("Could not open
    password file");

    You can also use the die method to end the script and print an appropriate message on screen if the operation fails for some reason.  Once the file has been opened, you’ll need to read the contents of it so that it can be compared to the input from the password form: 

    $storedpass = fread($filetoread, filesize($fileloc)) or die
    ("Could not read stored password");

    You set a variable to hold the data from the file and call the fread() method which takes two parameters: the file pointer and the length of the file.  You may or may not know the length of your password. For future programming ease (when the password needs to be changed) you can use the filesize() method to just grab it all.  As soon the file is no longer needed, it should be closed:

      fclose($filetoread);



     
     
    >>> More PHP Articles          >>> More By Dan Wellman
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT