Alpha
Share and Organize Code
login register
HotNewTop
F.A.Q.
1. To begin adding code first register.
2. Create your own tags for code that you add.
3. To see your own code click Me, to see all code click Everyone, or filter by people rob.
4. To follow a user, click on their username, rob and hit follow.
5. Add other code to your library by clicking the
button near the code titles (note that you can not edit these codes).
6. To create a copy of a code that you can edit use fork.
7. If you can't find your code, double check your filter settings as you can filter by Category, User and Tag simultaneously.
8. Add comments to a code by clicking on it's title or the comments link underneath it.

Select Elements That Have Any Portion Within the Viewing Window (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$.extend($.expr[':'],
{
    inView: function(el)
    {
        var offset = $(el).offset();

        return !(
            (offset.top > $(window).height() + $(document).scrollTop()) ||
            (offset.top + $(el).height() < $(document).scrollTop()) ||
            (offset.left > $(window).width() + $(document).scrollLeft()) ||
            (offset.left + $(el).width() < $(document).scrollLeft())
        )
    }
});

// usage
$(#container :inView")
[+] (13) more...
comments (0) flag fork

Content Editable in HTML5 (plain)

1
2
3
<div contentEditable>
blah blah
</div>
comments (0) flag fork

Hide Directory Structure from URL (plain)

1
2
# In httpd.conf or apache2.conf whatever the case may be...
Options -Indexes
comments (1) flag fork

Hide Apache Version Header (plain)

1
2
3
4
# In httpd.conf or apache2.conf whatever the case may be...
# Ensure the following 2 lines are set:
ServerTokens ProductOnly
ServerSignature Off
comments (0) flag fork

Show Amount of RAM Used (plain)

1
2
3
4
5
# In MB
free -m

# In GB
free -g
[+] (1) more...
comments (1) flag fork

Display Top 3 RAM Hogs (plain)

1
ps auxf | sort -nr -k 4 | head -3 | awk '{print $11 $12}'
comments (1) flag fork

Display Zombie Processes (plain)

1
ps aux | awk '{ print $8 " " $2 }' | grep -w Z
comments (1) flag fork

Find Worst Indexes in all Databases (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
SQL script to grab the worst performing indexes in all databases
*/
SELECT
  t.TABLE_SCHEMA AS `db`
, t.TABLE_NAME AS `table`
, s.INDEX_NAME AS `index name`
, s.COLUMN_NAME AS `field name`
, s.SEQ_IN_INDEX `seq in index`
, s2.max_columns AS `# cols`
, s.CARDINALITY AS `card`
, t.TABLE_ROWS AS `est rows`
, ROUND(((s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) * 100), 2) AS `sel %`
FROM INFORMATION_SCHEMA.STATISTICS s
INNER JOIN INFORMATION_SCHEMA.TABLES t
  ON s.TABLE_SCHEMA = t.TABLE_SCHEMA
  AND s.TABLE_NAME = t.TABLE_NAME
INNER JOIN (
  SELECT
     TABLE_SCHEMA
   , TABLE_NAME
   , INDEX_NAME
   , MAX(SEQ_IN_INDEX) AS max_columns
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA != 'mysql'
  GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
) AS s2
ON s.TABLE_SCHEMA = s2.TABLE_SCHEMA
AND s.TABLE_NAME = s2.TABLE_NAME
AND s.INDEX_NAME = s2.INDEX_NAME
WHERE t.TABLE_SCHEMA != 'mysql'                         /* Filter out the mysql system DB */
AND t.TABLE_ROWS > 10                                   /* Only tables with some rows */
AND s.CARDINALITY IS NOT NULL                           /* Need at least one non-NULL value in the field */
AND (s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) < 1.00 /* Selectivity < 1.0 b/c unique indexes are perfect anyway */
ORDER BY `sel %`, s.TABLE_SCHEMA, s.TABLE_NAME          /* Switch to `sel %` DESC for best non-unique indexes */
LIMIT 20;
/*
Change this row limit to whatever you prefer
...
*/
[+] (36) more...
comments (1) flag fork

Find Fragmented Tables (plain)

1
2
3
SELECT table_schema,table_name,data_free
FROM information_schema.tables where table_schema
NOT IN ('information_schema','mysql') AND data_free > 0 ORDER BY table_schema, data_free DESC;
comments (0) flag fork

Undo potential damage caused by Magic Quotes (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (get_magic_quotes_gpc() == 1) {
    // Magic quotes *should* be off, but just in case...
    $superglobals = array(&$_REQUEST, &$_GET, &$_POST, &$_COOKIE);
    foreach ($superglobals as &$superglobal) {
        $superglobal = removeMagicQuotes($superglobal);
    }
}

function removeMagicQuotes($data) {
    if (is_array($data) == false) {
        $data = stripslashes($data);
    } else foreach ($data as &$value) {
        $value = Helper::removeMagicQuotes($value);
    }
    return $data;
}
[+] (12) more...
comments (1) flag fork

Create SVN repository and users (plain) (source)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
svnadmin create /path/to/<repo_name>
chown -R apache:apache /path/to/<repo_name>

vi /path/to/<repo_name>/conf/svnserve.conf
auth-access = write
password-db = passwd

<Location /svn>
    DAV svn
    SVNPath /home/svn/<sitename>
    Authtype Basic
    AuthName "My Repository"
    AuthUserFile /home/svn/mofodata/conf/passwd
    Require valid-user
</Location>
[+] (11) more...
comments (0) flag fork

List and remove a stash (plain)

1
2
git stash list
git stash drop stash@{0}
comments (0) flag fork

Remove working directory changes and reset to last commit (plain)

1
git reset --hard
comments (0) flag fork

Select Elements That Have an id Attribute Specified (plain)

1
2
3
4
5
6
7
8
9
10
$.extend($.expr[':'],
{
    hasId: function(el)
    {
        return $(el).attr('id') !== undefined && $(el).attr('id') !== '';
    }
});

// usage
$("#container :hasId");
[+] (6) more...
comments (0) flag fork

Select Elements That Have the Specified Color Value in rgb(x, x, x) Format (plain)

1
2
3
4
5
6
7
8
9
10
11
$.extend($.expr[':'],
{
    color: function(el, i, m)
    {
        if(!m[3]) {return false;}
        return $(el).css('color') === m[3];
    }
});

// usage
$("#container :color(rgb(255, 0, 0))");
[+] (7) more...
comments (0) flag fork

Select Elements That Have a CSS font-weight of 700 (bold) (plain)

1
2
3
4
5
6
7
8
9
10
$.extend($.expr[':'],
{
    isBold: function(el)
    {
        return $(el).css("fontWeight") === '700';
    }
});

// usage
$("#container :isBold");
[+] (6) more...
comments (0) flag fork

Select Elements Larger Than the Specified Element (plain)

1
2
3
4
5
6
7
8
9
10
11
$.extend($.expr[':'],
{
    largerThan: function(el, i, m)
    {
        if(!m[3]) {return false;}
        return $(el).width() * $(el).height() > $(m[3]).width() * $(m[3]).height();
    }
});

// usage
$("#container :largerThan(#element)");
[+] (7) more...
comments (0) flag fork

Selects All Anchors With the Specified Target Value (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
$.extend($.expr[':'],
{
    target: function(el, i, m)
    {
        if(!m[3]) {return false;}
        return (m[3] === '_self' && ($(el).attr('target') == '' || !el.target)) || (m[3] === $(el).attr('target'));
    }
});

// usage
$("#container :target(_self)");
$("#container :target(_parent)");
$("#container :target(frameName)");
[+] (9) more...
comments (0) flag fork

Selects All Anchor Tags With a Domain Different Than the Current Domain (plain)

1
2
3
4
5
6
7
8
9
10
11
$.extend($.expr[':'],
{
    external: function(el)
    {
        if(!el.href) {return false;}
        return el.hostname && el.hostname !== window.location.hostname;
    }
});

// usage
$("#container :external");
[+] (7) more...
comments (0) flag fork

Selects all Elements to the Right of the Specified Element (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
$.extend($.expr[':'],
{
    rightOf: function(el, i, m)
    {
        var oe = $(el).offset();
        var om = $(m[3]).offset();

        return oe.left > om.left + $(m[3]).width();
    }
});

// usage
$("#container :righttOf(#element)");
[+] (9) more...
comments (0) flag fork

Selects all Elements to the Left of the Specified Element (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
$.extend($.expr[':'],
{
    leftOf: function(el, i, m)
    {
        var oe = $(el).offset();
        var om = $(m[3]).offset();

        return oe.left + $(el).width() < om.left;
    }
});

// usage
$("#container :leftOf(#element)");
[+] (9) more...
comments (0) flag fork

Select Elements With HeightGreater Than or Less Than the Specified Value (plain)

1
2
3
4
5
6
7
8
9
10
11
12
$.extend($.expr[':'],
{
    height: function(el, i, m)
    {
        if(!m[3]||!(/^(<|>)\d+$/).test(m[3])) {return false;}
        return m[3].substr(0,1) === '>' ? $(el).height() > m[3].substr(1) : $(el).height() < m[3].substr(1);
    }
});

// usage
$(".container:height(>100)");
$("#container :height(<100)");
[+] (8) more...
comments (0) flag fork

Select Elements With Width Greater Than or Less Than the Specified Value. (plain)

1
2
3
4
5
6
7
8
9
10
11
12
$.extend($.expr[':'],
{
    width: function(el, i, m)
    {
        if(!m[3]||!(/^(<|>)\d+$/).test(m[3])) {return false;}
        return m[3].substr(0,1) === '>' ? $(el).width() > m[3].substr(1) : $(el).width() < m[3].substr(1);
    }
});

// usage
$(".container:width(>100)");
$("#container :width(<100)");
[+] (8) more...
comments (0) flag fork

Check if an Element is "loaded" (plain)

1
2
3
4
5
6
7
8
$.extend($.expr[':'],
{
    loaded: function(el) { return $(el).data('loaded'); }
});

// usage
$("img").load(function(){ $(this).data("loaded", true); });
$("#container img:loaded");
[+] (4) more...
comments (1) flag fork

Load jQuery From Google With Graceful (local) Fallback (plain)

1
2
3
4
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
    window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>');
</script>
comments (0) flag fork

Prevent Hotlinking (plain)

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/ .*$ [NC]  
RewriteRule \.(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
comments (1) flag fork

Set up a Default Image (plain)

1
2
3
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
comments (1) flag fork

Strip Query Strings (plain)

1
2
3
RewriteEngine On
RewriteCond %{QUERY_STRING} example=  
RewriteRule (.*) http://www.domain.com/$1? [R=301]
comments (1) flag fork

Block User Agents (plain)

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} UserAgentA [OR]  
RewriteCond %{HTTP_USER_AGENT} UserAgentB
RewriteRule ^/* http://www.domain.com/sorry.html [L]
comments (1) flag fork

Block a Specific IP Address (plain)

1
2
3
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^(A\.B\.C\.D)$
RewriteRule ^/* http://www.domain.com/sorry.html [L]
comments (1) flag fork