From Microsoft to Linux….almost

By StrangeWork.com: As most of you know I am in the process of starting my own Web Development Company in New Jersey. Starting a company takes a lot of work, but of course I knew this going into it. Business is great and we are starting to grow!

PHP LogoI’ve decided to shift focus on my technologies track a little. I’ve always programmed with Microsoft technologies including classic ASP, ASP.NET, VB, SQL Server, etc. I’ve been developing dynamic websites with classic ASP for almost 8 years now and I still love that language. I use ASP.NET occasionally when the project calls for it, but I love scripting languages. I’ve decided to focus my efforts on PHP rather than ASP.NET.

Scripting has always come natural to me, so why focus on a more object-oriented language? Exactly, so from this point on I will be primarily developing in ASP and PHP. The logic is the same as ASP, just different syntax so the switch won’t be too dramatic.

Expect to see some sweet PHP applications rolling out in the coming months!

How to: Update FriendFeed using ASP

By StrangeWork.com: Last week the popular feed aggregator FriendFeed launched their new API.

FriendFeed Logo The new API makes it easy for anyone to create custom applications to interact with FriendFeed.

Below is a script I wrote in ASP to post an image and link to your feed stream using XMLHTTP. I’ve included the complete script for download at the bottom of this post.

‘*** set your FriendFeed username and remote key variables
‘*** your users can obtain their remote key here: http://friendfeed.com/remotekey
ff_username = “username”
ff_remotekey = “remotekey”

‘*** set the URL you want to link to
ff_url = “http://snapfoo.com”

‘*** set the text to add to your post
ff_update = “Visit SnapFoo.com”

‘*** set the image URL to post to FriendFeed
‘*** this will be posted as a thumbnail on your feed
ff_filename = “http://snapfoo.com/images/snapfoo_logo.jpg”

‘*** package all of your variables in one variable to post to the FriendFeed API
ff_post = “http://” & URLEncode(ff_username) & “:” & URLEncode(ff_remotekey) & “@friendfeed.com/api/share?title=” & URLEncode(ff_update) & “&link=” & URLEncode(ff_url) & “&image0_url=” & URLEncode(ff_filename)

‘*** post the update
Set xml = Server.CreateObject(“Microsoft.XMLHTTP”)
xml.Open “POST”, ff_post, False
xml.setRequestHeader “Content-Type”, “content=text/html; charset=iso-8859-1”
xml.Send
Set xml = Nothing

That’s it! Using the above method you can easily create scripts to update FriendFeed with text, links, and photos!

Download Source File to Update FriendFeed using ASP

How to: Update Pownce using ASP

By StrangeWork.com: On February 28th, 2008 Pownce released version 2.0 of their API. The major update in this new version is the ability to post messages, links, files, events and even replies via the API directly to a Ponwce.com account.

Pownce LogoBelow is an ASP script I wrote to update your Pownce profile automatically through the new API. I’ve included the complete script for download at the bottom of this post.

Pownce requires the username and password values be encoded using base64. Below is an ASP function that will convert any value over to base64 encode.

const BASE_64_MAP_INIT = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”
dim nl
‘ zero based arrays
dim Base64EncMap(63)
dim Base64DecMap(127)

‘ must be called before using anything else
PUBLIC SUB initCodecs()
‘ init vars
nl = “

” & chr(13) & chr(10)
‘ setup base 64
dim max, idx
max = len(BASE_64_MAP_INIT)
for idx = 0 to max – 1
‘ one based string
Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
next
for idx = 0 to max – 1
Base64DecMap(ASC(Base64EncMap(idx))) = idx
next
END SUB

‘ encode base 64 encoded string
PUBLIC FUNCTION base64Encode(plain)

if len(plain) = 0 then
base64Encode = “”
exit function
end if

dim ret, ndx, by3, first, second, third
by3 = (len(plain) 3) * 3
ndx = 1
do while ndx <= by3 first = asc(mid(plain, ndx+0, 1)) second = asc(mid(plain, ndx+1, 1)) third = asc(mid(plain, ndx+2, 1)) ret = ret & Base64EncMap( (first 4) AND 63 ) ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second 16) AND 15 ) ) ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third 64) AND 3 ) ) ret = ret & Base64EncMap( third AND 63) ndx = ndx + 3 loop ' check for stragglers if by3 < len(plain) then first = asc(mid(plain, ndx+0, 1)) ret = ret & Base64EncMap( (first 4) AND 63 ) if (len(plain) MOD 3 ) = 2 then second = asc(mid(plain, ndx+1, 1)) ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second 16) AND 15 ) ) ret = ret & Base64EncMap( ((second * 4) AND 60) ) else ret = ret & Base64EncMap( (first * 16) AND 48) ret = ret & "=" end if ret = ret & "=" end if base64Encode = ret END FUNCTION ' initialize call initCodecs

Every site/script that interacts with the Pownce API requires an application key. To get your application key, simply register a new application on Pownce. Just replace {app_key} in the below script with the application key that Pownce provides to you.

In this example we will be posting a LINK to your Pownce profile. Be sure to reference the Pownce 2.0 API documentation for specifications on posting messages, events, files, etc.

‘*** set your Pownce username and password variables
p_username = “username”
p_password = “password”

p_username = p_username & “:” & p_password

‘*** Base64 encode your username and password
p_username = base64Encode(p_username)

‘*** replace {app_key} with YOUR application key
strPownce = “http://api.pownce.com/2.0/send/link.xml?app_key={app_key}”

‘*** the link you want to post to Pownce
pownce_link = “http://snapfoo.com/images/snapfoo_logo.jpg”

‘*** the description you want to post to Pownce
pownce_update = “SnapFoo.com Logo – Foo You!”

Set xml = Server.CreateObject(“Microsoft.XMLHTTP”)
xml.Open “POST”, strPownce, False
xml.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded”
xml.setRequestHeader “Authorization”, “BASIC ” & p_username
xml.Send(“note_to=public&url=” & pownce_link & “&note_body=”&pownce_update)

‘*** view the Pownce response
Response.Write xml.responseText

Set xml = Nothing

That’s it! Using the above method you can easily create scripts to update Pownce with anything you would like.

Download Source File to Update Pownce using ASP

The ASP.NET 2.0 Anthology Book from SitePoint

By StrangeWork.com: Yesterday I received my new book from SitePoint.com, The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks.ASP.NET 2.0 Anthology Book

After skimming through my new read I must say, SitePoint can write some amazing books! I love how each section is prefaced with a question:

“How do I read data from my database?”
“How do I use source control?”
“How do I require users to log in?”

This method seems to flow easier for me in understanding new development techniques and code.

If you are interested in learning ASP.NET 2.0 using C# or just looking to refine your ASP.NET 2.0 skills then this is a must read.

301 Permanent Redirect using ASP

By StrangeWork.com: I’ve been using my search engine optimization skills on a few different sites recently. The Democrats for Monmouth County website is my current challenge. One of the main challenges I faced was converting the old ugly links over to the new SEO friendly links without losing placement in the search results.

Link conversion example
Old URL:
http://mcds2007.com/Default.asp?P=393

New URL:
http://mcds2007.com/contact

To keep your existing search engine rank, use a Permanent 301 Redirect to force the spiders to the new link. Simply add the below code to the top of your ASP script:

Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “/contact”

Easy as that! Google, Yahoo, and the rest of the search engines will update your site links in their index without penalizing the new link.

How To: Update your Twitter status with ASP

Here is a simple script I wrote in classic ASP to update your Twitter status via the Twitter API. This script handles basic HTTP authentication to validate your Twitter account and URL Encoding to send over friendly status updates.

<%
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")

twitter_username = "username"	'change to your twitter username
twitter_password = "password"	'change to your twitter password

new_status = "visit strangework.com!"		'change to your new status

xml.Open "POST", "http://" & twitter_username & ":" & twitter_password & "@twitter.com/statuses/update.xml?status=" & server.URLencode(new_status), False
xml.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"
xml.Send

Response.Write xml.responseText		'view Twitter's response

Set xml = Nothing
%>

How To: Create Custom ASP URLs with Querystrings using ISAPI Rewrite

ISAPI Rewrite is a powerful URL manipulation engine based on regular expressions. It acts mostly like Apache’s mod_Rewrite, but is designed specifically for Microsoft’s Internet Information Server (IIS). ISAPI_Rewrite is an ISAPI filter written in pure C/C++ so it is extremely fast. ISAPI_Rewrite gives you the freedom to go beyond the standard URL schemes and develop your own scheme.

Below are a few very easy to follow ISAPI Rewrite rules using regular expressions:


EXAMPLE 1

DESCRIPTION:
use one querystring as a subdirectory

ORIGINAL URL:
domain.com/member.asp?username=brad

NEW URL:
domain.com/brad

ISAPI REWRITE RULE:
RewriteRule /([^/]+) /member.asp?username=$1 [I,L]


EXAMPLE 2

DESCRIPTION:
use two querystrings as subdirectories

ORIGINAL URL:
domain.com/member.asp?username=brad&page=2

NEW URL:
domain.com/brad/2

ISAPI REWRITE RULE:
RewriteRule /(?!images|js|css)([^/]+)/([^/]+) /member.asp?username=$1&page=$2 [I,L]

* notice the (?!images|js|css) section of the rule. This piece tells the above rule to ignore those subdirectories (images, js, css).


EXAMPLE 3

DESCRIPTION:
use one hard coded subdirectory and one querystring as a subdirectory

ORIGINAL URL:
domain.com/member.asp?user_id=1

NEW URL:
domain.com/widget/1

ISAPI REWRITE RULE
RewriteRule /widget/([^/]+) /member.asp?user_id=$1 [I,L]


Search engine spiders, and users, will ONLY see the newly formatted link. The querystrings are still being passed to the server, but they are not visible to anyone surfing the site. This is a HUGE advantage for making a dynamic site SEO friendly. Search engine spiders have always had issues with long complex querystrings, but masking the URL using ISAPI REWRITE has finally closed the gap between dynamic sites and SEO.

ISAPI Rewrite Homepage

How To: Create ASP and AJAX username availability check example

So you’re using Classic ASP and you want to incorporate some AJAX into your scripts? No problem! The below example shows how easily it is to include a username availability check on your site using classic ASP and AJAX.

The below example checks the username after each key is pressed rather than when the form is submitted. You will need to create two files: ajax.asp and ajax_username.asp

You can download the sample source files at the bottom of this post

ajax.asp code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<HEAD>
	<TITLE>ASP and AJAX username availability check</TITLE>
	<script language="javascript">
	function OnChangedUsername()
	{
		if(document.form1.newuserid.value == "")
			{
				document.form1.btnCheckAvailability.disabled = true;
			}
		else
		{
			document.form1.btnCheckAvailability.disabled = false;
		}
	}
	function createRequestObject() {
		var ro;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			ro = new XMLHttpRequest();
		}
		return ro;
	}

	var http = createRequestObject();

	function sndReq() {
		http.open('get', 'ajax_username.asp?username='+document.form1.newuserid.value);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}

	function handleResponse() {
		if(http.readyState == 4){
			var response = http.responseText;
			var update = new Array();

			if(response.indexOf('|' != -1)) {
				update = response.split('|');
				document.getElementById("username_chk").innerHTML = update[0];
			}
		}
	}
	</script>
</HEAD>
<BODY>

<form method="post" action="javascript:void(0);" name="form1">
	<table>
		<tr>
			<td><input type="newuserid" name="newuserid" id="newuserid" size="20" onKeyUp="sndReq();" /></td>
		</tr>
		<tr>
			<td><input id="btnCheckAvailability" type="button" disabled="disabled" value="Check Availability" onClick="sndReq();"></td>
		</tr>
		<tr>
			<td><div ID="username_chk"></div></td>
		</tr>
		<tr>
			<td>Brought to you by <a href="http://strangework.com" target="_blank">Brad Williams</a></td>
		</tr>
	</table>
</form>

</BODY>
</HTML>

ajax_username.asp code:

<%
Set username = Request.QueryString("username")

'*** START - SET YOUR DNS-LESS CONNECTION VARIABLES
db_username = "username"		'DB username
db_password = "password"		'DB password
db_catalog = "database_name"		'DB name
dp_datasource = "192.168.1.1"		'DB IP
'*** END - SET YOUR DNS-LESS CONNECTION VARIABLES


Set conn = Server.CreateObject("ADODB.Connection")
conn.CommandTimeout = 0
c="Provider=SQLOLEDB.1;User ID=" & db_username & ";password=" & db_password & ";Initial Catalog=" & db_catalog & ";Data Source=" & dp_datasource & ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096"
conn.Open c

'*** ADJUST THIS SELECT QUERY TO CHECK THE USERNAME AGAINST YOUR MEMBERS TABLE
SQL = "SELECT username FROM table WHERE username='" & username & "' "
Set chk_username = Server.CreateObject("ADODB.Recordset")
chk_username.Open SQL, conn, 3, 3
If chk_username.EOF = False then
	response.write "USERNAME ALREADY TAKEN"
Else
	response.write "USERNAME IS AVAILABLE"
End if

chk_username.close
set chk_username = nothing

conn.close
set conn = nothing
%>

Just adjust the DSN-Less connection settings and the SELECT query above and your all set!

If you would rather check a username after the submit button is pressed just change this line:

to this:

DISCLAIMER – There is no error handling, form validation, or query injection functions or routines in this script. This is a basic username checking script. Be sure to include these features if you use this code!

Feel free to do whatever you would like with this code. If this helps then you might consider sending a link back to strangework.com!

click here to download both source files

First ASP.NET App

So I’m back from training and using my new ASP.NET skills already! Take a look at our Designer Handbag store dPetruzzi.com. Notice at the bottom of the screen there is a footer bar that allows you to enter your email address. Well there it is! My first “live” ASP.NET app. It’s fairly simple, but the knowledge I gained in my class helped out tremendously!

Stay tuned. Next up is “tell a friend”

ASP.NET training

I’m going to an ASP.NET training course starting on Monday. The course is Developing Microsoft ASP.NET Web Applications Using Visual Studio .NET. I’m extremely excited about going to training. It’s been too long and I need to really start focusing on ASP.NET development in future projects.