How does facebook do this?

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
It first started with this: http://forums.x10hosting.com/programming-help/100402-how-does-facebook-do.html

Misson was able to show me how to do it using isset(), but making a new if statement for each 'parameter' was very tiring and confusing. But misson also mentioned using array_keys()

I tried this out:
PHP:
print_r(array_keys($_REQUEST));
and got this
Code:
Array (     [0] => compose [1] => account_php?compose     [2] => start )
so I tried calling upon, $_REQUEST[0], but that did nothing.

So does anyone have a solution that I can use that I would not need to make a new if statement for each parameter?
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
I think you have to use $_GET(REQUEST_URI)

and use explode to get string by string.

As registered globals are off.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Exactly what do you want to do?
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
http://www.facebook.com/inbox/?compose&ref=mb
how does FB pass "compose" in php?
because the variable compose is not set to anything.

misson showed me how to do it using isset(), but that would mean I would have to create a new if statement for each one I want to pass. I want a way so that I can just find out what its looking for and include the necessary files for it.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
   # legal options
$permitted = array( 'compose' , 'destroy', 'giggle', 'whistle' );
   # all options sent
$submitted = array_keys( $_GET ) ;
   # legal options sent
$ok_submitted = array_intersect( $permitted , $submitted ) ;

foreach( $ok_submitted as $option ){

  // Do something with $option
}
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
PHP:
   # legal options
$permitted = array( 'compose' , 'destroy', 'giggle', 'whistle' );
   # all options sent
$submitted = array_keys( $_GET ) ;
   # legal options sent
$ok_submitted = array_intersect( $permitted , $submitted ) ;

foreach( $ok_submitted as $option ){

  // Do something with $option
}
thank you!

another thing popped up. what happens if some sob comes and tries to use:
site.php?calendar&staff

I need a fail-safe so that it only executes the first command. but as far as i know foreach does not have a way to stop it, unlike while().
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
thank you!

another thing popped up. what happens if some sob comes and tries to use:
site.php?calendar&staff

I need a fail-safe so that it only executes the first command. but as far as i know foreach does not have a way to stop it, unlike while().

If you want just one option executed:

PHP:
   # legal options
$permitted = array( 'compose' , 'destroy', 'giggle', 'whistle' );
   # all options sent
$submitted = array_keys( $_GET ) ;
   # legal options sent
$ok_submitted = array_intersect( $permitted , $submitted ) ;
 
## INSERT A TEST TO MAKE SURE AT LEAST ONE OPTION
 
foreach( $ok_submitted as $option ){
 
  // Do something with $option
 
  break ;  # STOP AFTER ONE
}

Does not guarantee which will be executed.

If you list the options in order you want to perform, then something like


PHP:
   # legal options
$permitted = array( 'compose' , 'destroy', 'giggle', 'whistle' );
   # all options sent
$submitted = array_keys( $_GET ) ;
   # legal options sent
 
$found_option = false ;
 
foreach( $permitted as $option ){
 
    if( in_array(  $option , $submitted ) ){
        # DO SOMETHING WITH $option
        $found_option = true ;
        break;
    }
 
} 
 
# HANDLE CASE WHERE $found_option is false
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
thank again

PHP:
# legal options
$permitted = array( 'compose' , 'destroy', 'giggle', 'whistle' );
   # all options sent
$submitted = array_keys( $_GET ) ;
   # legal options sent
$ok_submitted = array_intersect( $permitted , $submitted ) ;
 
$numOptionSel = count($ok_submitted);
if($numOptionSel == 1) {
   foreach($ok_submitted as $option) {
   echo $option;
   }
   echo 'working...';
} else {
   echo 'Stop messing around with my script!';
}

I just need to add some more fail-safes and errors, but thank you for your help
+REP
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Also, provide a default option that is displayed when no option is specified.
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
Also, provide a default option that is displayed when no option is specified.

good point.
this is what I got
PHP:
# legal options 
$permitted = array( 'compose' , 'destroy', 'giggle', 'whistle' ); 
   # all options sent 
$submitted = array_keys( $_GET ) ; 
   # legal options sent 
$ok_submitted = array_intersect( $permitted , $submitted ) ; 
  
$numOptionSel = count($ok_submitted); 
if($numOptionSel == 1) { 
   foreach($ok_submitted as $option) { 
   echo $option; 
   } 
   echo 'working...'; 
} elseif($numOptionSel == 0) {
   # if $submitted is not in $permitted
   echo 'Invalid Error Performed.';
} else { 
   echo 'Stop messing around with my script!'; 
}
how would I add a if statement for no parameter being passed?
right now, "} elseif($numOptionSel == 0) {" is picking it up
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Here's yet another way of handling multiple options and setting a default:

PHP:
$permitted = array( 'calendar' , 'staff', 'news', 'profile' );
$submitted = array_keys( $_GET ) ;
# add default action of 'compose'
$submitted[] = 'news';
//either:
# legal options sent
$ok_submitted = array_values(array_intersect( $permitted , $submitted )) ;
# take the first action, ignore the rest
$action = $ok_submitted[0];
//or:
list($i, $ok_submitted) = each(array_intersect( $permitted , $submitted ));

If you don't want to allow a visitor to specify more than one option in the query string, error on count($ok_submitted > 2).
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
Here's yet another way of handling multiple options and setting a default:

PHP:
$permitted = array( 'calendar' , 'staff', 'news', 'profile' );
$submitted = array_keys( $_GET ) ;
# add default action of 'compose'
$submitted[] = 'news';
//either:
# legal options sent
$ok_submitted = array_values(array_intersect( $permitted , $submitted )) ;
# take the first action, ignore the rest
$action = $ok_submitted[0];
//or:
list($i, $ok_submitted) = each(array_intersect( $permitted , $submitted ));
If you don't want to allow a visitor to specify more than one option in the query string, error on count($ok_submitted > 2).

it seems to not be working for me.
PHP:
$permitted = array('general', 'calendar', 'staff', 'events', 'history');
$submitted = array_keys( $_GET ) ;
# add default action of 'compose'
$submitted[] = 'general';
//either:
# legal options sent
$ok_submitted = array_values(array_intersect( $permitted , $submitted )) ;
# take the first action, ignore the rest
$action = $ok_submitted[0];

echo $action;
echo '<br />';
echo 'working';

when I add in:
PHP:
$submitted[] = 'general';
it overides all other settings
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Reorder the first array


PHP:
$permitted = array( 'calendar', 'staff', 'events', 'history' , 'general' );
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Your default action should be the last one.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Whoops... I had $permitted and $submitted in the wrong order in the call to array_intersect(...). It preserves the element order for the first argument. The line should have read:
PHP:
$ok_submitted = array_values(array_intersect( $submitted, $permitted )) ;
Calls to var_dump() (such as var_dump($ok_submitted);) are very useful in a case like this.
Edit:
Whoops... I had $permitted and $submitted in the wrong order in the call to array_intersect(...). It preserves the element order for the first argument. The line should have read:
PHP:
$ok_submitted = array_values(array_intersect( $submitted, $permitted )) ;
Calls to var_dump() (such as var_dump($ok_submitted);) are very useful in a case like this.

Just goes to show the importance of testing, which I neglected for once.
 
Last edited:

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
Sorry for digging up this thread.
But I've never said thank you to you guys yet and gratitude is due.

so thank you.
 
Top