Page 1 of 1

page sequencer verifying function?

Posted: Fri Nov 07, 2008 3:22 am
by bugpanye
hi all... its my first post here,
anyhows i written this piece of code.. which reads the mode in the current URL,
and checks it with a sequence array to make sure you didnt jump the step before...

i know they could be certain loopholes here but.. after hours of coding my head is abit woozy.. can someone help me out and check the problems? :crazy:

Code: Select all

 
<?php
    //
    $sequence = array(
        'postjob'=>array('postjob','postjob2','postjob3','postjob4','postjob5','postjob6')
    );
 
    
    foreach ($sequence as $key=>$seq)
    {
        if (strpos($_GET['mode'],$key))
        {
            //start the sequence verification
            for ($i = 0; $i < sizeof($seq); $i++)
            {
                //check for current mode
                if ($_GET['mode']==$seq[$i])
                {
                    //match your mode with the previous mode
                    if ($seq[$i-1] == $_SESSION['previous_mode'] AND $i > 0)
                    {
                        //if matched, continue
                        
                    } elseif ($i > 0) {
                        //if not matched, but not first result
                        
                    }
                }
            }
        }
    }
?>
 

Re: page sequencer verifying function?

Posted: Fri Nov 07, 2008 4:55 am
by novice4eva
HI there, i have mode some modification to the array coz i thought deeper array are not required for your purpose: but i may be wrong. Here is what i did -

Code: Select all

 
    session_start();
    $sequence = array('postjob','postjob2','postjob3','postjob4','postjob5','postjob6');
    $matchFound = false;
        $mode = $_GET['mode'];
    
    if(!(strpos($mode,'postjob')===false))
    {
        foreach ($sequence as $key=>$seq)
        {
            //check for current mode
            if ($mode==$seq)
            {
                //match your mode with the previous mode
                if (($key==0) || ($sequence[($key-1)] == $_SESSION['previous_mode']))
                {
                    $_SESSION['previous_mode'] = $seq;
                    //if matched, continue
                    echo 'You have followed the sequence'.$key;
                    $matchFound = true;
                }
            }
            if($matchFound){break;}
        }
        if(!$matchFound)
        {
             //if not matched, do what needs to be done
             echo 'You have not followed the sequence';
        }
    }
 
The only "wrong" thing in your code was this

Code: Select all

 
if (strpos($_GET['mode'],$key))
 
That may return 0 if the match is found from the first char rendering as false for if condition, you wouldn't want that :wink: and added the break conditions to just avoid needless cycles of processes!