knight47 Posted March 8, 2007 Share Posted March 8, 2007 I have a small advertising script that will advertise a link on the main page of my site, what I'm wondering is how I can block a link from being submitted, what I currently have is an if statement that will not allow a specific link, but it is case sensitive. so for example, if I want to block site1.com, the user can submit SITE1.com, or sItE1.com, SiTE1.com, etc.. and they would all go through fine. How can I have it so that it doesn't matter the case, it will block the whole site1.com, no matter what the case is? OR, an alternative I was thinking about is forcing all letters to go lower case, is that a possibility in php? thanks in advance Quote Link to comment Share on other sites More sharing options...
skali Posted March 8, 2007 Share Posted March 8, 2007 To solve case problem. You can use $all_small = strtolower('AnY CASE yOu LIKE'); then compare it with whatever you like. Quote Link to comment Share on other sites More sharing options...
knight47 Posted March 8, 2007 Author Share Posted March 8, 2007 To solve case problem. You can use $all_small = strtolower('AnY CASE yOu LIKE'); then compare it with whatever you like. Hello Skali, thanks for your reply. I tried this, and for some reason it didn't work, would you happen to know why? $url = htmlspecialchars($_POST['url'], ENT_QUOTES); // cleans the submitted url $clean_url = stripslashes($url); $replace = array("http://www.", "HTTP://", "http://", "WWW.", "www."); $displayed_url = str_replace($replace, "", $clean_url); // removes the http:// or the www. from a link $banned_sites = array("site1.com", "site2.com"); if ($_POST['Submit'] && strtolower("$displayed_url") == $banned_sites) { echo "Sorry, but this URL has been banned from being advertised on our site."; } elseif.... { // code } The code works fine, but for some reason the blocking of a link will not work. Thanks. Quote Link to comment Share on other sites More sharing options...
Guest footballkid4 Posted March 8, 2007 Share Posted March 8, 2007 Try this: <?php $url = parse_url(stripslashes(htmlspecialchars($_POST['url'], ENT_QUOTES))); $banned_sites = array("site1.com", "site2.com"); // Check if it was even supplied if ( ! $url['host'] ) { // Not supplied } // Check if it's banned if ( in_array( strtolower( $url['host'] ), $banned_sites ) ) { // Banned } else { // Not banned, continue } ?> Quote Link to comment Share on other sites More sharing options...
knight47 Posted March 8, 2007 Author Share Posted March 8, 2007 thanks footballkid4, havn't tried it yet. but any idea why my code wouldn't work? Quote Link to comment Share on other sites More sharing options...
Glyde Posted March 8, 2007 Share Posted March 8, 2007 On this line: if ($_POST['Submit'] && strtolower("$displayed_url") == $banned_sites) One of two things is done somewhat incorrectly. 1) A variable name has no need to be placed in quotes. While it's strtolower("$displayed_url") is the EXACT SAME as strtolower($displayed_url), the quotes are not needde. 2) You are checking a string (strtolower("$displayed_url")) against an array ($banned_sites). You must use something like in_array to iterate through each element and then check for equality. Quote Link to comment Share on other sites More sharing options...
knight47 Posted March 8, 2007 Author Share Posted March 8, 2007 hello everyone, thanks for all the help. This is all of my code, nothing too advanced, but for some reason it's still not working, any ideas on why? <?php $url = htmlspecialchars($_POST['url'], ENT_QUOTES); // cleans the submitted url $clean_url = stripslashes($url); // removes slashes from the submitted url $disc = htmlspecialchars($_POST['disc'], ENT_QUOTES); // cleans the submitted disc. $cleaner_disc = stripslashes($disc); // removes slashes from the submitted disc. $replace = array("http://www.", "HTTP://WWW.", "http://WWW.", "HTTP://www", "http://", "HTTP://", "www.", "WWW."); // what is going to be replaced form the URL $displayed_url = str_replace($replace, "", $clean_url); // removes the "http://www." from the URL, this is going to be what is advertised $code = '<h1 class="style4"><a href="' . $clean_url . '">' . $displayed_url . '</a> </h1> <center><table width="94%" border="0" cellspacing=" " cellpadding=" "> <tr> <td><div align="left"><span class="style15">' . $cleaner_disc . '</span></div></td> <td><div align="right"><span class="style15"><a href="advertise.html">Replace with your link... </a></span></div></td> </tr> </table></center>'; $banned_sites = array("boysfood.com", "porn.com"); // porn websites if ($_POST['Submit'] && in_array($banned_sites, strtolower($displayed_url))) { echo "Sorry, but this URL has been banned from being advertised on our site."; echo '<meta HTTP-EQUIV="REFRESH" content="5; url=index.htm">'; } elseif ($_POST['Submit'] && strlen($displayed_url) < 31 && strlen($cleaner_disc) < 63) { $link = "link.php"; $create = fopen($link, 'w') or die("The file could not be created, please try again later"); fwrite($create, $code); fclose($create); echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.htm">'; } else { echo "Sorry, but you broke the internet! No not really, but on a more seriose note, you probably exceeded the max character limit on the URL, or the description field, please note that \"http://www.\" does not count, so please do not exclude this from you're URL. Thank you!"; } ?> I'm now getting an error: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/sbai/public_html/advertise.php on line 23 Line 23 is: if ($_POST['Submit'] && in_array($banned_sites, strtolower($displayed_url))) and it still allows the blocked URL from being submitted. and yes, my coding is probably very very sloppy, i'm fairly new to php, and i'm not really looking for a solution that i can copy/paste, because I won't learn anything from that! I'm looking for a solution that I would understand, I know there are probably different methods to achieve this, but i'm just a beginner Quote Link to comment Share on other sites More sharing options...
Glyde Posted March 8, 2007 Share Posted March 8, 2007 The in_array parameters are switched around. Instead of: in_array($banned_sites, strtolower($displayed_url)) It should be: in_array(strtolower($displayed_url), $banned_sites) Tell me how that goes Quote Link to comment Share on other sites More sharing options...
knight47 Posted March 8, 2007 Author Share Posted March 8, 2007 ok, I made the switch, and now I get: Fatal error: Call to undefined function: () in /home/sbai/public_html/advertise.php on line 23 - no matter what url I submit Line 23 is: if ($_POST['Submit'] && in_array($strtolower($displayed_url), $banned_sites)) btw, if you would like to test it out ??? Quote Link to comment Share on other sites More sharing options...
knight47 Posted March 8, 2007 Author Share Posted March 8, 2007 For now I'm just using this: if ($_POST['Submit'] && strtolower($displayed_url) == "porn.com" || strtolower($displayed_url) == "boysfood.com") which seems to be working fine, but I'm just curios on why the array didn't work... Quote Link to comment Share on other sites More sharing options...
knight47 Posted March 8, 2007 Author Share Posted March 8, 2007 Sorry for the triple posting, but I seem to have figured it out. I think the problem was I am not supposed to put a function inside of in_array() What I had was in_array(strtolower($url), $banned_sites); that didn't work, so what I did was: $site = strtolower($displayed_url); if ($_POST['Submit'] && in_array($site, $banned_sites)); this seemed to work. so for future references, don't put functions inside of in_array() Quote Link to comment Share on other sites More sharing options...
Glyde Posted March 8, 2007 Share Posted March 8, 2007 It works fine. You problem was that you put a $ in front of strtolower, making it $strtolower(). PHP tries to search for the variable $strtolower and then run the function, but it didn't exists. Say $strtolower = "hey";, with $strtolower(), PHP would call the function hey(). 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.