Jump to content

Recommended Posts

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?

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!

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 />";
?>

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.  :D

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);

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.