I found an amazing resource, completely worth sharing.
It’s Free Online Course Material from MIT (yes, THE Massachusetts Institute of Technology MIT…).
It’s got free lecture notes, exams, and videos from actual MIT courses.
I found an amazing resource, completely worth sharing.
It’s Free Online Course Material from MIT (yes, THE Massachusetts Institute of Technology MIT…).
It’s got free lecture notes, exams, and videos from actual MIT courses.
After reading about the Just Another Perl Hacker code snippets on Wikipedia, I was pretty intrigued. The only problem is, I hate Perl. So, here is my version: just another php hacker.
I cleaned the code up a little bit here for readability (which is against the point, I guess).
foreach(explode("2","1152117298211521162114") as $z) {
$h .= chr($z);
}
$f = (string)(2*19*227*11489);
$q = chr($h($f,(int)$f[3],(int)$f[2]+1)).chr($h($f,(int)$f[2]+1,(int)$f[0]/3)).chr($h($f,(int)$f[strlen($f)-1]+1,(int)$f[0]/3));
$z = eval("\$u=".$q."(".str_replace("3",").$q(","117311031153101311431053973108310531223101").");");
$w = "a:23:{i:0F106F1F11F2F-2F3F1F4F-84F5F65F6F13F7F1F8F5F9F-12F10F-3F11F13F12F-82F13F80F14F-8F15F8F16F-80F17F72F18F-7F19F2F20F8F21F-6F22F13;}";
$b = str_replace("F",";i:", $w);
foreach($u($b) as $c) {
echo($q($s+=$c));
}
Here is the more obfuscated text:
foreach(explode("2","1152117298211521162114")as$z){$h.=chr($z);}$f=(string)(2*19*227*11489);$q=chr($h($f,(int)$f[3],(int)$f[2]+1)).chr($h($f,(int)$f[2]+1,(int)$f[0]/3)).chr($h($f,(int)$f[strlen($f)-1]+1,(int)$f[0]/3));$z=eval("\$u=".$q."(".str_replace("3",").$q(","117311031153101311431053973108310531223101").");");$b=str_replace("F",";i:","a:23:{i:0F106F1F11F2F-2F3F1F4F-84F5F65F6F13F7F1F8F5F9F-12F10F-3F11F13F12F-82F13F80F14F-8F15F8F16F-80F17F72F18F-7F19F2F20F8F21F-6F22F13;}");foreach($u($b)as$c){echo($q($s+=$c));}
With PHP, it is extremely simple to create a file that can be interpreted by speadsheet programs, such as Excel or OpenOffice.org.
When you think about what this really means, it should be clear how simple it is. We’ll do this with by created a CSV file. CSV stands for Comma-Separated Values. So basically, we’ll print out a text file with a csv MIME type, and some separated values. The MIME type can either be “text/csv” or “text/x-csv”. The standard, per the name, is to separated the values with a comma, and all values that contain a comma should be wrapped in double quotes. For example:
we,are,separated
"And here, ",we have a, comma in a value
Another way, which may be the more common practice, per my version of OpenOffice.org, is to separate the values with a semicolon:
One;Two;Three
I’ll use the semicolon method. Here is a quick script that will create a small CSV file, which can be read by a spreadsheet program:
header("Content-type: text/csv");
$data = array(
array(
"a-1",
"a-2",
"a-3",
),
array(
"b-1",
"b-2",
"b-3",
),
array(
"c-1",
"c-2",
"c-3",
),
);
foreach($data as $row) {
echo implode(";", $row) . "\n\r";
}
Now, this will prompt the user for a download, which will end in a .php filename. If you have access, and are using Apache, you can solve this with a little .htaccess. Simply putting a .htaccess file in the directory, with this in it, should do the trick.
AddType application/x-httpd-php .csv
With that, working properly, you should be able to create PHP scripts that use a .csv extension. Then, when you run the above script, save it as “mydata.csv” and the downloader should see no connection to PHP.
Now, how can we use this in a little more realistic scenario? Well, we can use this to create CSV dumps of SQL result sets.
$query = @mysql_query($sql);
$data = array();
while($row = @mysql_fetch_assoc($query)) {
//first time through, grab the keys
if(!sizeof($data)) {
$data[] = array_keys($row);
}
$tmp = array();
foreach($row as $key=>$value) {
$tmp[] = $value;
}
$data[] = $tmp;
}
foreach($data as $row) {
echo implode(";", $row) . "\n\r";
}
And there you have it! That script should be capable of turning your queries into a spreadsheet readable file. Fun, right?
In short, the Singleton Pattern is a design pattern used when we want only ONE instance of a class to be initialized. This is useful in CPU/Memory intensive classes, such as database handle abstractions.
The Singleton Pattern is a pretty fundamental pattern, worth learning about, if you aren’t already aware. It’s characterized by a private constructor, which can only be called by class member methods, and a public static accessor method, to return a reference to the object.
Here is a quick example of how it can be implemented in PHP (There are other ways, as well).
class Singleton {
private static $__instance;
protected $_value;
private function __construct() {
$this->_value = rand();
}
public static function getInstance() {
if(!isset(self::$__instance)) {
self::$__instance = new Singleton();
}
return self::$__instance;
}
public function display() {
echo $this->_value . "<br />";
}
}
Use it like this:
$foo = Singleton::getInstance(); $foo->display(); $bar = Singleton::getInstance(); $bar->display();