Skip to:
Content
Pages
Categories
Search
Top
Bottom

PHP Warning: parse_url

  • In the php_errorlog, there is a common warning, “PHP Warning: parse_url(…..”. This warning is caused within the bb_get_path function in bb-includes/functions.bb-core.php. The common cause of this issue is due to urls being passed to this function in the following format:

    /bb-login.php?re=http://example.com/tags.php?tag=sometag

    Due to the http:// within the query string, parse_url will fail and log an error. The way to get around this is to pre-pend the website address and http:// to the beginning of the url, such as:

    http://example.com/bb-login.php?re=http://example.com/tags.php?tag=sometag

    This format is valid with the parse_url function. To change your code to use this format, change the bb_get_path function in bb-includes/functions.bb-core.php to the following:

    function bb_get_path( $level = 1, $base = false, $request = false ) {

    if ( !$request )

    $request = $_SERVER;

    if ( is_string($request) )

    {

    // parse_url throws a warning for a url that has http:// as a query parameter and doesn’t start with http://

    $pos = strpos($url, “http://”);

    // Check if we found http:// in the string and it’s not at the beginning, and the url starts with a forward slash

    if ($pos !== false && $pos > 0 && substr($request,0,1) == “/”)

    {

    // We got here, so it’s not at the beginning and is in the url. Add in the domain to the beginning

    $oldRequest = $request; // Use a new variable since some PHP versions don’t like re-assigning

    $request = “http://”.$_SERVER["HTTP_HOST"].$oldRequest;

    }

    $request = parse_url($request);

    }

    if ( !is_array($request) || !isset($request) )

    return ”;

    $path = rtrim($request, ” tnrx0B/”);

    if ( !$base )

    $base = rtrim(bb_get_option(‘path’), ” tnrx0B/”);

    $path = preg_replace(‘|’ . preg_quote($base, ‘|’) . ‘/?|’,”,$path,1);

    if ( !$path )

    return ”;

    if ( strpos($path, ‘/’) === false )

    return ”;

    $url = explode(‘/’,$path);

    if ( !isset($url[$level]) )

    return ”;

    return urldecode($url[$level]);

    }

    Doing this will get rid of the common error in your error log, and prevent site issues.

    Note: This will need to be changed if your site is using SSL, such as https://

Viewing 2 replies - 1 through 2 (of 2 total)
  • I pasted the code with an error in it. Use this function instead:

    function bb_get_path( $level = 1, $base = false, $request = false ) {

    if ( !$request )

    $request = $_SERVER;

    if ( is_string($request) )

    {

    // parse_url throws a warning for a url that has http:// as a query parameter and doesn’t start with http://

    $pos = strpos($request, “http://”);

    // Check if we found http:// in the string and it’s not at the beginning, and the url starts with a forward slash

    if ($pos !== false && $pos > 0 && substr($request,0,1) == “/”)

    {

    // We got here, so it’s not at the beginning and is in the url. Add in the domain to the beginning

    $oldRequest = $request; // Use a new variable since some PHP versions don’t like re-assigning

    $request = “http://”.$_SERVER["HTTP_HOST"].$oldRequest;

    }

    $request = parse_url($request);

    }

    if ( !is_array($request) || !isset($request) )

    return ”;

    $path = rtrim($request, ” tnrx0B/”);

    if ( !$base )

    $base = rtrim(bb_get_option(‘path’), ” tnrx0B/”);

    $path = preg_replace(‘|’ . preg_quote($base, ‘|’) . ‘/?|’,”,$path,1);

    if ( !$path )

    return ”;

    if ( strpos($path, ‘/’) === false )

    return ”;

    $url = explode(‘/’,$path);

    if ( !isset($url[$level]) )

    return ”;

    return urldecode($url[$level]);

    }


    amandastock
    Member

    @amandastock

    This works perfectly. Thank you for posting.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.