Skip to content

Instantly share code, notes, and snippets.

@hcmn
hcmn / todo.php
Created September 24, 2012 00:38
PHP: very simple to-do list
<?php
// Database Constants
define("DB_SERVER", "127.0.0.1");
define("DB_USER", "root");
define("DB_PASS", "happy");
define("DB_NAME", "folio");
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if ( !$connection ) {
@hcmn
hcmn / gist:3772849
Created September 23, 2012 19:57
PHP: Sanitize and Validate User Input
source: http://stackoverflow.com/questions/129677/whats-the-best-method-for-sanitizing-user-input-with-php
also see: http://net.tutsplus.com/tutorials/php/sanitize-and-validate-data-with-php-filters/
"It's a common misconception that user input can be filtered. PHP even has a (now deprecated) "feature", called magic-quotes, that builds on this idea. It's nonsense. Forget about filtering (Or cleaning, or whatever people call it).
What you should do, to avoid problems is quite simple: Whenever you embed a string within foreign code, you must escape it, according to the rules of that language. For example, if you embed a string in some SQL targeting MySql, you must escape the string with MySql's function for this purpose (mysql_real_escape_string).
Another example is HTML; If you embed strings within HTML markup, you must escape it with htmlspecialchars. This means that every single echo or print statement should use htmlspecialchars.
A third example could be shell commands; If you are going to embed strings
@hcmn
hcmn / index.html
Created July 28, 2012 02:53
JQuery: .attr() example where callback is in action.
<!DOCTYPE html>
<!-- from JQuery API -->
<html>
<head>
<style>
div { color:blue; }
span { color:red; }
b { font-weight:bolder; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
@hcmn
hcmn / gist:3132602
Created July 17, 2012 22:35
HTML5: data type attribute
//"Custom data attributes are intended to store custom data private to the page or application,
// for which there are no more appropriate attributes or elements."
// - dev.w3.org/html5/spec/elements.html#embedding-custom-non-visible-data-with-the-data-attributes
//Examples below:
<div data-type="sprite" data-offsetY="100" data-Xposition="50%" data-speed="2"></div>
@hcmn
hcmn / gist:3129527
Created July 17, 2012 13:59
JQuery: Callback example
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback) {
// Call our callback, but using our own instance as the context
callback.apply(this, ['Hi', 3, 2, 1]);
}
function foo(salutation, three, two, one) {
alert(salutation + " " + this.name + " - " + three + " " + two + " " + one);
@hcmn
hcmn / gist:3129499
Created July 17, 2012 13:51
JQuery: Checking for existence & type of callback
function mySandwich(param1, param2, callback) {
alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);
if (callback && typeof(callback) === "function") {
callback();
}
}
mySandwich('ham', 'cheese', 'vegetables');
@hcmn
hcmn / sep_class_names.js
Created July 13, 2012 21:11
Javascript: separating multiple class names
// ----------------------------------------------------------------------------
// HasClassName
// credit: dzone.com
// Description : returns boolean indicating whether the object has the class name
// built with the understanding that there may be multiple classes
//
// Arguments:
// objElement - element to manipulate
// strClass - class name to add
//
@hcmn
hcmn / index.html
Created July 13, 2012 17:26
JQuery: Horizontal Navigation Tabs Controlling Content Area
<!DOCTYPE html>
<html>
<head>
<title>Menu Navigator</title>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src='script.js'></script>
</head>
<body>
<div class="tabbed-menu">
@hcmn
hcmn / panel.css
Created July 12, 2012 18:54
JQuery: Sliding Panel
#panel {
padding: 50px;
height: 100px;
width: 500px;
text-align: center;
font-size: 24px;
font-family: Arial;
font-weight: bold;
background: #EEE;
cursor: pointer;
@hcmn
hcmn / index.html
Created July 11, 2012 20:40
JQuery UI: tabs
<html>
<head>
<title>Sliders!</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/base/jquery-ui.css"/>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Nunc tincidunt</a></li>