Server Request Validation

Discussion in 'PHP' started by pein87, Jun 6, 2012.

  1. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    This functions solves the problem of always writing the same code to check if a get or post request is set or not.

    Background



    I created this because I was tired of the extra code needed just to see if a request variable has the array key set or not. I don't want to use error suppression because those errors come in handy so I created this. This does not use request super global only get or post. Gone are the days of writing a bunch of if is set pairs in your php code. ternary if statement for variable assignment.

    The Code



    PHP:
    <?php

    function isRequest($name,$type,$val NULL)
    {
        
    strtoupper($type); // convert type value to all uppercase for switch statement
        
        
    $returnVal// create variable that is not initiated yet but holds boolean values casted as numerics
        
        
    switch($type// check if $type is either get or post request types
        
    {
            case 
    'GET'// is get perform actions below
                
                
    if(isset($_GET[$name])) // if get super global has been give the array key of $name
                
    {
                    if(!
    is_null($val)) //  get super global does have the array key of $name check if the value of $val is not empty; its not empty
                    
    {
                        if(
    $_GET[$name] == $val// $val is not empty check if the value for the get super global matches $val; matches return 1 for true
                        
    {
                            
    $returnVal 1;
                        }
                        else 
    // doesn't match return 0 for false
                        
    {
                            
    $returnVal 0;    
                        }
                    } 
    // value for $val is empty but get super global has an array key of $name; return 1 for true
                    
    else
                    {
                        
    $returnVal 1;
                    }
                } 
    // get super global doesn't have an array key of
                
    else
                {
                    
    $returnVal 0;
                }
                
            break;
            
            case 
    'POST'// check if $type is post and perform actions below
            
                
    if(isset($_POST[$name])) // check if post super global has array key of $name; it does return 1 for true
                
    {
                    
                    
    $returnVal 1;
                }
                else 
    // it doesn't return 0 for false
                
    {
                    
    $returnVal 0;    
                }
                    
            break;
            
            default: 
    // $type value does not match our type list set $returnVal to 0 or false
                
    $returnVal 0;
            break;
        }
        return 
    $returnVal// give response of boolean back from operation
    }

    ?>
    Sample usage( add get variable to your url like so http://localhost/test.php?userid=23)

    PHP:
    $uID isRequest('userid','GET') ? $uID $_GET['userid'] : 0;

    echo 
    $uID;
    The variable now has the value of 23 and you can sanitize that data and do type checking too.
     
  2. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    I made some corrections or advances? Hmm... really I don't know what to call it. I have been .NET'ing for a couple of days so I changed it so it assigns the value of the global based on if its get or post with the ability to get the type it is. If your asking for the type it will return and array. index 0 is the value and index 1 is the type. If its neither or its empty the return value is null.

    PHP:
    function request($val,$rtntyp false)
    {
        
    $rtnVal;
            
        if(isset(
    $_GET[$val]) && !empty($_GET[$val]))
        {
            switch(
    $rtntyp)
            {
                case 
    false:
                    
    $rtnVal $_GET[$val];
                break;
                
                case 
    true:
                    
    $rtnVal[0] = $_GET[$val];
                    
    $rtnVal[1] = "GET";
                default:
                    
    $rtnVal $_GET[$val];
                break;
            }
        }
        elseif(isset(
    $_POST[$val]) && !empty($_POST[$val]))
        {
            switch(
    $rtntyp)
            {
                case 
    false:
                    
    $rtnVal $_POST[$val];
                break;
                
                case 
    true:
                    
    $rtnVal[0] = $_POST[$val];
                    
    $rtnVal[1] = "GET";
                default:
                    
    $rtnVal $_POST[$val];
                break;
            }
        }
        else
        {
            
    $rtnVal NULL;
        }
            
        return 
    $rtnVal;
    }
    Sample usage

    PHP:
    $uid request("uid");
    if(
    $uid !== null || $uid !== "")
    {
        echo 
    $uid;
    }
    else
    {
        echo 
    "no value set";
    }
    I like this version better because you just need to see if there is a value for the variable where you can perform sanitation of the data and type checking. I figure most don't care so long as they can perform the needed actions on it but for integrity purposes I did add type capturing in case you want to know what type it is. Inspired by .NET's request. I would have made it a class but I can't think of any additional method or properties to add to it so it wouldn't serve a point to have a class with one method.
     
    Last edited: Jun 11, 2012
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice