SitePoint Sponsor

User Tag List

Results 1 to 23 of 23

Thread: another problem

  1. #1
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    another problem

    this is the code on start of index.php
    and it wont validate from db if the ip is blocked?
    PHP Code:
    session_start();
    include_once(
    "config.php");
    $_SESSION["ip"] = $_SERVER['REMOTE_ADDR'];
    $ip $_SESSION["ip"]; 
    $query mysql_query("SELECT * FROM IP WHERE ip = ".$ip); 
    while(
    $row mysql_num_rows($query)) {
    if(
    $row[2] == 'block') {
    echo 
    "You cant view this page";
    } else {
    // rest of page
    }
    mysql_free_result($query);

    suggestions?
    thanx in advance

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I think I mentioned in an earlier post
    PHP Code:
    $_SESSION["ip"] = $_SERVER['REMOTE_ADDR']; 
    $ip $_SESSION["ip"]; 
    $query mysql_query("SELECT * FROM IP WHERE ip = ".$ip); 
    $ip_checkmysql_num_rows($query);

    if(
    $ip_check 0){
    //do something
    } else {
    //do nothing 
    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i know but...
    my table is
    id ip status
    1 x null

    i need to check if status is blocked to block the page!
    im making sense?

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Stekene, Belgium
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $query = mysql_query("SELECT * FROM IP WHERE ip = ".$ip);
    I think you have to place single quotes around $ip.
    Code:
    $query = mysql_query("SELECT * FROM IP WHERE ip = '".$ip ."'");
    Good artists copy, great artists steal (Picasso)

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont need couse it is called concatenation!!!

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Stekene, Belgium
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    i dont need couse it is called concatenation!!!
    Echo your SQL statement:

    It will look like:

    SELECT * FROM IP WHERE ip = 127.0.0.1

    I don't think this is valid SQL.
    Good artists copy, great artists steal (Picasso)

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    perhaps something like...

    PHP Code:
    $_SESSION["ip"] = $_SERVER['REMOTE_ADDR']; 
    $ip $_SESSION["ip"]; 
    $query mysql_query("SELECT * FROM IP WHERE ip = ".$ip); 
    $result mysql_fetch_assoc($query);

    if(
    $result["ip"] == 'blocked') {
    header("Location: goaway.php");

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Reminder: No need to use that tone. People are only trying to help.

    I think fbronx is correct. All variables, but not integers, must have quotes around.

    Here is another solution (haven't tested it):
    PHP Code:
    session_start(); 
    include_once(
    "config.php"); 

    $ip $_SERVER['REMOTE_ADDR'];
    $_SESSION["ip"] = $ip

    $sql "SELECT status 
              FROM IP 
             WHERE ip = '
    $ip'";
    echo 
    "<p>SQL: $sql</p>"// This line can be removed when working
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_num_rows($query) or die(mysql_error());

    echo 
    '<p>Status: ' $row['status'] , '</p>';  // This line can be removed when working
    if($row['status'] == 'block') { 
        echo 
    "You cant view this page"
    } else { 
        
    // rest of page 

    mysql_free_result($result); 
    As you see I've removed the while-loop. I did that becuase I assume an ip-address is only registered once on your db. At least it should be.

    -Helge

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just said that it was a concatenation....
    without any tone....
    sorry if i hurt anyody.....
    Quote Originally Posted by Helge
    Reminder: No need to use that tone. People are only trying to help.

    I think fbronx is correct. All variables, but not integers, must have quotes around.

    Here is another solution (haven't tested it):
    PHP Code:
    session_start(); 
    include_once(
    "config.php"); 

    $ip $_SERVER['REMOTE_ADDR'];
    $_SESSION["ip"] = $ip

    $sql "SELECT status 
              FROM IP 
             WHERE ip = '
    $ip'";
    echo 
    "<p>SQL: $sql</p>"// This line can be removed when working
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_num_rows($query) or die(mysql_error());
    // where u have find this $query?
    echo '<p>Status: ' $row['status'] , '</p>';  // This line can be removed when working
    this line wont work...
    if(
    $row['status'] == 'block') { 
        echo 
    "You cant view this page"
    } else { 
        
    // rest of page 

    mysql_free_result($result); 
    As you see I've removed the while-loop. I did that becuase I assume an ip-address is only registered once on your db. At least it should be.

    -Helge

  10. #10
    SitePoint Enthusiast mrobinson's Avatar
    Join Date
    Aug 2004
    Location
    New York, NY, USA
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whether using concantenation or not, the SQL needs to be correct. All strings need to be enclosed in single-quotes.

    To clarify...

    If the field contains a string it must be enclosed in single-quotes:
    Code:
     SELECT * FROM table WHERE field = 'abcde';
    If the field contains a number no quotes are used:
    Code:
      SELECT * FROM table WHERE field = 12345;
    If the field contains a NULL:
    Code:
      SELECT * FROM table WHERE field IS NULL;
    Hope this helps...

    Regards,
    Mark

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont know...
    nothing comes up from $row['status'];
    ....

  12. #12
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What code do you use?
    What does your SQL looks like?
    You don't get any error messages?

    -Helge

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the code
    PHP Code:
    session_start();
    include_once(
    "config.php");
    $_SESSION["ip"] = $ip
    $ip $_SESSION["ip"]; 


    $sql "SELECT status 
              FROM IP 
             WHERE ip = '
    $ip'"
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_num_rows($result) or die(mysql_error()); 
    echo 
    $row[2];
    echo 
    '<p>Status: ' $row['status'] , '</p>';  // This line can be removed when working 
    if($row['status'] == 'block') { 
        echo 
    "You cant view this page"
    } else { 
    //rest of page
    }
    mysql_free_result($sql); 
    config is ok!
    and this is the table
    Code:
    CREATE TABLE `IP` (
      `id` int(11) NOT NULL auto_increment,
      `ip` text NOT NULL,
      `status` text NOT NULL,
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;
    any error here?

  14. #14
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is this doing:
    PHP Code:
    $_SESSION["ip"] = $ip
    $ip $_SESSION["ip"]; 
    Where is $ip coming from.
    Did you try the exact code I posted?

    -Helge
    Last edited by Helge; Aug 27, 2004 at 06:26. Reason: i=I

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    this is the code
    PHP Code:
    session_start();
    include_once(
    "config.php");
    $_SESSION["ip"] =  $_SERVER['REMOTE_ADDR'];  
    $ip $_SESSION["ip"]; 


    $sql "SELECT status 
              FROM IP 
             WHERE ip = '
    $ip'"
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_num_rows($result) or die(mysql_error()); 
    echo 
    $row[2];
    echo 
    '<p>Status: ' $row['status'] , '</p>';  // This line can be removed when working 
    if($row['status'] == 'block') { 
        echo 
    "You cant view this page"
    } else { 
    //rest of page
    }
    mysql_free_result($sql); 
    config is ok!
    and this is the table
    Code:
    CREATE TABLE `IP` (
      `id` int(11) NOT NULL auto_increment,
      `ip` text NOT NULL,
      `status` text NOT NULL,
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;
    any error here?
    no status output???

  16. #16
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does your SQL ($sql variable) look like? If you had used the exact same code as I gave you in post #8 that would have been printet. Add this line to the code (already in my code in post #8):
    PHP Code:
    echo "<p>SQL: $sql</p>"// This line can be removed when working 
    Also are you sure the ip you're looking for actually is in the database table?
    -Helge

  17. #17
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Stekene, Belgium
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe setting the error_reporter can help you find some errors. Place the following at the very beginning of your file:

    PHP Code:
     error_reporting(E_ALL); 
    And don't use the type TEXT for an IP and state. You should use varchar.
    Good artists copy, great artists steal (Picasso)

  18. #18
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $_SESSION["ip"] = $_SERVER['REMOTE_ADDR']; 
    $ip $_SESSION["ip"]; 
    $sql "SELECT status FROM IP WHERE ip = '$ip'"
    echo 
    "<p>SQL: $sql</p>"//this line show the result from db correctly!!!
    //SQL: SELECT status FROM IP WHERE ip = '82.48.234.126'
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_num_rows($result) or die(mysql_error()); 
    echo 
    '<p>Status: ' $row['status'] , '</p>'//this one not???!!!
    if($row['status'] == 'block') { 
        echo 
    "You cant view this page"
    } else { 
    //rest
    }
    bla bla 
    the db is that
    Code:
    CREATE TABLE `IP` (
      `id` int(11) NOT NULL auto_increment,
      `ip` text NOT NULL,
      `status` text NOT NULL,
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM
    wheres the stuck here?
    thanx in advance

  19. #19
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Spottet two errors. Try this:
    PHP Code:
    error_reporting(E_ALL);
    session_start(); 
    include_once(
    "config.php"); 

    $ip $_SERVER['REMOTE_ADDR']; 
    $_SESSION["ip"] = $ip

    $sql "SELECT status 
              FROM IP 
             WHERE ip = '
    $ip'"
    echo 
    "<p>SQL: $sql</p>"// This line can be removed when working 
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_fetch_array($result) or die(mysql_error()); 
     
    echo 
    '<p>Status: ' $row['status'] , '</p>';  // This line can be removed when working 
    this line wont work... 
    if(
    $row['status'] == 'block') { 
        echo 
    "You cant view this page"
    } else { 
        
    // rest of page 

    mysql_free_result($result); 
    -Helge

  20. #20
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finaly Helge!!!!!!
    After 2 Hours Of Headacke!!!!!

    Thanx A Lot.....
    I Have To Take U To A Dinner!!!!!!!
    Cheers

  21. #21
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finally. Glad you got it working.

    -Helge

  22. #22
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please helge can u report me those 2 errs!
    just for my php culture advancing!!!!

    thanx

  23. #23
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You did fix one of them yourself: I did mix the $query and $result variable.
    The other one was using mysql_num_rows() instead of mysql_fetch_array().

    -Helge


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
  •