A quick PHP function to get Post, Get, or Session variables; and Cookies too

Posted in Programming  
E-Mail This Post/Page   

It should be pretty easy to get $_GET, $_POST, $_SESSION, or $_COOKIE variables in PHP code. The problem is that just entering $_GET['variable'] causes an error if the variable does not exist. What's needed is a way to open the variable, and get a blank string if the variable is not defined.

That's what this function does. You pass a variable name and it searches through the get, post, session, and cookie arrays to see if it exists. If the variable doesn't exist in any of these arrays, a blank string is returned. More importantly, no error is tripped.

It checks in the order get, post, session, and cookie. So, if you have a cookie and a session variable named 'something', the value in $_SESSION['something'] will be returned.

PHP:
  1. function getvar($sVarName)
  2. /*  Returns the value of a passed variable either by GET or
  3. POST.  Will return a blank string if the variable does not
  4. exist in either method.
  5. Notes:  This function will check to see if the variable
  6. exists in the GET collection first.  If it does
  7. not then it will check the POST.  If the variable
  8. does not exist in either it will return a blank
  9. string.  Failing those two, it will check SESSION
  10. variables and then cookies.
  11. */
  12. {
  13. if (array_key_exists($sVarName, $_GET) == TRUE)
  14. {
  15. $temp = $_GET[$sVarName];
  16. }
  17. else if (array_key_exists($sVarName, $_POST) == TRUE)
  18. {
  19. $temp = $_POST[$sVarName];
  20. }
  21. else if (array_key_exists($sVarName, $_SESSION) == TRUE)
  22. {
  23. $temp = $_SESSION[$sVarName];
  24. }
  25. else if (array_key_exists($sVarName, $_COOKIE) == TRUE)
  26. {
  27. $temp = $_COOKIE[$sVarName];
  28. }
  29. else
  30. {
  31. $temp = "";
  32. }
  33.  
  34. //Return variable to calling routine
  35. return $temp;
  36. }

PHPInfo - Or, how to tell more about your server than you want to know
Benchmarking PHP: Inserting text with include(), require(), or get_file_contents()
Benchmarking PHP: Single versus Double Quotes
PayPal returning with ?=Return+To+Merchant
PHP function - directory_size()

4 Comments on “A quick PHP function to get Post, Get, or Session variables; and Cookies too”

Daniel on

There are a few things wrong with this code. Line 28, instead of looking in $_COOKIE, attempts to call a function $_SESSION. (And $_SESSION is, of course, actually an array rather than a function.) And no check is made as to whether there is a session, which could result in a failure at line 22.

Ryan on

You’re right about $_COOKIE. That’s a typo, that I just fixed, from when I came back and added those lines.

I don’t think you need to check for a session. If there’s not an active session than the isset() will catch that the variable isn’t set. Of course I may be wrong. What error would come up in case a session wasn’t started?

Daniel on

The diagnostic will be “Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in” followed by the file and line in which the call is attempted. So you want “else if (session_id() != “”) { if (array_key_exists($sVarName, $_SESSION) == TRUE) $temp = $_SESSION[$sVarName]; }” in place of the simpler code.

Ryan on

That’s right, of course, and now that I see it written out it’s pretty obvious.

Leave a Comment