greg Posted July 4, 2006 Share Posted July 4, 2006 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.comif the page is located at: http://www.maindomain.com/page1.php --------- is validatedif the page is located at: http://subdomain.maindomain.com/page1.php ----- is not validated.Could anyone help me on this please.ThanksGreg[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] Quote Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/ Share on other sites More sharing options...
Orio Posted July 4, 2006 Share Posted July 4, 2006 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 Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/#findComment-52949 Share on other sites More sharing options...
wildteen88 Posted July 4, 2006 Share Posted July 4, 2006 Its becuase parse_url includes the subdomain with the host nameSo 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. Quote Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/#findComment-52950 Share on other sites More sharing options...
greg Posted July 4, 2006 Author Share Posted July 4, 2006 Thank you for your post wildteen88The 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?ThanksGreg Quote Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/#findComment-52955 Share on other sites More sharing options...
greg Posted July 4, 2006 Author Share Posted July 4, 2006 [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?ThanksGreg Quote Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/#findComment-52956 Share on other sites More sharing options...
Orio Posted July 4, 2006 Share Posted July 4, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/#findComment-52964 Share on other sites More sharing options...
greg Posted July 7, 2006 Author Share Posted July 7, 2006 Orio,Thank you very much. It works perfectly.greg Quote Link to comment https://forums.phpfreaks.com/topic/13656-error-with-parse_url/#findComment-54580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.