Jump to content


Photo

Error with @parse_url


  • Please log in to reply
6 replies to this topic

#1 greg

greg
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 04 July 2006 - 04:53 PM

Hello,
I'm trying to validate a web page if this web page is from the same domain. I have used the following code, and it works perfect except when the web page to be validated is under a subdomain of the main domain.
For example:
domain: http://www.maindomain.com

if the page is located at: http://www.maindomain.com/page1.php --------- is validated
if the page is located at: http://subdomain.mai...n.com/page1.php ----- is not validated.

Could anyone help me on this please.
Thanks
Greg

if ($preventOtherDomains = 'yes')
			{
			$lparseurl = @parse_url($_POST['url']);
			$rparseurl = @parse_url($_POST['links_url']);
			if ($lparseurl['host'] !== $rparseurl['host']) 
			{
			$reciprocal_error_text_add = ' Must be from same domain.';
			$reciprocal_error = true;
			}
		    }





#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 04 July 2006 - 05:00 PM

I suggest you to leave only the "domain.com" part. In every check drop from both the http:// part and the www or subdomain part.

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 July 2006 - 05:03 PM

Its becuase parse_url includes the subdomain with the host name

So basically your if statment will be doing this:
if maindomain.com is not equal to subdomain.maindomain.com then echo "Must be from same domain".

To make this work you want to use eregi or use substr and get the last 14 characters from $rparseurl, which will be maindomain.com, then compare the last 14 chars with your $lparseurl['host'] variable.



#4 greg

greg
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 04 July 2006 - 05:11 PM

Thank you for your post wildteen88

The problem I see using the substract is that domains are not equal in lenght, so what happens when the domain is i.e. www.1234.com?
Thanks
Greg


#5 greg

greg
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 04 July 2006 - 05:13 PM

I suggest you to leave only the "domain.com" part. In every check drop from both the http:// part and the www or subdomain part.

Orio.

Orio,
thank you for your answer. That's what I was thinking, but I don't know how to get it. Could you please elaborate a little bit on this?
Thanks
Greg

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 04 July 2006 - 05:26 PM

You do something like this:

function drop_prefix($domain){
if(strstr(strstr($domain, "."),".")!==FALSE){ //check that there are at least two dots
$array=explode(".",$domain);
$array=array_shift($array);
$domain=implode(".",$array);
};
return $domain;
};
 

Note: this function will return "co.uk" for something like "domain.co.uk". But if it's "www.domain.co.uk", it'll return "domain.co.uk".
Oh, and if there are less then two dots, it returns the original value (so you can send something like domain.com and nothing will happen).

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 greg

greg
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 07 July 2006 - 10:37 PM

Orio,
Thank you very much. It works perfectly.
greg




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users