"Three Strikes You're Out" rule fails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    "Three Strikes You're Out" rule fails

    I have a counter that evokes the "Three Strikes You're Out" rule.. if
    you make more than N mistakes it auto-resets to avoid flooding
    $_SESSION with attempt after attempt, etc.

    However, the counter never advances beyond 1!

    [PHP]
    // HANDLE THE PART WHERE THE STUDENT INFORMATION WILL BE UPDATED OR
    SEARCHED
    if (is_array($_POS T) && @sizeof($_POST) > 0) {
    $accepter =& new Accepter($stude nt_id);
    if (!$accepter->isValid) $errorArray = $accepter->getErrorArray( );
    } else {
    // NEW 3/24/2006: MAKE SURE THE $_SESSION KOUNTER IS RESET SINCE
    THEY HAVE DONE NO FORM ACTION
    //unset($_SESSION["${projectAcron ym}_kounter"]);
    //@session_unregi ster("${project Acronym}_kounte r"); //
    DESTROY SESSION ERROR COUNTER TO FORCE IT TO RESET
    $_SESSION["${projectAcron ym}_kounter"] = null;
    }

    [/PHP]

    And this is supposed to advance the $_SESSION counter:

    [PHP]

    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    New 2/27/2006: New "Three Strikes You're Out" Rule: To prevent
    overstuffing of $_SESSION and other memory-encroaching collection
    objects,
    a "Three Strikes You're Out" rule will be implemented. If the user
    makes fewer than 3 mistakes either within Accepter or in
    ActionPerformer combined,
    then a $_SESSION counter will increase, up to 2 tries. After the 2nd
    try, all $_SESSION variables prefixed by $projectAcronym are destroyed
    and you are
    automatically rerouted back to the default page

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    print_r("Before : "); print_r($_SESSI ON["${projectAcron ym}_kounter"]);
    print_r("<P>");
    if (is_array($_POS T) && @sizeof($_POST) > 0 && is_object($acce pter)
    && @is_a($accepter , 'Accepter') && is_object($ap) && @is_a($ap,
    'ActionPerforme r') &&
    (!$accepter->isValid || !$ap->isSuccessful ) &&
    (int)$_SESSION["${projectAcron ym}_kounter"] >= 1
    ) {
    foreach ($_SESSION as $field) if (strpos($field, $projectAcronym )
    === 0) unset($_SESSION[$field]);
    $qs = '?sort=' . $_REQUEST['sort'] . '&willDesc=' .
    $_REQUEST['willDesc'] . '&willShowDetai l=1&id=' . $_REQUEST['id'];
    $errorMsg = "<p><font color=\"#cc0000 \"><b>Applicati on display
    restarted due to too many errors, all values reset</b></font></p>";
    $qs .= '&errorMsg=' . urlencode($erro rMsg);
    header('Locatio n: ' . $_SERVER['PHP_SELF'] . $qs);
    } elseif (is_array($_POS T) && @sizeof($_POST) > 0 &&
    is_object($acce pter) && @is_a($accepter , 'Accepter') && is_object($ap)
    && @is_a($ap, 'ActionPerforme r') &&
    (!$accepter->isValid || !$ap->isSuccessful ) &&
    (int)$_SESSION["${projectAcron ym}_kounter"] >= 0
    ) {
    if ((int)($_SESSIO N["${projectAcron ym}_kounter"]) > 0)
    (int)$_SESSION["${projectAcron ym}_kounter"]++; else
    $_SESSION["${projectAcron ym}_kounter"] = 1;
    }
    print_r("After: ");
    print_r($_SESSI ON["${projectAcron ym}_kounter"]); print_r("<P>");
    //--END OF "Three Strikes You're Out"
    RULE------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    [/PHP]

    Problem is that the session counter never advances beyond 1, it remains
    1 indefinitely unless you do not do a form action then it's null again.

    It's supposed to go to 1 if you make one mistake, to 2 if you make
    another, and so on until you reach the limit and then it auto resets
    while destroying all SESSION objects whose keys are stamped to this
    particular project alone.

    Help!

    Thanx
    Phil

  • Oli Filth

    #2
    Re: &quot;Three Strikes You're Out&quot; rule fails

    comp.lang.php said the following on 27/03/2006 16:55:[color=blue]
    > print_r("Before : "); print_r($_SESSI ON["${projectAcron ym}_kounter"]);
    > print_r("<P>");
    > if (is_array($_POS T) && @sizeof($_POST) > 0 && is_object($acce pter)
    > && @is_a($accepter , 'Accepter') && is_object($ap) && @is_a($ap,
    > 'ActionPerforme r') &&
    > (!$accepter->isValid || !$ap->isSuccessful ) &&
    > (int)$_SESSION["${projectAcron ym}_kounter"] >= 1
    > ) {
    > foreach ($_SESSION as $field) if (strpos($field, $projectAcronym )
    > === 0) unset($_SESSION[$field]);
    > $qs = '?sort=' . $_REQUEST['sort'] . '&willDesc=' .
    > $_REQUEST['willDesc'] . '&willShowDetai l=1&id=' . $_REQUEST['id'];
    > $errorMsg = "<p><font color=\"#cc0000 \"><b>Applicati on display
    > restarted due to too many errors, all values reset</b></font></p>";
    > $qs .= '&errorMsg=' . urlencode($erro rMsg);
    > header('Locatio n: ' . $_SERVER['PHP_SELF'] . $qs);
    > } elseif (is_array($_POS T) && @sizeof($_POST) > 0 &&
    > is_object($acce pter) && @is_a($accepter , 'Accepter') && is_object($ap)
    > && @is_a($ap, 'ActionPerforme r') &&
    > (!$accepter->isValid || !$ap->isSuccessful ) &&
    > (int)$_SESSION["${projectAcron ym}_kounter"] >= 0
    > ) {
    > if ((int)($_SESSIO N["${projectAcron ym}_kounter"]) > 0)
    > (int)$_SESSION["${projectAcron ym}_kounter"]++; else
    > $_SESSION["${projectAcron ym}_kounter"] = 1;
    > }
    > print_r("After: ");
    > print_r($_SESSI ON["${projectAcron ym}_kounter"]); print_r("<P>");
    > //--END OF "Three Strikes You're Out"[/color]

    That's some of the most unreadable code I've ever seen.


    --
    Oli

    Comment

    • David Haynes

      #3
      Re: &quot;Three Strikes You're Out&quot; rule fails

      Oli Filth wrote:[color=blue]
      > comp.lang.php said the following on 27/03/2006 16:55:[color=green]
      >> print_r("Before : "); print_r($_SESSI ON["${projectAcron ym}_kounter"]);
      >> print_r("<P>");
      >> if (is_array($_POS T) && @sizeof($_POST) > 0 && is_object($acce pter)
      >> && @is_a($accepter , 'Accepter') && is_object($ap) && @is_a($ap,
      >> 'ActionPerforme r') &&
      >> (!$accepter->isValid || !$ap->isSuccessful ) &&
      >> (int)$_SESSION["${projectAcron ym}_kounter"] >= 1
      >> ) {
      >> foreach ($_SESSION as $field) if (strpos($field, $projectAcronym )
      >> === 0) unset($_SESSION[$field]);
      >> $qs = '?sort=' . $_REQUEST['sort'] . '&willDesc=' .
      >> $_REQUEST['willDesc'] . '&willShowDetai l=1&id=' . $_REQUEST['id'];
      >> $errorMsg = "<p><font color=\"#cc0000 \"><b>Applicati on display
      >> restarted due to too many errors, all values reset</b></font></p>";
      >> $qs .= '&errorMsg=' . urlencode($erro rMsg);
      >> header('Locatio n: ' . $_SERVER['PHP_SELF'] . $qs);
      >> } elseif (is_array($_POS T) && @sizeof($_POST) > 0 &&
      >> is_object($acce pter) && @is_a($accepter , 'Accepter') && is_object($ap)
      >> && @is_a($ap, 'ActionPerforme r') &&
      >> (!$accepter->isValid || !$ap->isSuccessful ) &&
      >> (int)$_SESSION["${projectAcron ym}_kounter"] >= 0
      >> ) {
      >> if ((int)($_SESSIO N["${projectAcron ym}_kounter"]) > 0)
      >> (int)$_SESSION["${projectAcron ym}_kounter"]++; else
      >> $_SESSION["${projectAcron ym}_kounter"] = 1;
      >> }
      >> print_r("After: ");
      >> print_r($_SESSI ON["${projectAcron ym}_kounter"]); print_r("<P>");
      >> //--END OF "Three Strikes You're Out"[/color]
      >
      > That's some of the most unreadable code I've ever seen.
      >
      >[/color]
      It's still obtuse if you do reformat it...
      My comments with '--' prefix.

      print_r("Before : ");
      print_r($_SESSI ON["${projectAcron ym}_kounter"]);
      print_r("<P>");

      if(
      is_array($_POST )
      && @sizeof($_POST) > 0
      && is_object($acce pter)
      && @is_a($accepter , 'Accepter')
      && is_object($ap)
      && @is_a($ap, 'ActionPerforme r')
      && ( ! $accepter->isValid || !$ap->isSuccessful )
      -- up to here the two clauses of the if are identical
      && (int)$_SESSION["${projectAcron ym}_kounter"] >= 1 ) {
      -- if we get here, i.e. kounter > 0, then we never increment it
      -- net result: the kounter goes from 0 in the second clause
      -- and stays at one here - which is the observed result.
      -- Also, the casts to int are not needed
      foreach( $_SESSION as $field)
      if( strpos($field, $projectAcronym ) === 0 )
      -- why ===? checking the return type of strpos for integer?
      unset($_SESSION[$field]);

      $qs = '?sort='.$_REQU EST['sort']
      .'&willDesc='.$ _REQUEST['willDesc']
      .'&willShowDeta il=1'
      .'&id='.$_REQUE ST['id'];
      $errorMsg = "<p><font color=\"#cc0000 \">"
      ."<b>Applicatio n display restarted due to too many errors, all values
      reset</b>"
      ."</font></p>";
      $qs .= '&errorMsg='.ur lencode($errorM sg);
      header('Locatio n: '.$_SERVER['PHP_SELF'].$qs);
      } elseif(
      is_array($_POST )
      && @sizeof($_POST) > 0
      && is_object($acce pter)
      && @is_a($accepter , 'Accepter')
      && is_object($ap)
      && @is_a($ap, 'ActionPerforme r')
      && ( ! $accepter->isValid || !$ap->isSuccessful )
      && (int)$_SESSION["${projectAcron ym}_kounter"] >= 0 ) {
      -- the previous if clause will trap on kounter >= 1, so this test
      -- should be == 0 if kounter has any chance of going negative
      -- otherwise it is useless.
      if ((int)($_SESSIO N["${projectAcron ym}_kounter"]) > 0)
      (int)$_SESSION["${projectAcron ym}_kounter"]++;
      else
      $_SESSION["${projectAcron ym}_kounter"] = 1;
      -- so we know that kounter is always zero, to only this line will
      -- be used, moving kounter to 1.
      }
      print_r("After: ");
      print_r($_SESSI ON["${projectAcron ym}_kounter"]);
      print_r("<P>");
      //--END OF "Three Strikes You're Out"

      -david-

      Comment

      • comp.lang.php

        #4
        Re: &quot;Three Strikes You're Out&quot; rule fails

        Read my comments below in **

        David Haynes wrote:[color=blue]
        > Oli Filth wrote:[color=green]
        > > comp.lang.php said the following on 27/03/2006 16:55:[color=darkred]
        > >> print_r("Before : "); print_r($_SESSI ON["${projectAcron ym}_kounter"]);
        > >> print_r("<P>");
        > >> if (is_array($_POS T) && @sizeof($_POST) > 0 && is_object($acce pter)
        > >> && @is_a($accepter , 'Accepter') && is_object($ap) && @is_a($ap,
        > >> 'ActionPerforme r') &&
        > >> (!$accepter->isValid || !$ap->isSuccessful ) &&
        > >> (int)$_SESSION["${projectAcron ym}_kounter"] >= 1
        > >> ) {
        > >> foreach ($_SESSION as $field) if (strpos($field, $projectAcronym )
        > >> === 0) unset($_SESSION[$field]);
        > >> $qs = '?sort=' . $_REQUEST['sort'] . '&willDesc=' .
        > >> $_REQUEST['willDesc'] . '&willShowDetai l=1&id=' . $_REQUEST['id'];
        > >> $errorMsg = "<p><font color=\"#cc0000 \"><b>Applicati on display
        > >> restarted due to too many errors, all values reset</b></font></p>";
        > >> $qs .= '&errorMsg=' . urlencode($erro rMsg);
        > >> header('Locatio n: ' . $_SERVER['PHP_SELF'] . $qs);
        > >> } elseif (is_array($_POS T) && @sizeof($_POST) > 0 &&
        > >> is_object($acce pter) && @is_a($accepter , 'Accepter') && is_object($ap)
        > >> && @is_a($ap, 'ActionPerforme r') &&
        > >> (!$accepter->isValid || !$ap->isSuccessful ) &&
        > >> (int)$_SESSION["${projectAcron ym}_kounter"] >= 0
        > >> ) {
        > >> if ((int)($_SESSIO N["${projectAcron ym}_kounter"]) > 0)
        > >> (int)$_SESSION["${projectAcron ym}_kounter"]++; else
        > >> $_SESSION["${projectAcron ym}_kounter"] = 1;
        > >> }
        > >> print_r("After: ");
        > >> print_r($_SESSI ON["${projectAcron ym}_kounter"]); print_r("<P>");
        > >> //--END OF "Three Strikes You're Out"[/color]
        > >
        > > That's some of the most unreadable code I've ever seen.
        > >
        > >[/color]
        > It's still obtuse if you do reformat it...
        > My comments with '--' prefix.
        >
        > print_r("Before : ");
        > print_r($_SESSI ON["${projectAcron ym}_kounter"]);
        > print_r("<P>");
        >
        > if(
        > is_array($_POST )
        > && @sizeof($_POST) > 0
        > && is_object($acce pter)
        > && @is_a($accepter , 'Accepter')
        > && is_object($ap)
        > && @is_a($ap, 'ActionPerforme r')
        > && ( ! $accepter->isValid || !$ap->isSuccessful )
        > -- up to here the two clauses of the if are identical[/color]

        ** How? I don't see how they're identical in any way, they're
        completely distinctive
        [color=blue]
        > && (int)$_SESSION["${projectAcron ym}_kounter"] >= 1 ) {
        > -- if we get here, i.e. kounter > 0, then we never increment it
        > -- net result: the kounter goes from 0 in the second clause
        > -- and stays at one here - which is the observed result.
        > -- Also, the casts to int are not needed[/color]

        ** I don't follow you here. Sorry, elaborate more, please. And I've
        had no luck without casts using $_SESSION objects
        [color=blue]
        > foreach( $_SESSION as $field)
        > if( strpos($field, $projectAcronym ) === 0 )
        > -- why ===? checking the return type of strpos for integer?[/color]

        ** I am checking to see if the $_SESSION key starts with
        "$projectAcrony m", isn't that what strpos() does?
        [color=blue]
        > unset($_SESSION[$field]);
        >
        > $qs = '?sort='.$_REQU EST['sort']
        > .'&willDesc='.$ _REQUEST['willDesc']
        > .'&willShowDeta il=1'
        > .'&id='.$_REQUE ST['id'];
        > $errorMsg = "<p><font color=\"#cc0000 \">"
        > ."<b>Applicatio n display restarted due to too many errors, all values
        > reset</b>"
        > ."</font></p>";
        > $qs .= '&errorMsg='.ur lencode($errorM sg);
        > header('Locatio n: '.$_SERVER['PHP_SELF'].$qs);
        > } elseif(
        > is_array($_POST )
        > && @sizeof($_POST) > 0
        > && is_object($acce pter)
        > && @is_a($accepter , 'Accepter')
        > && is_object($ap)
        > && @is_a($ap, 'ActionPerforme r')
        > && ( ! $accepter->isValid || !$ap->isSuccessful )
        > && (int)$_SESSION["${projectAcron ym}_kounter"] >= 0 ) {
        > -- the previous if clause will trap on kounter >= 1, so this test
        > -- should be == 0 if kounter has any chance of going negative
        > -- otherwise it is useless.
        > if ((int)($_SESSIO N["${projectAcron ym}_kounter"]) > 0)
        > (int)$_SESSION["${projectAcron ym}_kounter"]++;
        > else
        > $_SESSION["${projectAcron ym}_kounter"] = 1;
        > -- so we know that kounter is always zero, to only this line will
        > -- be used, moving kounter to 1.
        > }[/color]

        ** No it should not always be 0, it might be 0 but it could be 1, 2,
        .... N. It's supposed to be at the point where you made a mistake and
        it auto--increments.
        [color=blue]
        > print_r("After: ");
        > print_r($_SESSI ON["${projectAcron ym}_kounter"]);
        > print_r("<P>");
        > //--END OF "Three Strikes You're Out"
        >
        > -david-[/color]

        Comment

        Working...