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 (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

Top Tags Alphabetically

XSLT (5)
actionscript (7)
apache (13)
awk (5)
bash (19)
cli (4)
cron (4)
css (8)
django (4)
dns (6)
email (11)
error (4)
expressionengine (21)
fastcgi (11)
flash (7)
freebsd (7)
google (4)
html (7)
java (5)
javascript (29)
launchd (4)
lighttpd (39)
linux (8)
mac (13)
movabletype (5)
mysql (20)
osx (30)
perl (8)
php (52)
python (10)
rails (39)
recipe (11)
regex (12)
ruby (33)
rubyonrails (5)
search (4)
searchbox (4)
shell (36)
sql (11)
ssh (30)
subversion (14)
svn (15)
sysadmin (9)
textdrive (5)
textpattern (10)
tiger (7)
tsql (4)
typo (4)
unix (7)
xhtml (5)

« Newer Snippets
Older Snippets »
Showing 61-80 of 407 total

Show line numbers in vi

:se nu

Check out all TextMate Bundles with SVN

svn --username anon --password anon co http://macromates.com/svn/Bundles/trunk/Bundles /Users/ned/Library/Application\ Support/TextMate/Bundles

Small and simple MySQL (and PHP) Connection

<?php
$conn = mysql_connect("DBHOST", "DBUSERNAME", "DBPASSWORD");
mysql_select_db("DBTABLE", $conn) or die(mysql_error());
?>


This is probably the most basic way of connecting to your MySQL database. :)

Remove favicons from Safari's cache

Safari caches the favicon for a site the first time it views it, even if the site doesn't have a favicon. This means if one is added later, or it is changed, you'll need to manually remove the cached file. This is pretty simple for a single site:

grep -r  "domain.tld" ~/Library/Safari/Icons/


Then, just remove all the files that match.

Back up MySQL databases

Dump all MySQL databases to a location of your choice. I run this as a backup_script from rsnapshot.

This requires the following .my.cnf in the home directory of the user the script runs as (root, when being run from rsnapshot). As it contains an important password, permissions on this file should be 600 (-rw-------).
[client]
user     = root
password = YOUR_MYSQL_ROOT_PASSWORD


#!/bin/bash -e
###########################################################
# Back up MySQL databases
###########################################################

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
PATH="$PATH:/usr/local/mysql/bin"

# My config
#BACKUP_DIR="."

BACKUP_DIR=""

# Do not backup these databases
IGNORE="test"

# Get list of all databases
DB_LIST=`mysql -Bse 'show databases'`

for db in $DB_LIST; do

        # set skip variable
        skip=0

        if [ "$IGNORE" != "" ]; then
                for i in $IGNORE; do
                        [ "$db" == "$i" ] && skip=1 || :
                done
        fi

        if [ "$skip" == "0" ]; then
                mysqldump $db | gzip -9 > $BACKUP_DIR/$db.sql.gz
        fi

done

exit 0



Back up Subversion repositories

Dump all Subversion repositories to a location of your choice. I run this as a backup_script from rsnapshot.

#!/bin/sh -e
###########################################################
# Back up Subversion repositories
###########################################################

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Subversion config
###########################################################

# My config
#SVN_DIR="/var/svn"
#BACKUP_DIR="."

SVN_DIR=""
BACKUP_DIR=""
SVN_REPOS=`ls $SVN_DIR`

# Dump repositories
###########################################################

for repos in $SVN_REPOS; do
    svnadmin dump --incremental --deltas --quiet $SVN_DIR/$repos \
        > $BACKUP_DIR/$repos
done

exit 0



drupal module_hook problem

// does not work

function testmodule_menu($may_cache) {
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path' => 'node/12',
      'title' => t('Testing 123'),
      'callback' => 'do_it',
      'access' => TRUE,
      'type' => MENU_CALLBACK);
    return $items;
  }
}



// works

function testmodule_menu($may_cache) {
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path' => 'nodenode/12',
      'title' => t('Testing 123'),
      'callback' => 'do_it',
      'access' => TRUE,
      'type' => MENU_CALLBACK);
    return $items;
  }
}

grep-based phrase search with Spotlight

Some examples:


# for mdfind "phrase* " and "phrase! " also work, but not "phrase ! "
mdfind -0 -onlyin ~ "phrase This is a" | xargs -0 grep -ails -E 'This is a phrase!'


mdfind -0 "It's just $8.99!" | xargs -0 grep -ails -E "It's just \\\$8\\.99\\!"


mdfind -0 'Fr\303\251d\303\251ric Chopin' | xargs -0 grep -ails -Z -E 'Fr.d.ric Chopin' | \
xargs -0 basename


Enable svn+ssh remote logins

Installing Subversion for local use is general an easy install, but allowing remote access to your svn repository over SSH can be problomatic dependent upon your OS and the means taken to install.

For Darwinports and Fink on OS X the install location has to be added to users $PATHs, but there are extra steps outlined here for use of the svn+ssh means of access:

http://subversion.tigris.org/faq.html#ssh-svnserve-location

A much easier alternate is to sym link the svn binaries to a place on the default PATH (used by the SSH login):

#For Darwinports
ln -s /opt/local/bin/sv* /usr/bin/

#For Fink
ln -s /sw/bin/sv* /usr/bin/


This links all the binaries at once.

Block Blacklisted Bandwidth Thieves from Hotlinking Your Image Files

Code for an .htaccess file located in a directory containing images (or any other type of file for that matter) to which you want to block hotlinking from blacklisted websites.
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?evilwebsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://forum\.evilwebsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?bandwidththieves\.net [NC]
RewriteRule \.(jpeg|jpg|gif|bmp|png|JPEG|JPG|GIF|BMP|PNG)$ http://example.com/pwnt.gif [L]

example.com/pwnt.gif
is the location of the file you want to serve in place of the hotlinked image. Mine just says 'PWNT'.

Alternate last line to simply block the images from being hotlinked instead of serving a replacement image:
RewriteRule \.(jpeg|jpg|gif|bmp|png|JPEG|JPG|GIF|BMP|PNG)$ - [F]
« Newer Snippets
Older Snippets »
Showing 61-80 of 407 total