Help: Block IPs Before a Cookie Drop

theguru68

Registered Member
Joined
Jun 20, 2009
Messages
68
Reaction score
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?
 
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. :)
 
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);
}

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

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.
 
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. :)
 
Yes he surely did! I wish I could put a couple of you guys in my back pocket lol!!!!


:beach2(2)
 
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.

Well, I'm watching video after video and reading till my eyes bleed out. So, yeah, I foresee issues!
 
Back
Top