php script to filter a text file and extract lines starting with keyword?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anne001

    #1

    php script to filter a text file and extract lines starting with keyword?

    Hi

    For a class, students are going to run an experiment on line. Each time
    a subject runs, his/her data is appended to one giant text file. Their
    own data set will be just one line starting with the keyword they gave
    as identification.

    The faculty does not want the students to be able to download and see
    the giant data file. He wants the students to only download and see the
    data that starts with their own identification tag.

    in unix, filtering a file to keep only the line starting with code MCB
    would look something like
    tail -f your_file_name | grep MCB
    from what I read.

    Given the concerns the faculty has for protecting the database, what do
    I need to look into to write a php script that would access the data
    file, but only show a web page with the data corresponding to the
    students identification code?

    thanks for some direction, php functions, php keywords I need to look
    into.
    Anne

  • Nick

    #2
    Re: php script to filter a text file and extract lines starting withkeyword?

    anne001 wrote:[color=blue]
    > Hi
    >
    > For a class, students are going to run an experiment on line. Each time
    > a subject runs, his/her data is appended to one giant text file. Their
    > own data set will be just one line starting with the keyword they gave
    > as identification.
    >
    > The faculty does not want the students to be able to download and see
    > the giant data file. He wants the students to only download and see the
    > data that starts with their own identification tag.
    >
    > in unix, filtering a file to keep only the line starting with code MCB
    > would look something like
    > tail -f your_file_name | grep MCB
    > from what I read.[/color]

    <?php

    $user = 'MCB'; //get this value from wherever it is you get it

    //get datafile as an array of lines
    $lines = file('datafile. txt');

    // Loop through the lines
    foreach ($lines as $line_num => $line)
    {
    if (strpos($line,$ user) === 0) //string found at start of line
    echo $line;
    }

    ?>

    Comment

    • anne001

      #3
      Re: php script to filter a text file and extract lines starting with keyword?

      Thank you very much.

      It just occured to me that the faculty's request to keep the raw data
      off limit may just not be possible: Any student can read the php code,
      find the address of the
      raw file, and download all of it.

      It seems to me his TA will have to run the script once a day to a file,
      which the students will in turn access with your little script.

      I was running through the tutorial
      Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

      to set php up -- which the students will have to do also.

      I also like to turn Off the allow_url_fopen feature, though this is
      more cautious paranoia than any tried-and-true trick. This setting
      allows the PHP fopen function to load remote documents stored on other
      web servers, and has been implicated in a number of break-ins with
      poorly designed applications. Turn it Off for now, and if you ever need
      to use it in the future, you'll probably be smart enough to make a more
      informed decision.

      If we turn off allow_url_fopen , will file work?
      Anne

      Comment

      • Nick

        #4
        Re: php script to filter a text file and extract lines starting withkeyword?

        anne001 wrote:[color=blue]
        > Thank you very much.
        >
        > It just occured to me that the faculty's request to keep the raw data
        > off limit may just not be possible: Any student can read the php code,
        > find the address of the
        > raw file, and download all of it.
        >
        > It seems to me his TA will have to run the script once a day to a file,
        > which the students will in turn access with your little script.
        >
        > I was running through the tutorial
        > http://www.macdevcenter.com/pub/a/ma...30/apache.html
        > to set php up -- which the students will have to do also.[/color]

        Are the students accessing this through a browser? If so then they won't
        be able to read the PHP source code, and the raw file doesn't need to
        be accessible to them, only by the script.

        To do this you will probably want to move the raw file so it's not under
        the web directory, and then access it as file('../file.txt') or whatever
        the path would be then. That way they can't get to the file.

        If they're running PHP directly as a command-line script, then you will
        just have to make sure that the raw file permissions make it unreadable
        to the students, but readable to PHP (which has default user 'nobody',
        I think).
        [color=blue]
        > If we turn off allow_url_fopen , will file work?[/color]

        Yes, that only applies to opening files from remote http:// locations,
        in my example it was a local file.

        Comment

        • anne001

          #5
          Re: php script to filter a text file and extract lines starting with keyword?

          Thank you so much for taking the time to help me, your script worked
          just fine for the class.

          thank you

          Comment

          Working...