page sequencer verifying function?

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
bugpanye
Forum Newbie
Posts: 7
Joined: Fri Nov 07, 2008 3:11 am

page sequencer verifying function?

Post 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
                        
                    }
                }
            }
        }
    }
?>
 
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: page sequencer verifying function?

Post 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!
Post Reply