Jump to content

URLCheck Help


steviez

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :confused: cant seem to get that to work.

Link to comment
Share on other sites

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; }	
}

 

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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.

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.