SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Ip addresses

  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    uk
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Ip addresses

    Hi.
    The below is used to disallow certain IP's from posting scores a website game.
    If their IP is, eg: 1.2.3.4, then I have found I am having to enter the full IP into the Db in order to deny.
    How can the below be written so that I can enter just, eg: 1.2. so all corresponding IP's, eg: 1.2.3.4, 1.2.4.5 are disallowed?

    Thanks for any help.


    $query = mysql_query('SELECT ip FROM table_name') or error_msg('Could not access database.');
    while ($row = mysql_fetch_row($query)) {
    if ($player_ip == $row[0]) error_msg('Scoring disallowed');
    }

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    what you could so is store 1.2.*.* into the database, so that this string would be returned just like all the other ip numbers

    then all you need to do is change your logic in that loop to test for asterisks going from left to right

    i'm not sure how to do that (split? explode? nested ifs?) because while i know mysql pretty well, i don't do php

    perhaps you should ask in the php forum

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You'll need to use regular expressions. Something like this:
    Code:
    1\.2\.[0-9]{1,3}\.[0-9]{1,3}
    and compare an IP address this way:

    Code:
    if (eregi($_SERVER['REMOTE_ADDR'], "1\.2\.[0-9]{1,3}\.[0-9]{1,3}") ) { 
        // do something
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •