Grow your CSS skills. Land your dream job.

checking the contents of a variable on PHP

  • # January 27, 2009 at 12:43 pm

    Hey everyone,

    A question about PHP.

    I want to have a PHP script that has the ability to check a variable for certain values.

    lets say I want to check to see if a text string contains [url-http// somewhere in the variable, how can I test for this?
    I am thinking an if statement, but how can I check for the occurrence of certain parts of a variable

    # January 27, 2009 at 1:00 pm

    you could use preg_match() to search for a regular expression. For example:

    Code:
    if (preg_match(“{[url-http//}”, $content))
    {
    }

    http://us.php.net/preg-match

    The opening and closing brackets around the string that you are searching for are necessary, because the define the beginning and end of the regular expression.

    # January 27, 2009 at 1:28 pm
    "falkencreative" wrote:
    you could use preg_match() to search for a regular expression. For example:

    Code:
    if (preg_match(“{[url-http//}”, $content))
    {
    }

    http://us.php.net/preg-match

    The opening and closing brackets around the string that you are searching for are necessary, because the define the beginning and end of the regular expression.

    so the $content is the variable I am checking?
    Edit: tried this and it does not work

    edit: why I am looking for something like this, is I got an email script that works fine in most sites, but one has nonsense coming from it, I think it is being used by a human, no real way of telling I don;t think, but this is what I get from the script:
    Name: slrvwoq
    Tel: VeZsEAKNtwwpuwrlUY
    Details: ybca3Y <a href="http://emqbeghrwjwp.com/">emqbeghrwjwp</a&gt;, jqcldddlfcux, [link=http://axpxfzkuwubv.com/]axpxfzkuwubv[/link], http://jnznifmyzssr.com/

    # January 28, 2009 at 5:18 am

    Well thats not gonna fix the spamproblem problem.
    Its better to use some sort of captcha or an other option. If you google for contact form spam there are lots of little things you can do.

    # January 28, 2009 at 1:13 pm

    Yes, $content is the variable you are checking.

    I’m not sure why it isn’t working for you, but it is a method I am using with success on my site. Basically, I use preg_match() to check the contents of the message that the user is trying to send to me. If they match a regular expression I have set up (I am checking for values such as "href" and "link="), I generate an error and ask the user to correct the input.

    No, it won’t ultimately fix the spam problem, but it will catch the sort of spam you are getting, and should block it from coming through, at least as long as it has the "href" and other values I am checking for.

    # January 28, 2009 at 2:47 pm
    "Argeaux" wrote:
    Well thats not gonna fix the spamproblem problem.
    Its better to use some sort of captcha or an other option. If you google for contact form spam there are lots of little things you can do.

    Cheers, I got some good tips

    "falkencreative" wrote:
    Yes, $content is the variable you are checking.

    I’m not sure why it isn’t working for you, but it is a method I am using with success on my site. Basically, I use preg_match() to check the contents of the message that the user is trying to send to me. If they match a regular expression I have set up (I am checking for values such as "href" and "link="), I generate an error and ask the user to correct the input.

    No, it won’t ultimately fix the spam problem, but it will catch the sort of spam you are getting, and should block it from coming through, at least as long as it has the "href" and other values I am checking for.

    I got it working now. but this way:

    Code:
    if(preg_match(“/http/i”, “$Body”))
    {
    print ““;
    exit;
    }

    What I have is…
    a javascript check for user input to highlight non entered fields.
    a validation field that my php checks the variable input by the user, if it is not correct it redirects.
    A hidden field in html, if it has anything my php redirects

    PHP checks for hidden value, validation value, if email is empty, if body is empty, if body has http using preg_match which redrects to google.

    so far after my testing, it seems to work fine

    cheers guys

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".