GET vs POST related problem solved

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deacon57
    New Member
    • Nov 2006
    • 1

    GET vs POST related problem solved

    FYI - If you are a computer scientist (or geek), this post may be for you.

    I wanted to log any search keywords, etc that are used on my site. I created a simple
    program that logs search terms into a log file. The problem I experienced was that
    I was getting double entries in the log... that is, when writing to the file, each line I
    wrote to the file was repeated twice.

    How was that possible? Scanning the code provided no clues. The PHP code in the file is in-line, there are no control structures, so looping is not possible. The changing datestamp on duplicate lines proved that the file was being called from 1 to 3 times... bizarre.

    After mucho, mucho hours spent tweaking the code and scanning the net for similar problems (couldn't find any) I almost gave up.

    The solution I stumbled on (last thing I could tweak) was to change the search form's METHOD to "POST" from "GET."

    It appears (see below) that the GET was somehow running the ACTION processing file from one to three times. Sounds bizarre, but
    I have proof that it happened! It is re-creatable. Try it yourself.

    Go figure... I'm posting this information in case some other poor soul spends hours trying to figure out a similar double logging problem.

    Not sure why the difference between POST vs GET solves the problem - but I'm happy to have stumbled upon a workable solution after many hours... and no joy searching the web for an answer, either.

    Any explanation(s) for this strange behavior would be greatly appreciated.

    Peter Deac Bakke, Tucson AZ

    PS. Website is running PHP4, hosted on www.globalserve rs.com



    ---------


    Input form:

    <form action="searchc ode.php" method="GET">

    // NOTE:
    // To fix my problem, the line above was changed to <form action="searchc ode.php" method="POST">
    // The following form is in a table, but I've stripped the table tags out for brevity



    <b>Website Name or URL: </b><br />
    <input type="text" name="name_form ">


    <b> Enter Keywords or Phrases:
    <input type="text" name="keywords_ form">

    <b>Enter state: </b>

    <select name="location_ form">
    <option value="" selected="selec ted">None</option>
    <option value="US">US - National</option>
    <option value="AL">Alab ama</option>
    .
    .
    .
    //note: all states are listed as options... they have been removed here for brevity

    </select>


    <b>Enter Viewpoint : </b> <br />
    [PHP]
    <?php
    include($DOCUME NT_ROOT . "viewpoint.php" );
    ?>
    [/PHP]
    <b>Enter Website Type: </b><br />
    [PHP]
    <?php
    include($DOCUME NT_ROOT . "type.php") ;
    ?>
    [/PHP]

    <input type="submit" value="Search Websites">
    <INPUT value="Clear Fields" type="reset">


    </form>


    ---------



    [PHP]

    $filecontents = "";

    // Time stamp
    $timenow = date("Ymd, G:i:s T");
    $filecontents .= ($timenow . "|");


    if ( ($name_form != NULL) ) {
    $filecontents .= ("Name/URL=" . $name_form . "|");
    }
    if ( ($location_form != NULL) ) {
    $filecontents .= ("State=" . $location_form . "|");
    }
    if ( ($viewpoint_for m != NULL) ) {
    $filecontents .=("Viewpoint= " . $viewpoint_form . "|");
    }
    if ( ($type_form != NULL) ) {
    $filecontents .=("Type=" . $type_form . "|");
    }
    if ( ($keywords_form != NULL) ) {
    $filecontents .=("Keywords=" . $keywords_form . "|");
    }

    $filecontents .= "\n";

    $filetoopen = "$pathtofil e . search.log";
    $searchlog1 = fopen($filetoop en, "a");
    fwrite($searchl og1, $filecontents);
    fclose ($searchlog1);
    [/PHP]

    ---------


    Meanwhile, in the output file:

    // WHILE USING METHOD="GET" in the input form, writing to the file resulted in DOUBLE entries seen below
    // Note that the TIMESTAMP at the beginning of duplicate lines is one second later... meaning the
    // file's code is executing twice, sometimes three times(!). That is to say, to the best of my knowledge, the action file was
    // being called multiple times, somehow. The PHP code in the file performing the writing is in-line, there is no control structure
    // or looping in the file, making in-file duplicate writing impossible. The ACTION file must have been called more than once.
    // The datestamp differences prove that the php file was executed more than once (!?) ... I even saw three duplicate lines
    // occasionally with three different timestamps written to the file.

    20061107, 1:07:31 MST|Keywords=zz XXZZXXZ|
    20061107, 1:07:32 MST|Keywords=zz XXZZXXZ|
    20061107, 1:08:43 MST|Keywords=wh atever|
    20061107, 1:08:44 MST|Keywords=wh atever|
    20061107, 1:10:53 MST|Name/URL=zxczcxxc|St ate=AL|Keywords =zxczcz|
    20061107, 1:10:54 MST|Name/URL=zxczcxxc|St ate=AL|Keywords =zxczcz|
    20061107, 1:10:55 MST|Name/URL=zxczcxxc|St ate=AL|Keywords =zxczcz|

    // Then, after CHANGING THE INPUT FORM TO USE METHOD="POST", writing output to file was AOKAY - no double entries!!
    // Go figure

    20061107, 1:13:44 MST|State=AK|
    20061107, 1:14:39 MST|State=DE|
    20061107, 1:22:03 MST|Name/URL=xyxyxyxyxy| State=KS|Keywor ds= |
    20061107, 1:34:11 MST|Name/URL=primary|
    20061107, 1:36:50 MST|State=NH|
    20061107, 1:38:02 MST|Keywords=fg fghfdh|
    20061107, 1:45:33 MST|Name/URL=dfsdfsd|Typ e=Academic|


    ----END

    Again, any explanation(s) for this strange behavior would be greatly appreciated.

    Peter Deac Bakke, Tucson AZ
Working...