Jump to content

blocking a specific string from being blocked from submittion


knight47

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest footballkid4

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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() :)

Link to comment
Share on other sites

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().

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.