Never been to DZone Snippets before?

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

About this user

Jeffrey Barke http://jeffreybarke.net/

« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS 

PHP headers to serve JSON

The first two headers prevent the browser from caching the response (a problem with IE and GET requests) and the third sets the correct MIME type for JSON.

   1  
   2  header('Cache-Control: no-cache, must-revalidate');
   3  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
   4  header('Content-type: application/json');

mssql_insert_id

// Duplicates functionality of
   1  mysql_insert_id()
while using SQL Server. From PHP manual.

   1  
   2  function mssql_insert_id() {
   3  	$id = false;
   4  	$res = mssql_query('SELECT @@identity AS id');
   5  	if ($row = mssql_fetch_row($res)) {
   6  		$id = trim($row[0]);
   7  	}
   8  	mssql_free_result($res);
   9  	return $id;
  10  }

Optimizing Mac OS X Mail.app

From Forever For Now:

Mail stores a so-called Envelope Index which contains cross references between message subjects, contents and other interesting aspects. Over time, this index becomes less and less efficient and slows down Mail.

The following action performed in the Terminal will clean up Mail's internal database, saving disk space and speeding up the application.

* Quit Mail from the menu or using Apple + Q and wait until the black arrow disappears from Mail's Dock icon.
* Open up Terminal using Spotlight via Alt + Space or start it from the Applications/Utilities folder.
* Type or copy the following line into the Terminal window:

   1  
   2  sqlite3 ~/Library/Mail/Envelope\ Index vacuum;

foreach like syntax in JavaScript

From justsomeguy's post to http://forums.invisionpower.com/lofiversion/index.php/t168975.html:

Since all JavaScript objects are really just associative arrays, there is a "foreach" like syntax for the "for" construct. Without it, it would be very hard to work with many common JavaScript objects. It's actually very simple to use and incredibly useful if you like associative arrays.

   1  
   2  //this is safe only if you can assure object has not been extended.
   3  var array = new Object();
   4  //use the below if Object has been extended
   5  var array;
   6  //everything below here works fine regardless of the two above cases
   7  array['something'] = 'foo';
   8  array['somethingelse'] = 'bar';
   9  for (keyVar in array) {
  10     alert(keyVar + '--' + array[keyVar]);
  11  }

301 redirect with PHP

For redirects on servers that have .htaccess locked down:

   1  
   2  header('HTTP/1.0 301 Moved Permanently');
   3  header('Location: http://new-page.com/');
   4  header('Connection: close');
   5  exit();

Redirect one domain to another using mod_rewrite

   1  
   2  Options +FollowSymLinks 
   3  RewriteEngine on
   4  
   5  RewriteCond %{HTTP_HOST} ^www.old-domain.com$ [NC]
   6  RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]

Strip HTML tags from strings using Classic ASP and regular expressions

From http://www.bpsdesigns.co.uk/tutorials/using-regular-expressions-with-classic-asp/

   1  
   2  Function stripTags(HTMLstring)
   3  	Set RegularExpressionObject = New RegExp
   4  	With RegularExpressionObject
   5  		.Pattern = "<[^>]+>"
   6  		.IgnoreCase = True
   7  		.Global = True
   8  	End With
   9  	stripTags = RegularExpressionObject.Replace(HTMLstring, "")
  10  	Set RegularExpressionObject = nothing
  11  End Function

Generate unique filenames in PHP

This code is from http://www.weberdev.com/get_example-3543.html.

It produces filenames similar to this: 4293d8fd-ab63-7c82.tmp

   1  
   2  function uniqueFilename($strExt = '.tmp') {
   3  	// explode the IP of the remote client into four parts
   4  	$arrIp = explode('.', $_SERVER['REMOTE_ADDR']);
   5  	// get both seconds and microseconds parts of the time
   6  	list($usec, $sec) = explode(' ', microtime());
   7  	// fudge the time we just got to create two 16 bit words
   8  	$usec = (integer) ($usec * 65536);
   9  	$sec = ((integer) $sec) & 0xFFFF;
  10  	// fun bit--convert the remote client's IP into a 32 bit
  11  	// hex number then tag on the time.
  12  	// Result of this operation looks like this xxxxxxxx-xxxx-xxxx
  13  	$strUid = sprintf("%08x-%04x-%04x", ($arrIp[0] << 24) | ($arrIp[1] << 16) | ($arrIp[2] << 8) | $arrIp[3], $sec, $usec);
  14  	// tack on the extension and return the filename
  15  	return $strUid . $strExt;
  16  }
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS