Found something useful? Don't forget to leave a comment!


Saturday, November 21, 2009

Better-looking HTTP Basic authentication with XmlHttpRequest

I’m sure everyone has encountered the good old browser username/password dialog on some website or another that uses HTTP authentication. While the built-in browser dialog works, it looks old-fashioned, somewhat out-of-place and would be unprofessional on a large-scale website. In almost any case, a form-based login hooked up to a backend like PHP/MySQL would be ideal, but this is not always an option. But thanks to a little JavaScript, HTTP Basic authentication can also be performed through a spiffy-looking form. Ease of integration is the main advantage – you can integrate the login form anywhere on your website. It works pretty well (see below on Firefox quirk).

So let’s get down to the nitty-gritty of setting this up. I will only be detailing the JavaScript and form code, but at the end you can find a nice, simple HTML page that integrates everything.

First, we construct the login form. Note that there is not actually a FORM element since nothing is being GETted or POSTed – we call the JavaScript function described later to process the input.

   1: <fieldset>



   2:     <legend><b>LOGIN</b></legend>



   3:     Username<br />



   4:     <input name="user" id="u" style="width: 10em" type="text" onkeypress="handleEnter(this, event)" /><br />



   5:     Password<br />



   6:     <input name="pass" id="p" style="width: 10em" type="password" onkeypress="handleEnter(this, event)"/><br />



   7:     <input name="submitBtn" type="submit" value="Enter" onclick="submitLogin()" /></fieldset>






Note the functionality of the submit button – it calls the submitLogin JavaScript function, described below. Also note the IDs of the username and password fields, u and p.



The heart of the login function is powered by a JavaScript XmlHttpRequest call. The implementation as described below works and has been test on IE7+, and versions of Firefox in recent memory. It should also function on Chrome and Safari (not tested).



The following code is XHTML-compliant and should be placed within the HEAD section of the HTML file.





   1: <script type="text/javascript">



   2: //<![CDATA[



   3:  



   4: function submitLogin() {



   5:  



   6: username = document.getElementById('u').value;



   7: password = document.getElementById('p').value;



   8:  



   9: addr = "Main/";



  10: if (username.length == 0 || password.length == 0)



  11: { return false; }



  12: xmlhttp = new XMLHttpRequest();



  13: xmlhttp.open("HEAD", addr, false, username, password);



  14: xmlhttp.send();



  15: if (xmlhttp.status == 200)



  16: {



  17:     document.location = "Main/";



  18: }



  19: else



  20: {



  21:     alert(xmlhttp.status + ": Login failed. Check your credentials.");



  22: }



  23: return false;



  24: }



  25:  



  26: //]]>



  27: </script>




How it works:




  • We grab the username and password from the page elements with IDs of ‘u’ and ‘p’, corresponding to the fields we constructed above.


  • ‘addr’ corresponds to the password-protected directory or resource we are trying to access.


  • If any of the fields are blank, do nothing.


  • Construct and activate the XmlHttpRequest object using a header-only call to the address, passing the credentials the user supplied.


  • If we get a 200 OK response from the server, take the user to the protected page.


  • If not, pop up an error. (On Firefox, an incorrect username/password will pop up the browser dialog. The user will not see the error until they click ‘Cancel’)



Enjoy your shiny new login form! This works great for light applications that do not require full-blown DB authentication but still need a more user-friendly solution. I am currently implementing this as a front page for my Windows HFS file server.



*Thanks to Leo Vildosola for his Windows Live Writer Code Snippet plugin!

Sunday, November 8, 2009

DFI's Mini-ITX P55 motherboard

techPowerUp! News :: DFI Prepares First P55 Mini-ITX Motherboard

This is definitely one of the more exotic pieces of tech, but surely it would make for an awesome LAN party mini-rig. The PCIe x16 2.0 slot is pretty much required since the P55 has no IGP. They even claim that the board will have overclocking potential. That's all well and good, but just try finding a mini-ITX case with the airflow needed to cool a hot-running i5/i7 and the high-powered GPU you're likely going to stuff inside.

Saturday, November 7, 2009

TI modernizes calculator software with Windows 7 and x64 support

TI Connect Adds 64-Bit, Windows 7 Support - ticalc.org

TI Connect is an OK piece of software if you discount the fact that it flaked out on any OS past Windows XP. Users had to resort to workarounds like running XP in a virtual machine and bypassing the host USB interface in order to connect the calculator to the VM. Thankfully, TI has turned from suck-ass to something more like kick-ass with this new 1.6.1 beta patch. To be honest, though, I haven’t done any serious programming/linking/syncing for my TI-84 or TI-89 for quite a while.

Sunday, November 1, 2009

Create CUE files from dBpoweramp CD rips with dbpa2cue

EAC is undoubtedly a powerful tool for ripping CDs, thanks to its vast number of configuration options and support for features like secure mode, AccurateRip database checking, gap detection, and CUE file generation. For the most part, dBpoweramp is a similarly awesome CD ripping tool – although it’s not free, its flexible codec system and no-duh GUI make for a great combination of performance and ease-of-use. It does, however, lack the aforementioned ability of EAC to create CUE files, which can be used to re-burn ripped audio files back to a disc.

That’s where my new dbpa2cue tool comes in. When secure mode is used, dBpoweramp can be configured to write a detailed logfile with ripping information. dbpa2cue is able to read the relevant per-track information from the logfile and use this data to write a viable CUE file. If the ripped audio files are still present, dbpa2cue can optionally read tag data and add additional metadata to the CUE file.

The core of dbpa2cue utilizes libDBPA, my DBPA log processing library, originally written for the earlier dbpa2eac program (which was taken down following a complaint from a what.cd moderator). You will need the .NET Framework, 2.0 or later.

The GUI of dbpa2cue is very easy to use – simply browse for, drag and drop, or type in the path to a valid dBpoweramp ripping log. By default tagging data is used if possible.

Version 1.3 (released 1/13/2010) is now available for download:
http://www.box.net/shared/2pu0auhet4