1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help: Block IPs Before a Cookie Drop

Discussion in 'General Programming Chat' started by theguru68, Mar 10, 2012.

  1. theguru68

    theguru68 Registered Member

    Joined:
    Jun 20, 2009
    Messages:
    67
    Likes Received:
    35
    Here is my dilemma. I have searched high and low for a solution and keep coming up stone-walled. So, I don't know if it's even possible.

    I want to set a cookie, however, I only want the cookie to set AFTER the incoming IP has been checked to a database of IPs. If the incoming IP is within the database, I do not want to drop the cookie.

    It sounds so simple, yet seemingly not. Any thoughts or solutions?
     
  2. Amsterdammer

    Amsterdammer Power Member

    Joined:
    Aug 9, 2011
    Messages:
    515
    Likes Received:
    563
    I may be wrong here but isn't a cookie to be set before anything else loads?...

    The only way I see is to redirect them if they are not in the database. You can use php for that part when the page loads. Then redirect to a page to set the cookie.

    Or perhaps use htaccess but I know too little about it to be sure.

    This is not my field of expertise so I could be wrong but I hope it helps. :)
     
  3. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    Can't you use something like this?

    PHP:
    <?php

    $longip 
    ip2long($_SERVER['REMOTE_ADDR']);
    $found doYourDatabaseCheck($longip); // doYourDatabaseCheck() should return true or false

    if ( $found == false ) {
        
    $expire time() + 3600*24;
        
    setcookie("cookieName""cookieValue"$expire);
    }

    ?>
     
    • Thanks Thanks x 2
  4. theguru68

    theguru68 Registered Member

    Joined:
    Jun 20, 2009
    Messages:
    67
    Likes Received:
    35
    Amsterdammer, yes [as far as I understand it] and that is part of my dilemma.

    45415: I don't know if your code is correct, but it will certainly help get me in the right direction!!! Thank you!!! I have a LOOOONG way to go from here.
     
  5. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    It should work, just make sure you run this code first before everything else. Otherwise you will get an error saying that headers are already sent. Let me know, if you get any problems with the code.
     
    • Thanks Thanks x 1
  6. Amsterdammer

    Amsterdammer Power Member

    Joined:
    Aug 9, 2011
    Messages:
    515
    Likes Received:
    563
    Theguru68, actually, I think 45415 solved your problem completely. :D

    A cookie must be set before the html code and since php can proceed it....
    I just checked it.

    After reading your topic, I would thank him very very kindly for saving you months of work. ;)

    Best of luck. :)
     
    • Thanks Thanks x 1
  7. theguru68

    theguru68 Registered Member

    Joined:
    Jun 20, 2009
    Messages:
    67
    Likes Received:
    35
    Yes he surely did! I wish I could put a couple of you guys in my back pocket lol!!!!


    :beach2(2)
     
  8. theguru68

    theguru68 Registered Member

    Joined:
    Jun 20, 2009
    Messages:
    67
    Likes Received:
    35
    Well, I'm watching video after video and reading till my eyes bleed out. So, yeah, I foresee issues!