timmah1 Posted August 14, 2009 Share Posted August 14, 2009 I have a script that checks if a reciprocal link exists or not. I then tried to do functions that will check the posted reciprocal link against all reciprocal links in the database. If the domain name is same, it won't allow the script to continue, I thought I had it working, but for some reason it allows 2 of the same domain in the database. This functions works like this: If the link is http://www.test.com/testing/andAgain/ the function strips everything and just shows test.com So if someone submits a reciprocal link as http://www.test.com/testing/andAgain/links.php, but they have already submitted a reciprocal link as http://www.test.com/testing/andAgain/newlinks/links.php, the function strips everything away and just shows test.com for both, and shouldn't allow it. Anyhow, here is the script if anybody can look at it and see what I've done wrong. <?php $reciprocal = (isset($_POST['r_url'])?$_POST['r_url']:''); function GetDomain($reciprocal) { $nowww = ereg_replace('www.','',$reciprocal); $domain = parse_url($nowww); if(!empty($domain["host"])) { return $domain["host"]; } else { return $domain["path"]; } } $sql1="SELECT r_url FROM exchange_links"; $result1=mysql_query($sql1) or die("Could not get link information."); while($g1=mysql_fetch_array($result1)) { $main_url = $g1['r_url']; } //Get Reciprocal Link in Database function Domain($main_url) { $nowww = ereg_replace('www.','',$main_url); $domain = parse_url($nowww); if(!empty($domain["host"])) { return $domain["host"]; } else { return $domain["path"]; } } $rec1 = GetDomain($reciprocal); $rec2 = Domain($main_url); if($rec1 == $rec2){ echo "This domain name is already listed"; } else { ?> Thanks in advance Quote Link to comment Share on other sites More sharing options...
benphelps Posted August 14, 2009 Share Posted August 14, 2009 Here is how I changed it up, not sure if it will work for your database scheme. <?php $reciprocal = (isset($_POST['r_url'])?$_POST['r_url']:''); function Domain($reciprocal){ $nowww = ereg_replace('www.','',$reciprocal); $domain = parse_url($nowww); if(!empty($domain["host"])){ return $domain["host"]; }else{ return $domain["path"]; } } $sql1 = "SELECT r_url FROM exchange_links WHERE domain = '$reciprocal'"; $result1 = mysql_query($sql1) or die("Could not get link information."); $count = mysql_num_rows($result1); if($count != 0){ echo "This domain name is already listed"; } else { } ?> Quote Link to comment Share on other sites More sharing options...
timmah1 Posted August 14, 2009 Author Share Posted August 14, 2009 Much cleaner than mine benphelps, but I get the same results The script does that check, and if it passes, then it checks if the reciprocal link is listed on the page they say, if not, it gives an error Right now, in the database has a reciprocal link as http://www.vegasdsports.com/main/ When I try and add another link like http://www.vegasdsports.com/main/links.php, it gives the error that the link cannot be found, so it passed through the check if the url's are the same Quote Link to comment Share on other sites More sharing options...
benphelps Posted August 14, 2009 Share Posted August 14, 2009 Check it against the domain and the domain only, if it still gets added, then its happening in other parts of the code. Quote Link to comment Share on other sites More sharing options...
kratsg Posted August 14, 2009 Share Posted August 14, 2009 I have a code that does something more of just storing the domain names of referrers into the database... <?php function getDomain($url) { if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE){//if this is an invalid url return false; } $parts = parse_url($url); return $parts['scheme'].'://'.$parts['host']; } ?> So, if I used: if(getDomain("http://www.vegasdsports.com/main/links.php") == getDomain("http://www.vegasdsports.com/main/")){return true;} else {return false;} This should always return true. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted August 14, 2009 Author Share Posted August 14, 2009 Thank you both for all your help. I finally got it work with this <?php // Checks posted reciprocal link // function GetDomain($reciprocal){ $nowww = ereg_replace('www.','',$reciprocal); $domain = parse_url($nowww); if(!empty($domain["host"])){ return $domain["host"]; }else{ return $domain["path"]; } } // Checks reciprocal link database // function Domain($main_url) { $nowww = ereg_replace('www.','',$main_url); $domain = parse_url($nowww); if(!empty($domain["host"])){ return $domain["host"]; } else { return $domain["path"]; } } $sql1="SELECT r_url FROM exchange_links"; $result1=mysql_query($sql1) or die("Could not get link information."); while($g1=mysql_fetch_array($result1)) { $main_url = $g1['r_url']; } if(GetDomain($reciprocal) == Domain($main_url)){ echo "This domain name is already listed<br />"; echo "You may only submit 1 link per domain<br />"; echo "You may <a href='http://www.basketballfreepicks.com/contact_us/'>contact us</a> about listing another reciprocal link"; } ?> The problem with your benphelps, was it ran the posted link through the function to strip everything but the domain, but not the link in the database, so it would still submit it. With yours kratsg, if somebody posted http://www.test.com, and then came back and posted http://test.com, it allowed it to post. But with both of your help, I was able to get this to work. Thanks again Quote Link to comment 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.