www.webdeveloper.com
Results 1 to 4 of 4

Thread: writing/reading external files

  1. #1
    Join Date
    Jun 2003
    Location
    The United States Site: http://kellyj.t35.com
    Posts
    2,561

    writing/reading external files

    I use javascript and vbscript for everything so I kinda don't know any PHP, but I know that what I need has some PHP possibilities.

    I developed a code to write to a file 4 values in the form:

    value1 (name), value2 (date), value3 (time), value4 (email)

    The file is a basic txt file located in paths/file.txt. It contains many lines though and I need a code that will:

    1) loop the number of lines the text file is
    2) retrieve the values(1-4) of the first line in which user input is equal to value4 of that line
    3) i want to redirect the user to a page, depending on the values, based on an 'if' condition

    The text file looks like:

    Code:
    John, 23 December 2003, 12:23, email@server.com
    Matt, 24 December 2003, 1:53, email@server.com
    Mark, 25 December 2003, 8:25, email@server.com
    Jen, 26 December 2003, 21:14, email@server.com
    The code on the page has a text box and asks for email address. It then will check the address of each line until it finds a matching one. Then, depending on all four values of the first matching line, the user will be redirected to a specified page, for example: 'their name.htm'. If the emails don't match, it will go to '/newemail.htm'

    I think it is possible, but I am not quite sure where to start!
    Last edited by steelersfan88; 01-29-2004 at 04:36 PM.

  2. #2
    Join Date
    May 2003
    Location
    Millis, MA
    Posts
    453
    To get the values, you can use explode().

    To read/write to a file, you'll need the filesystem functions.

    Here's a basic way to do what you want:

    PHP Code:
    <?php
    $fileName 
    "paths/file.txt"// path and name of file
    $newMailPage "newmail.htm"// path and name of new email page
    $email $_GET['email']; // form email input;  change 'email' to the name of the input; change to $_POST['email'] if you use the POST method
    $fileHandle file($fileName); // open the file and read assign each line to an array
    foreach ($fileHandle as $line) {
        list(
    $val1$val2$val3$val4) = explode(','$line); // split the line into the four variables
        
    $val1 trim($val1); // remove whitespace from the front and back of the string
        
    $val2 trim($val2);
        
    $val3 trim($val3);
        
    $val4 trim($val4);
        if (
    $val4 == $email) {
            
    header("Location: " $val1 ".htm"); // send a redirect header
            
    exit; // exit the program, so the user won't be redirected to the new mail page
        
    }
    }
    header("Location: " $newMailPage);
    ?>
    Hope that helps!

  3. #3
    Join Date
    Jun 2003
    Location
    The United States Site: http://kellyj.t35.com
    Posts
    2,561
    thanks so much i changed it a tad because the one page wouldn't upload but it works perfectly!!!

  4. #4
    Join Date
    May 2003
    Location
    Millis, MA
    Posts
    453
    Glad to help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles