Page 1 of 2
How to find the base url
Posted: Sun Jan 15, 2006 9:23 am
by Extremest
I am having some problems. I need to take 2 urls and check to see if the base is the same. Meaning I need to take like
http://www.google.com and pretty much remove the www. Except that I need it to work even if there is no www. meaning if it was forums.devnetwork.net and being checked against
http://www.devnetwork.net it would say yes because the base is the same.
Posted: Sun Jan 15, 2006 9:31 am
by hawleyjr
Have you looked at the values the variables $_SERVER has?
Code: Select all
echo '<HR><PRE>'; print_r($_SERVER); echo '</PRE>';
Posted: Sun Jan 15, 2006 9:33 am
by hawleyjr
Oops, read the post wrong. Ignore my last post.
Use substring to compare the URL.
Code: Select all
if(substr('www.myDoamin.com', 0, 4) == 'www.'){
//DO SOMETHING
Posted: Sun Jan 15, 2006 11:30 am
by timvw
Altough it's not completely clear what you are trying to do.
A possible approach is to use strpos to determine the first occurence of a . (
http://www.example.org -> index = 4)
Then use this index to make substrings, so you keep example.org
Posted: Sun Jan 15, 2006 12:31 pm
by Extremest
I am checking urls to make sure they are in the same domain. as long as the base domain is the same. Trying to find the most effecienct way to do it.
Posted: Sun Jan 15, 2006 12:34 pm
by hawleyjr
Check out the function
parse_url
Posted: Sun Jan 15, 2006 12:39 pm
by Extremest
I am using that now. Yet for the host it will keep the www so it does not match. Trying to find a way to take the host from there and maybe scan it for the last . then go to the . before it or something not really sure how to do it.
Posted: Sun Jan 15, 2006 1:13 pm
by Extremest
Do yea guys think something like this would work ok.
Code: Select all
$url = "http://www.devnetwork.com";
$url1 = "http://forums.devnetwork.com";
$new = parse_url($url);
$new1 = parse_url($url1);
$new_c = strpos($new['host'],".");
$new_d = substr($new['host'],$new_c+"1");
if (stristr($new1['host'],$new_d) != false){
echo "We are at the devnetwork!!!";
}else{
echo "We are not at the same place";
}
Posted: Sun Jan 15, 2006 1:24 pm
by Extremest
ok that seems to be working pretty good. I have one other question. I do a select from my table of x amount of urls with a done value of 0. then while it is going through the result set I have it do an update for each url to change the done value to 1. It seems to work apache a lot harder now. Is there a way to maybe with an array of urls to do a batch update or something.
Posted: Sun Jan 15, 2006 1:35 pm
by timvw
There is a potential for problems with the first position of . approach
What happens if you have "i.am.example.org" and want to test if it is in the same domain as "example.org"?
Meaby it's better to test if the last parts (thus .example.org) of these strings are equal
If you have a dbms that supports subqueries:
Code: Select all
UPDATE table
SET something=otherthing
WHERE table_id IN (
SELECT table_id with criteria that you used to select from my table of x amount of urls with a done value of 0
)
Posted: Sun Jan 15, 2006 1:45 pm
by Extremest
I have mysql 5 I don't think it supports that. The base host will be what i put in. Will always be like
http://www.google.com or something to that effect.
Posted: Sun Jan 15, 2006 7:43 pm
by Extremest
would mssql be able to do it. If so is mssql faster than mysql?
Posted: Sun Jan 15, 2006 7:45 pm
by Jenk
Posted: Sun Jan 15, 2006 7:56 pm
by Extremest
I tried what he put down and mysql said that it did not support it yet.
Posted: Sun Jan 15, 2006 10:05 pm
by Extremest
ok I don't really know anything about mssql but I did manage to change my script over to use it. Although it does seem like it takes for ever to do what it already could do with mysql at a pretty nice speed.