ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

is_working_url()

February 22, 2009 10:17am

Subscribe [4]
  • #1 / Feb 22, 2009 10:17am

    TheFuzzy0ne

    4848 posts

    Thought I’d share this function with everyone. It actually checks to see if the link is valid.

    This version I keep in my own URL helper, and it uses prep_url().

    function is_working_url($url)
    {
        $url = prep_url($url);
    
        $header_arr = @get_headers($url);
    
        if (is_array($header_arr) && preg_grep('/HTTP\/\d+\.\d+ 200 OK/', $header_arr))
        {
            return TRUE;
        }
        
        return FALSE;
        
    }

    This version is a standalone version, which is ideal for those who don’t have the URL helper loaded, or don’t want to check if it is.

    function is_working_url($url)
    {
        if (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://')
        {
            $url = 'http://'.$url;
        }
    
        $header_arr = @get_headers($url);
    
        if (is_array($header_arr) && preg_grep('/HTTP\/\d+\.\d+ 200 OK/', $header_arr))
        {
            return TRUE;
        }
        
        return FALSE;
        
    }

    I find the functions very useful for form validation.

  • #2 / Feb 22, 2009 10:25am

    Johan André

    412 posts

    Great! Really useful!

  • #3 / Feb 22, 2009 10:01pm

    Simple and effective, thanks for sharing.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases