Jump to content

Archived

This topic is now archived and is closed to further replies.

greg

Error with @parse_url

Recommended Posts

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.maindomain.com/page1.php ----- is not validated.

Could anyone help me on this please.
Thanks
Greg

[code]
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;
}
    }
[/code]



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 "[i]Must be from same domain[/i]".

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote author=Orio link=topic=99426.msg391513#msg391513 date=1152032413]
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.
[/quote]
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

Share this post


Link to post
Share on other sites
You do something like this:

[code=php:0]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;
};[/code]
[hr]

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.

Share this post


Link to post
Share on other sites
Orio,
Thank you very much. It works perfectly.
greg

Share this post


Link to post
Share on other sites

×

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.