How can I parse_url in PHP when there is a URL in a string variable?

Posted by Eric O on Stack Overflow See other posts from Stack Overflow or by Eric O
Published on 2010-04-23T19:14:13Z Indexed on 2010/04/23 19:33 UTC
Read the original article Hit count: 168

Filed under:
|
|
|
|

I am admittedly a PHP newbie, so I need some help.

I am creating a self-designed affiliate program for my site and have the option for an affiliate to add a SubID to their link for tracking. Without having control over what is entered, I have been testing different scenarios and found a bug when a full URL is entered (i.e. "http://example.com").

In my PHP I can grab the variable from the string no problem. My problem comes from when I get the referring URL and parse it (since I need to parse the referring URL to get the host mane for other uses). Code below:

$refURL = getenv("HTTP_REFERER"); $parseRefURL = parse_url($refURL);

WORKS when incoming link is (for example):

http://example.com/?ref=REFERRER'S-ID&sid=www.test.com

ERROR when incoming link is (notice the addition of "http://" after "sid="):

http://example.com/?ref=REFERRER'S-ID&sid=http://www.test.com

Here is the warning message:

Warning: parse_url(/?ref=REFERRER'S-ID&sid=http://www.test.com) [function.parse-url]: Unable to parse url in /home4/'directory'/public_html/hosterdoodle/header.php on line 28

Any ideas on how to keep the parse-url function from being thrown off when someone may decide to place a URL in a variable? (I actually tested this problem down to the point that it will throw the error with as little as ":/" in the variable)

© Stack Overflow or respective owner

Related posts about php

Related posts about parsing