Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"if else statement" when no $_REQUEST exist

I am making a simple if and else statement to get value from a requested link my code is

if($_REQUEST['f_id']=='')
{
    $friend_id=0;
}
else
{
    $friend_id=$_REQUEST['f_id'];
}

and suppose the link is www.example.com/profile.php?f_id=3

now its simple as if the f_id is empty or with value either of the above if and else statement would run. but what is a user is just playing around with link and he removes the whole ?f_id=3 with link left to be opened with www.example.com/profile.php then how to detect that f_id dosen't exist and in that case redirect to a error page ?

like image 284
Param Veer Avatar asked Jul 30 '26 02:07

Param Veer


1 Answers

if ( isset( $_REQUEST['f_id'] ) ) {
    if($_REQUEST['f_id']=='') {
        $friend_id=0;
    } else {
        $friend_id=$_REQUEST['f_id'];
    }
} else {
    REDIRECT TO ERROR PAGE
}

UPDATE Since your URLS-s look like www.example.com/profile.php?f_id=3 you should use $_GET instead of $_REQUEST

like image 176
Zoltan Toth Avatar answered Jul 31 '26 17:07

Zoltan Toth