PHP Web Functions (Redirect, Get Current Full Url, Get Current Url Without Query String, Get POST Or GET Value, Is Ajax Request, Get User IP Address, Html Encode)

12/8/2013 4:13:10 AM

A series of PHP functions to make web development easier.

  • Redirect
  • Get Current Full Url
  • Get Current Url Without Query String
  • Get POST Or GET Value
  • Is Ajax Request
  • Get User IP Address
  • Html Encode
class WebFunctions
{
	static function HtmlEncode($value)
	{
		return htmlentities($value);
	}
	
	
    static function Redirect($url)
    {
        header('Location:  ' . $url);
        exit;
    }


    static function GetCurrentFullUrl()
    {
        return $_SERVER["REQUEST_URI"];
    }


    static function GetCurrentUrlWithoutQueryString()
    {
        return $_SERVER["PHP_SELF"];
    }


    static function GetPostGetValue($key)
    {
        try
        {
            if (isset($_POST[$key]))
            {
                return $_POST[$key];
            }
            else if (isset($_GET[$key]))
            {
                return $_GET[$key];
            }
        }
        catch(Exception $e)
        {

        }

        return null;
    }


    static function IsAjaxRequest()
    {
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
        {
            return true;
        }

        return false;
    }


    static function GetUserIPAddress()
    {
        try
        {
            return $_SERVER['REMOTE_ADDR'];
        }
        catch (Exception $e)
        {
        }

        return null;
    }
}