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? 
Google.com  
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

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek