To change or replace the "innerHTML" of an iframe on a page, use the following:
onClick="window.frames['myIframeID'].document.body.innerHTML = 'New html content!';
May 25, 2011
Accessing the HTML of an iframe element in Javascript
May 20, 2011
Updating _SESSION variables from old PHP v4 script
Quick way to update code that reads ‘session_is_registered(‘foo’) to isset($_SESSION['foo']) for PHP5 usage:
In vi type:
:%s/session_is_registered(\"\([a-zA-Z]*\"\))/\isset($_SESSION\[\'\1\'\])/
May 6, 2011
Tot Verbum – A Latin gibberish generator
This might seem silly to many people, but it’s really handy when you just need text to fill in a form or something for testing!
Convert time to/from UNIX Epoch timestamp
Here’s a simple online form to convert a UNIX timestamp to a human readable date
Enter the timestamp:
April 14, 2011
March 31, 2011
rsync backups over ssh
I always seem to forget how to do this, so here it is:
To copy files from /home/pvint/stuff directory to the /opt/backup directory on a remote server:
rsync -avz --progress /home/pvint/stuff -e ssh pvint@theBackupServer.calm:/opt/backup
Or going the other way, pull from remote server:
rsync -avz --progress -e ssh pvint@theBackupServer.calm:/opt/backup/stuff /home/pvint/
Notes:
- The -a option is for "archive" and does a few things, like copies symlinks as symlinks, preserves owenership (only if you are running as root though!)
- -v is for Verbose, -z compresses as it transfers
- A note regarding trailing slashes: rsync -avz –progress /home/pvint/stuff -e ssh pvint@theBackupServer.calm:/opt/backup will copy the files to /opt/backup/stuff/files… whereas adding the trailing slash to /home/pvint/stuff/ will copy the files and directories in the 'stuff' directory directly to the /opt/backup directory (omitting adding the 'stuff' directory)
March 17, 2011
Adding and Removing a Javascript Object’s Properties
You can add and remove a Javascript object’s properties “on the fly”:
var dog = new Object();
dog.frontLegs = 2; // adds a frontLeg property to the dog object
dog.backLegs = 2;
delete dog.frontLegs; // removes the frontLegs (poor dog!)
document.write('This dog has ' + dog.frontLegs + ' front legs and ' + dog.backLegs + ' back legs');
// will output "This dog has undefined front legs and 2 back legs"
This can be quite useful for using an object as a container holding other objects, much like a std::list in C++ STL. Example:
function Dog()
{
this.name = "noname";
}
var myDog = new Dog();
var yourDog = new Dog();
var dogs = new Object();
dogs.myDog = mydog;
dogs.yourDog = yourDog;
// do stuff with list of dogs....
March 9, 2011
Incremental “find as you type” search with AJAX/PHP
Here’s a simple way to handle a “search as you type” input field using javascript that avoids the issue of overloading the client, network, and server by sending requests for every keypress. Technically, this is a “find when you pause” way of doing it, but it seems to work rather intuitively in that when someone slows down it’ll do the search.
This is non-functional until you add an appropriate function (searchProfile() in this example), and is merely here as a simple example.
For reference, see the source below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <INPUT TYPE='text' SIZE=25 ID='email' onKeyUp=" if(this.value.length > 2) // only check when 3 or more characters { var nowTime = new Date().getTime(); // ensure that 200ms has elapsed since last keypress if((nowTime - lastKeyPress) > 200) { // set var lastKeyPress=0 as a global variable searchProfile(this.id,this.value); // run the method that does the AJAX call for the search return false; } else { lastKeyPress = nowTime; return false; } } else { replaceDiv('resultsList',''); // calls function that displays the results in a div }"> |
You can adjust the timing to your preference – I find that 200ms works well, as it doesn’t require much of a pause to start and yet doesn’t fire the search very often as a person is typing.
In the interest of keeping code readable and reusable, I would advise moving this out of the onKeyUp in the input tag to a function.
500 – Intermittent Internal Server Error when posting form – Apache
If you find that certain forms cause an “Internal Server Error” (500) in Apache on occasion, a good thing to check is for mod_security configuration blocking a “potential threat”.
In my case, I saw the following in /usr/local/apache/logs/modsec2_audit.log:
[Wed Mar 09 13:36:34 2011] [error] [client 192.168.50.222] ModSecurity: Rule 7dd3610 [id "-"][file "/usr/local/apache/conf/modsec2/rootkits.conf"][line "155"] - Execution error - PCRE limits exceeded (-8): (null). [hostname "www.example.com"] [uri "/myFormScript.php"] [unique_id "AiGDV0PjjwgAAHwpOAoAAAAA"] [Wed Mar 09 13:36:34 2011] [error] [client 192.168.50.222] ModSecurity: Access denied with code 500 (phase 2). Pattern match "((select|grant|delete|insert|drop|alter|replace|truncate|update|create|rename|describe)[[:space:]]+[A-Z|a-z|0-9|\\*| |\\,]+[[:space:]]+(from|into|table|database|index|view)[[:space:]]+[A-Z|a-z|0-9|\\*| |\\,]|UNION SELECT.*\\'.*\\'.*,[0-9].*INTO.*FROM)" at REQUEST_BODY. [file "/usr/local/apache/conf/modsec2.user.conf"] [line "346"] [hostname "www.example.com"] [uri "/myFormScript.php"] [unique_id "AiGDV0PjjwgAAHwpOAoAAAAA"]
The solution (for me) for this problem was to whitelist my host (it’s an administrative tool in a CMS system, so it’s only used from certain IPs, so in /usr/local/apache/conf/modsec2/whitelist.conf I added the following line:
SecRule REMOTE_ADDR "^192.168.50.222$" noLog,allow
where 192.168.50.222 is the IP address of the machine that I access it from (not the server’s IP)
March 7, 2011
Linux: rsync file copy and backup over ssh
To copy files over the network from one machine to another:
Copy from remote server to local (pull):
rsync -avz -e ssh remoteuser@example.com:/home/source_directory /home/destination
Copy from local machine to remote hist (push):
rsync -avz /home/backups/source_directory -e sshremoteuser@example.com:/home/destination/directory

Like