Jump to content

Error with @parse_url


greg

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]



Link to comment
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.

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

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.