Category Archives: Web Development - Page 2

Password Protect a Directory in Apache 2

For anyone who wants to do this, the steps are as follows:

  • Edit /etc/apach2/sites-available/default
  • Look for the line "AllowOverride" in the section <Directory /var/www>
  • Change the line from "AllowOverride None" to "AllowOverride All"
  • Change to the directory you wish to password protect
  • Create a .htaccess file. The contents of mine is shown below. The only truly important line is AuthUserFile. This parameter should contain the path to the file containing your usernames/passwords. You should be able to figure out what everything else is on your own.
  • Create the .htpasswd file in the directory of your choosing with the following command:
1
sudo htpasswd -c /path/to/.htpasswd username

My .htaccess file:

1
2
3
4
5
6
7
AuthUserFile /var/www/path/to/site/.htpasswd
AuthName "Please Log In"
AuthGroupFile /dev/null
AuthType Basic
<Limit GET POST PUT>
    Require valid-user
<Limit>

Sorting a XMLListCollection in Flex

Recently I had the need to sort and XMLListCollection in Flex. This seems to be a rather straightforward task. I had a XMLList where each item had an element called 'Image'. I wanted to sort by the value of the 'Image' node in descending order.

The original code was as follows:

1
2
3
4
5
6
var z:XMLListCollection = new XMLListCollection(xmlList);
var mySort:Sort = new Sort();
 
mySort.fields = [new SortField("Image",false,true)];
z.sort = mySort;
z.refresh();

This seemed to work fine for some time. Eventually, the client came back and alerted us that values were now be returned incorrectly. In fact, the set of values being returned was simply a repetition of the value of the first node of the first element. How can this be corrected? Quite simply actually. I first decided not to use the built in sorting capabilities of ActionScript. I found a function and modified the code as follows (the code I modified can be found <a href="http://www.nuff-respec.com/technology/sort-xml-by-attribute-in-actionscript-3">here</a>):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static function sortXMLByAttribute($xml:XMLList, $element:String, $options:Object = null):XMLList{
 
	//store in array to sort on
	var xmlArray:Array = new Array();
 
	for each(var item:XML in $xml){
		var object:Object = {
			data	: item,
			order	: item.elements($element)
		};
		xmlArray.push(object);
	}
 
	//sort using the power of Array.sortOn()
	xmlArray.sortOn('order',$options);
 
	//create a new XMLList with sorted XML
	var sortedXmlList:XMLList = new XMLList();
 
	for each(var xmlObject:Object in xmlArray ){
		sortedXmlList += xmlObject.data;
	}
 
	return sortedXmlList.copy();
}

The I changed my original code to the following:

1
var z:XMLListCollection = new XMLListCollection(sortXMLByAttribute(xmlList,"Image",Array.DESCENDING));

And that solved the problem. So why the issue in the first place? I'm not really 100% sure at the moment, but I'm looking into it. Maybe someone else out there has some input?

Flex Security Error Accessing URL or Channel.Security.Error

I had this wonderful error in a Flex Application that I couldn't figure out.  Every time the app attempted to access the Web Service it returned the error "Security Error Accessing URL". Well, thanks to this helpful site - http://talsma.tv/post.cfm/flash-9-0-124-and-webservice-over-https-channel-security-error - my problems have been solved.

Apparently there is a security issue with Flash 9.0.124 that requires the following line to be added to the CrossDomain.xml file:

<allow-http-request-headers-from domain="*" headers="SOAPAction"/>

Content-Disposition attachment vs inline

Today I ran into an interesting issue. We have some legacy code in .NET 1.1 that exports an HTML table to Microsoft Excel. This export occurs by simply rendering the table via Response.Write and setting the header content-disposition to "attachment; filename=FileName.xls". The original code looked something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")
Response.Charset = "utf-8"
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.ms-excel"
 
Dim stringWrite As IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
 
tblTable.RenderControl(htmlWrite)
 
Response.Write(stringWrite.ToString())
Response.Flush()
Response.End()

The problem that occurred was that any user using Internet Explorer (surprise, surprise!) would get a prompt to download the file but the file would not download! The file worked properly in all other browsers. The solution is to change

1
Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")

to

1
Response.AddHeader("content-disposition", "inline;filename=SalesByProductReport.xls")

Now, why exactly does this work? I'm not sure, so if you know please tell me.

ASP .NET Nested Forms (Or why do I get the error 'Invalid postack or callback argument')

I have now come across a few situations where using nested forms in ASP .NET causes problems. The typical error produced in this case is a 'Invalid postback or callback argument' error. This occurs because ASP .NET allows the rendering of multiple, nested forms but fails to validate the forms when a postback occurs. Thus when ASP .NET recognizes nested forms in a page, it marks the page as not valid (Page.IsValid returns false).

The reason that this error occurs is because multiple, nested forms cannot reside on a single aspx page .

The solution is very simple: The submit button for the user created form must contain the following attribute: onclick="this.form.submit();". A recent example I came across is below. The basic form that was developed and placed inside an aspx page as a nested form was as follows (only the beginning and ending of this form is shown. The content is not necessary or relevant):

1
2
3
4
5
6
7
8
9
10
11
12
<form accept-charset="UTF-8" action="http://www.response-o-matic.com/mail.php" method="post" enctype="multipart/form-data">
<table>
<tbody>
...
<tr>
<td colspan="2" align="center">
<input value="Submit Form" type="submit" />
</td>
</tr>
</tbody>
</table>
</form>

The following code demonstrates the proper way of entering this form so that nested forms will work. Please note that I reiterated all of the attributes (action, method, enctype, etc...) of the form in the JavaScript call.

1
2
3
4
5
6
7
8
9
10
11
12
<form accept-charset="UTF-8" action="http://www.response-o-matic.com/mail.php" method="post" enctype="multipart/form-data">
<table>
<tbody>
...
<tr>
<td>
<input onclick="this.form.action='http://www.response-o-matic.com/mail.php'; this.form.method='post';this.form.enctype='multipart/form-data';this.form.submit();" value=" Submit Form " type="submit" />
</td>
</tr>
</tbody>
</table>
</form>

SEO 301 Redirects for ASP .NET 1.1

I recently worked on a project where any URL following the form of http://www.mydomain.com/subDomain/Default.aspx needed to 301 redirect to http://www.mydomain.com/subDomain/. Basically this calls for stripping the 'Default.aspx' off of any request that has it. The project was to be completed using Visual Basic .NET 1.1.

The solution is  is to add a few lines of code in the Global.asax.vb file inside of the  Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) function. The final product looks like this:

1
2
3
4
5
6
7
8
9
Sub Application_BeginRequest(ByVal sender As Object, ByVal e AsEventArgs)
    Dim oPath As String = Request.CurrentExecutionFilePath.ToLower
    If Not oPath.EndsWith("default.aspx") Then Return
 
    Response.Status = "301 Moved Permanently"
    Response.AddHeader("Location", "http://www.myDomain.com" +  
    oPath.Substring(0, oPath.IndexOf("default.aspx")))
 
End Sub

Debugging .NET 1.1 With Visual Studio 2003 on Windows XP x64

In order to debug .NET 1.1 properly, it is necessary to uninstall the .NET 2.0 framework when you want to debug. The command to uninstall is:

C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_regiis.exe -u

The command to reinstall when your are done is:

C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_regiis.exe -i

Upload Large Files in ASP.NET

Here's the simple solution to uploading large files in ASP .net.

Add a new key in the web.config. The fileSize (maxRequestLength) is in kilobytes with a default size of 4.

The key to add is

1
<httpRuntime maxRequestLength="2000000"/>

Apache Compiler Error

While upgrading Apache on Mac OS X 10.4 I got the following error:

Cannot use an external APR-util with the bundled APR

The solution - Add this to configure options:

--with-included-apr

This issue is also addressed at the Apache Site