How to find the base url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

How to find the base url

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Have you looked at the values the variables $_SERVER has?

Code: Select all

echo '<HR><PRE>'; print_r($_SERVER); echo '</PRE>';
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Check out the function parse_url
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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";
}
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
)
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

would mssql be able to do it. If so is mssql faster than mysql?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

I tried what he put down and mysql said that it did not support it yet.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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.
Post Reply