|
Treytor posted:Here is the script edited a bit, and this is what will get run repeatedly as people refresh like mad. What line do I throw in there to check to see if a time stamp is $timeout older than $now? And if so, delete that entry. I know you posted a few days ago, but I just saw it and it's too fun to pass up, so here's a five minute version of an approach you may try... php:<? define('LOG_PATH', '/tmp/mylogs'); define('TIMEOUT', 300); function ip2path($ip_string, $check_path = false) { $ip_long = ip2long($ip_string); $filename = constant('LOG_PATH'); for($i = 0; $i < 4; $i++) { $filename .= '/' . ($ip_long >> ($i*8) & 255); if($i == 3 && $check_path && !file_exists($filename)) { for($j = 0; $j < 3; $j++) @mkdir(constant('LOG_PATH') . ($create_dir .= '/' . ($ip_long >> ($j*8) & 255))); } } return $filename; } function recordData($ip) { $fh = fopen(ip2path($ip, true), 'w+'); fwrite($fh, pack('l', time())); fclose($fh); } function checkLog($ip) { if(($fh = @fopen(ip2path($ip), 'r')) === false) return true; list($foobar, $logtime) = unpack('l', fread($fh, 4)); fclose($fh); return (time() + 200 > ($logtime + constant('TIMEOUT'))); } # example use... $some_ip = '21.36.14.10'; printf("path for %s is: %s\n", $some_ip, ip2path($some_ip)); recordData($some_ip); printf("is ip ok? %s\n", checkLog($some_ip) ? 'yes' : 'no'); ?> As a bonus, if you want to delete old entries, you can cron a one-liner with find to remove files older than a certain modification time.
|
# ¿ Apr 4, 2008 22:59 |
|
|
# ¿ Apr 30, 2024 12:07 |
|
nbv4 posted:I'm not worried about that, I'm worried about actually running the queries twice, one for each object. I'm kind of a performance stickler like that. Is there anyway to easily copy a bunch of member variables from one object to another? I admit I'm not really an OOP expert. You don't want to copy member data or do the SQL multiple times -- that defeats the purpose of OOP. Remove functionality from the large object into other classes. It may make sense for the main class to hold instances of these new classes. The new classes could use properties of the main object by receiving a reference to it: php:<? class Foo { var $fighter; var $some_data = 'awesome'; function Foo() { $this->fighter = new FooFighter(); } function attack() { $this->fighter->kungfoo($this); } } class FooFighter { function kungfoo(&$foo) { echo "HIII-YAH! I'm " . $foo->some_data . "!\n"; } } ... $foo = new Foo(); $foo->attack(); // prints "HIII-YAH! I'm awesome!" ?>
|
# ¿ Apr 7, 2008 21:51 |
|
drcru posted:How do I search for <br /> with preg_match_all? preg_match_all("#<br />#", $string, $m) Or, if you want to be slashy... preg_match_all("/<br \/>/", $string, $m) Or, if you want to be flexible... preg_match_all("#<br(?: /)>#", $string, $m)
|
# ¿ Apr 9, 2008 05:15 |
|
Inquisitus posted:MVC theory question: When applying MVC, it is absolutely normal for the view to get state from a model object directly. If you want to expose some other interface than what your ORM provides, maybe read up on the adaptor pattern.
|
# ¿ Apr 21, 2008 18:50 |
|
Grigori Rasputin posted:Since zero evalutes to false, it fails even though I can tell the difference between a month that never occurs and a month at the zero position - I just can't test for it. You can test specifically for failure. This will set $i to zero, but the echo will not happen. php:<? $str = 'january is my favorite month'; if(($i = strpos($str, 'january')) === false) echo "not found\n"; ?>
|
# ¿ Apr 23, 2008 01:40 |
|
Zorilla posted:Triple-equals signs always weird me out. Since we're evaluating the success/failure of a variable assignment, is your example the same as this? It's not the same, because strpos() will find the string successfully and return zero (because the match occurs at offset zero). It's a case where a non-true value does not indicate failure. The triple equals does the same comparison, but only succeeds if the values compared are of the same type. The false keyword is implicitly boolean, so it is like (bool)0. This lets you check if strpos() failed regardless of its return value.
|
# ¿ Apr 23, 2008 03:03 |
|
|
# ¿ Apr 30, 2024 12:07 |
|
PHP's foreach is a mess. You need to be really careful with it because of how it performs implicit copies and may screw around with references in unexpected ways. Because PHP has no lexical scope, foreach also does not clean up after itself properly as it does in other languages. The effects differ depending what version of PHP you are using. I think a for loop is much safer and predictable.
|
# ¿ Jul 10, 2008 03:24 |