DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world


  • submit to reddit

Recent Snippets

require 'digest/sha1'
sha1 = Digest::SHA1.hexdigest('something secret')
                    I put this in 

#limit amount of virtula memory, 
#CPU time and number of processes
#used by normal users.
if [ $UID -ge 500 ]
    ulimit -m 1000000
    ulimit -v 1000000
    ulimit -u 150

With this script, I am able to survive                
                    A PHP cgi binary compiled with fcgi support

> /usr/local/www/cgi-bin/php5-fcgi -v
PHP 5.0.3 (cgi-fcgi) (built: Dec 30 2004 22:44:32)

Central config in httpd.conf

<IfModule mod_fastcgi.c>
FastCgiIpcDir /usr/local/www/fcgi_ipc/tmp
AddHandler fastcgi-script .fcgi
FastCgiSuexec /usr/local/sbin/suexec
FastCgiConfig -singleThreshold 100 -killInterval 300 -autoUpdate -idle-timeout 240 -pass-header HTTP_AUTHORIZATION
<Location /php-fastcgi/>
Options ExecCGI        
SetHandler fastcgi-script

In a virtual host

SuexecUserGroup ${USER} ${GROUP}
ScriptAlias /php-fastcgi/ ${HOME}/php-fastcgi/ 
AddType application/x-httpd-fastphp .php
Action application/x-httpd-fastphp /php-fastcgi/php5-fcgi

And then the ${HOME}/php-fastcgi/php5-fcgi wrapper	

export PHPRC 
exec /usr/local/www/cgi-bin/php5-fcgi 

The PHPRC environment sets the directory where php.ini is to be found                
                    a generic object type thing for perl where you can use setanything and getanything to set and get properties via the AUTOLOAD method.
package thing;

use strict;

sub new {
	my %self;
	my ($class,@rest) = @_;
	unless ($#rest%2) { die "Odd parameter count\n"}
	for (my $k=0; $k<@rest; $k+=2) {
		my $wotsit = lc($rest[$k]);
		$wotsit =~ s/-//;
		$self{$wotsit} = $rest[$k+1];
	bless \%self,$class;

        my ($where,$val) = @_;
        our $AUTOLOAD;
        $AUTOLOAD =~ s/.*://;
        my ($dirn,$what) = ($AUTOLOAD =~ /(...)(.*)/);
        if ($dirn eq "get") {
                return $$where{lc($what)};
        } elsif ($dirn eq "set") {
                $$where{lc($what)} = $val;
        return 1;


Here's a quick example of usage
use thing

$house = new thing("type","building","rooms",12,"foundations", "yes");

print $house -> gettype();
$house -> settype("tree");
print " " . $house -> gettype();

                    Suppose that you have a draggable list as described at To send that list from a form, do

function listToXml(){
	var vList = document.getElementById('phonetic');
        var vLen  = vList.childNodes.length;
	var vXml  = "<ul>";
        for(var i = 0; i < vLen - 1; i++){
	    vXml = vXml + '<li>' + vList.childNodes[i].innerHTML + '</li>';
	vXml = vXml + '</ul>';
	document.myform.listfield.value = vXml;


	<ul id="phonetic" class="sortable boxy" style="margin-left: 1em;">

<form name="myform">
<input type="text" name="listfield" value="" size="60">
<input type="button" value="submit2" name="submit2" onClick="listToXml()">

                    <b>Warning from admin: Some users have reported this snippet LOCKED UP THEIR COMPUTERS. Do NOT run it unless you accept this as a possible consequence!!</b>

Guaranteed to work on mandrake, even at security level 4 (5 being the highest).

dfdeshom@localhost$ :(){ :|:& };:
                    Handy shorthand syntax for php. From:

A compact (if cryptic) shorthand is useful: 

echo "var is ".($var < 0 ? "negative" : "positive"); 

is equivalent to: 

echo "var is "; 
if ($var < 0) 
    echo "negative"; 
    echo "positive"; 

The shorthand can be considered (expression ? true_value : false_value) where 'expression' is evaluated and, if true, the 'true_value' is returned, otherwise the 'false_value' is returned.

// simple script to download mp3's from a password protected site using a playlist file 
//remote username
$username = 'username';
// remote password
$password = 'password';

// m3u file
$get = file("file.m3u"); // m3u (playlist) file contains paths to  remote files

foreach($get as $url){
        $trim_url = rtrim($url); // trim newline
        $explode_url = explode("//", $trim_url); //explode url so you can insert username:password for wget
        $fixed_url = 'http://' . $username . ':' . $password . '@' . $explode_url[1];

        exec("wget $fixed_url"); // downloads to current directory

                    I run 
./ > .htaccess 
 every week or so... 
    Get the entries from a blacklist
    and print them to standard output
    latest = urllib2.urlopen("")
     str = ''
     begin = "RewriteCond %{HTTP_REFERER} ^.*"
     for line in latest:
         str+= begin+ re.sub('(\s#:url:|\s#:regex-url:|\s#:rbl:)',
                                   ".*$ [OR]\n",line)
        return str

Output looks like this...
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.*fondos-pantalla..*$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]
RewriteCond %{HTTP_REFERER} ^.**$ [OR]

                    I found this code on Mark Pilgrim's website, it's his code.

To prevent Greasemonkey user scripts from executing on your web site, put this line within the HEAD of every page:

<script type="text/javascript">
    window.addEventListener = null;