dombrorj Posted February 6, 2011 Share Posted February 6, 2011 Hi, Hoping someone can help me out with something that is probably really simple, but I can't seem to figure out. I'm storing referring URLs, and some of them are over 1,000 characters because of all the variables. How would I Get only the actual url and not all the variables, such as... http://www.google.com/ajax/search/start.php?eid=AAAAAwAgACAAAAH0v4jVmjrvBVRZxy6_CX4Rl8t6reioLF6htZb4qitx_2nR3dkfj7b8VJhn43Rc1JD0_Mn8S05zD1-Zg35FdE4rE6-cSmGB4Jrvebc3B6sFCY1vamkir_c--8ESJIzZeybl-22ddGIv4qP3JFtmaaLXWpOZSkyeSN17nD_I-vMnhsV8nY-gHeYTC4vqddkJQYI2bBSXP9Y0KHE189A1tFH9Cyprt4hpubL7KCKE2EPG4rErzSgnERqJMiqeLDtmdUs1Fe6e6nhaoE5bBim00LCnsfvQfYfX2wWkpglx2cZIASlb7H3AohF0amFZN2ZVeQBMhR7PsCNhhIXuuxa39-mxdjARVLqwpoQHXa2ODMu7jyNA7CO5pJ56F7vqtL2h9oDA-YlQ8BBgfXwKrmKaTKgWEU-mRqMeJ076B-JGPY6NjOg9Vkpe3KVhzh_L_rF4fz7kbQYKOgyGvmnsIEdkqlVCePqDOTvkKNMwOEdDW1LqYXIG3i3u21GGZAOqUslMnCix0sVIE09ENi-K6k75mvHxvTADiBXgPAw03BodR3I5hBfw3uFjkBKAfGJ1ir_03z5ec0Yz9RoSIOEYcZlvxeiTP-V4OnTz7n5eVYzD3o0c9mfyAYhLh4KY9LVxy88Pux2NSeIG3XAWextqYtPJhoIxWvO1qSp9tJBl_smywvlF2oZRQNCCbNNGeJ4BhdvmZ3OG7e0Jjm9FL4zEadh5pENY1A0lSInBN_reFiqe2iZh2_N8NX83Y51Tl0bn_Xrhf9SOI&c=4&f=0&ui=6002910216647-id_4d487a6db36b33126309761&en=1 I only need the info up to the "?" http://www.google.com/ajax/search/start.php My php code looks like: $referer = $_SERVER['HTTP_REFERER']; mysql_query("INSERT INTO `refs` (`referer`) VALUES ('$referer')"); Is there an easy way to handle this? Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/ Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 parse_url http://php.net/manual/en/function.parse-url.php Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1170678 Share on other sites More sharing options...
QuickOldCar Posted February 6, 2011 Share Posted February 6, 2011 Here's some code for parse_url http://www.phpfreaks.com/forums/php-coding-help/getting-the-path/msg1506898/#msg1506898 Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1170679 Share on other sites More sharing options...
dombrorj Posted February 6, 2011 Author Share Posted February 6, 2011 Ahh Thank You!! And I appreciate you linking me to your code QuickOldCar. Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1170682 Share on other sites More sharing options...
dombrorj Posted February 6, 2011 Author Share Posted February 6, 2011 Using parse_url, is there a better way to get the Host and Path of something like: http://site.com/folder1/folder2/page.html?var1=1&var2=2 I did this and is working, but not sure if this is the best way to go about it. Or is this good? $url = $_SERVER['HTTP_REFERER']; echo parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH); Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1170805 Share on other sites More sharing options...
QuickOldCar Posted February 8, 2011 Share Posted February 8, 2011 sorry for late reply, been busy here's a function to eliminate the query and any fragments off url's <?php function removeQuery($new_url) { if ($new_url != '') { $query_parse_url = parse_url(trim($new_url), PHP_URL_QUERY); $fragment_parse_url = parse_url($new_url, PHP_URL_FRAGMENT); $remove_query_and_fragment = str_replace(array("$query_parse_url","$fragment_parse_url"), '', $new_url); $trimmed_host_and_paths = rtrim($remove_query_and_fragment, '?,#,/'); return $trimmed_host_and_paths; } else { return "no url inserted"; } } ?> <?php //usage $url1 = "http://www.phpfreaks.com/forums/php-coding-help/?action=post"; $url2 = "www.phpfreaks.com/forums/php-coding-help/?action=post"; $url3 = "somesite.com/forum/location/#"; $url4 = "HTTP://WWW.PHPFREAKS.COM/forums/php-coding-help/?action=post&var=var/"; $example_1 = removeQuery($url1); $example_2 = removeQuery($url2); $example_3 = removeQuery($url3); $example_4 = removeQuery($url4); echo "$example_1<br />$example_2<br />$example_3<br />$example_4<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1171326 Share on other sites More sharing options...
dombrorj Posted February 8, 2011 Author Share Posted February 8, 2011 Hey, thanks again ThisOldCar. Works great. One question... what's the difference between using your code above and doing like I did? $url = $_SERVER['HTTP_REFERER']; echo parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH); Will your code cause less overhead? I'm learning slowly here, but getting there. Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1171477 Share on other sites More sharing options...
QuickOldCar Posted February 8, 2011 Share Posted February 8, 2011 Whatever works is fine, as long as it gets the job done the way you want it to. I just hack the end off , you show from the beginning. If start having port numbers, user names and passwords or anything other in the url would see the difference. I do it my way to have more control over it and also to be a function. this should be filtered $url = filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_STRING); Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1171492 Share on other sites More sharing options...
dombrorj Posted February 8, 2011 Author Share Posted February 8, 2011 That makes sense. Thanks for the reply and again for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/226888-how-to-get-just-referrer-url-and-not-all-the-variables/#findComment-1171493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.