steviez Posted April 30, 2011 Share Posted April 30, 2011 Hi, I am making a small URL service for my website and want to check if the user is trying to shorten my sites domain name so i can stop this from happening. I am thinking I would need to use PHP's preg_match function but dont quite know how to check for the above. Any help would be great on this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/ Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 I'm thinking something like this is what you are after <?php $url = "http://www.mysite.com/folder/script.php"; function checkSameDomain($url) { $domain = "mysite.com"; if (preg_match("/$domain/i", $url)) { return true; } else { return false; } } //usage if(checkSameDomain($url) == true) { echo "same domain"; } else { echo "different domain"; } ?> I was a little foggy as to your exact question. Do you mean you need to check if some other site is using your domain in some sort of shortening url already? If that is the case then use curl and follow any redirects..then use the checkSameDomain function. Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1208848 Share on other sites More sharing options...
steviez Posted May 1, 2011 Author Share Posted May 1, 2011 thats sort of what im looking for although the code you gave me does not work. Basicly if a user is on my site (www.mysite.com) and they try to shorten my sites domain (www.mysite.com) instead of an external site then it will return false. The script will work similar to tinyurl Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209068 Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 Then use it this way. function checkSameDomain($url) { $domain = "mysite.com"; if (preg_match("/$domain/i", $url)) { return false; } else { return true; } } Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209073 Share on other sites More sharing options...
steviez Posted May 1, 2011 Author Share Posted May 1, 2011 Then use it this way. function checkSameDomain($url) { $domain = "mysite.com"; if (preg_match("/$domain/i", $url)) { return false; } else { return true; } } Thats the same code cant seem to get that to work. Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209074 Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 The function is about as simple as can get as far as functions go. It surely works, do you have a snippet of code and how are trying to use this? If you post that here it would most likely help. Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209093 Share on other sites More sharing options...
steviez Posted May 1, 2011 Author Share Posted May 1, 2011 The function is about as simple as can get as far as functions go. It surely works, do you have a snippet of code and how are trying to use this? If you post that here it would most likely help. Here is my code: public function is_base_domain($url) { // set base domain $base = $this->CI->core->config['base_url']; // http://www.mysite.com/ // match if(preg_match('/$base/i', $url)){ return TRUE; }else{ return FALSE; } } Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209103 Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 I can't run that function. Try this instead <?php function is_base_domain($url) { //get the server name $base= $_SERVER['SERVER_NAME']; // match if(preg_match("/$base/i", $url)){ return TRUE; } else { return FALSE; } } $url= "http://somesite.com/script.php"; $check = is_base_domain($url); if($check == TRUE){ echo "same domain"; }else{ echo "different domain"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209152 Share on other sites More sharing options...
steviez Posted May 1, 2011 Author Share Posted May 1, 2011 still no joy, its letting it go ahead Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209154 Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 May have to switch it around, I have to true if is found Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209156 Share on other sites More sharing options...
steviez Posted May 1, 2011 Author Share Posted May 1, 2011 Om using TRUE if the url is found to be the same... this is very strange how it wont work Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209161 Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 Indeed, can clearly see the example works on it's own. So that leaves me to believe it's the way you are checking it. Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209164 Share on other sites More sharing options...
steviez Posted May 1, 2011 Author Share Posted May 1, 2011 to check im doing this: if(is_base_domain('http://www.mysite.com/') == TRUE){ // do function} Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209165 Share on other sites More sharing options...
QuickOldCar Posted May 1, 2011 Share Posted May 1, 2011 Hmmm shouldn't you be inserting the value of whateverr url to be checked is? $the_url_to_check = "http://adifferentsite.com/"; if(is_base_domain($the_url_to_check) == TRUE){ // do function} Now whether you need to check for true or false that would depend upon what your do function is gonna do. Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209172 Share on other sites More sharing options...
steviez Posted May 2, 2011 Author Share Posted May 2, 2011 Fixed it for some reason I had to do this: if(preg_match('/'.$base.'/i', $url)){ return TRUE; }else{ return FALSE; } Thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/235213-urlcheck-help/#findComment-1209255 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.