Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
<deleted user>

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.

code:
<?php ?>

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');

?>
I guarantee you this can take a hammering. Basically, you take advantage of the filesystem's ability to lookup filenames quickly. It maintains a directory structure based on the IP octets, so that you don't have 2^32 files in one directory (which would not be good). This also only uses 4 bytes of disk space per address you log.

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.

Adbot
ADBOT LOVES YOU

<deleted user>

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!"
?>

<deleted user>

drcru posted:

How do I search for &lt;br /> with preg_match_all?

preg_match_all("#&lt;br />#", $string, $m)

Or, if you want to be slashy...

preg_match_all("/&lt;br \/>/", $string, $m)

Or, if you want to be flexible...

preg_match_all("#&lt;br(?: /)>#", $string, $m)

<deleted user>

Inquisitus posted:

:siren: MVC theory question: :siren:

I'm currently playing around with the Propel ORM framework and trying to work out how best to integrate it into Kohana (an MVC framework). My question is how should I go about transforming Propel's generated data objects when they're passed through the controller and into the view? Leaving them as they are would be exposing details of the data layer to the presentation layer, but constructing a new object for each one seems completely over the top.

Not sure how to proceed; please advise!

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.

<deleted user>

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";
?>

<deleted user>

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?

php:
<?
$str = 'january is my favorite month';
if (!($i = strpos($str, 'january'))) echo "not found\n";
?>

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.

Adbot
ADBOT LOVES YOU

<deleted user>
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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply