Showing posts with label tips 'n tricks. Show all posts
Showing posts with label tips 'n tricks. Show all posts

Thursday, March 19, 2009

PHP's _SERVER["DOCUMENT_ROOT"] no good for filesystem path with virtual directories

One thing that can bring a serious headache is if you use the PHP variable $_SERVER["DOCUMENT_ROOT"] to open files in the file system and your site is placed in an Virtual Directory in IIS.

This server variable points to the root of the server and this is not necessarily the path the virtual directory points to.

Of course this solution could work in the right conditions; hosted on a web server and placed in a physical directory under this path. However, if you want to have a flexible application and avoid issues if it is placed in a virtual directory, then do not use this variable.

An alternative could be to use:
dirname(__FILE__)
This method with the provided constant will return the directory where the current script file (also for included files) is located. Then you can point to this path relatively if it is a well known location such as your application root. Another benefit is that this constant is always available, as the _SERVER[] constants are depending on the host.

So here's my advise: try to avoid using the $_SERVER["DOCUMENT_ROOT"] variable for paths and use physical paths instead.

Tuesday, November 25, 2008

Create a CssStyleCollection instance programmatically

Update 2011-07-12: Refer to this answer on Stackoverflow for a more elegant solution.

The CssStyleCollection class is a very useful class in ASP.NET. It manages CSS nicely in a programmatic way. However, if you would like to create an instance of this class in your own code, you will find it rather impossible. The class is sealed, so there is no way to inherit it. Moreover, the constructors are private, so you cannot create a new object!

I've seen many questions about how to create an instance of this class in online forums when I searched for it myself today. The solution is to delve into the internals of the class with Reflection. In that way it is possible to call the two private constructors of the class.

You can use the code below:

using System.Reflection;
using System.Web.UI;

namespace Tools
{
public class CssStyleTools
{
/// <summary>
/// Creates an instance of the CssStyleCollection class by reflection.
/// </summary>
/// <returns>A new instance of the CssStyleCollection class.</returns>
public static CssStyleCollection Create()
{
return (CssStyleCollection)typeof(CssStyleCollection).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0].Invoke(null);
}

/// <summary>
/// Creates an instance of the CssStyleCollection class by reflection and stores view state in the provided state bag.
/// </summary>
/// <param name="state">The storage object for the state.</param>
/// <returns>A new instance of the CssStyleCollection class.</returns>
public static CssStyleCollection Create(StateBag state)
{
return (CssStyleCollection)typeof(CssStyleCollection).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[1].Invoke( new object[]{ state });
}
}
}

It is as simple as calling the following code:

CssStyleCollection css = CssStyleTools.Create();

Feel free to comment if it was any use to you.

Tuesday, August 12, 2008

Learn from your tracks

Why would you have a file on your system where status messages can be dumped? Because it is there to warn you. But what if that alarm bell is ringing, but you don't hear it? Problems will arise...

That's why you should check the log files for your applications from time to time. Or actually, make it a regular thing.

Know your tools and platforms, check in what ways there is logging.

For web development, these logs are interesting:
  • Windows Event Log - for ASP.NET exceptions
  • IIS Server Log
  • PHP Log
So, check what your creations are doing, and learn from the mistakes they make.

Tuesday, June 17, 2008

Using using's class alias declaration

One big annoyance I had for a long time, is that generic types can create such a long and cryptic declaration in your code. But today I discovered a hidden feature of the using declaration that makes coding so much easier.

using is not only useful for importing namespaces. You can use it to declare an alias (shortcut, abbreviaton) for a class name too.

It is very simple, here is an example:

using S = System.String;

If you use the type S in your code, then it will refer to the type System.String. Note that you have to fully qualify the name to the class.

Lets have a look at a more complex declaration:

using ComplexType = System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<int,object>;

With this declaration you save a lot of space in your code. The next example shows this, one statement uses the alias type and the other statement does not:

// Using the using class alias declaration.
ComplexType ct1 = new ComplexType();

// Using the regular declaration.
Dictionary<string,Dictionary<int,object>> ct2 = new Dictionary<string,Dictionary<int,object>>();


I can guess which one you like. ;-)

However, of course there are some drawbacks. They are the same as namespace import using statements.
  1. The using class alias declaration is only valid in the file where it is declared.
  2. You must specify the using class alias declaration before using it in the code.
Although, I think it is quite easy to overcome these, as you are already familiar using namespace import using statements.

For more information read the MSDN article using Directive (C#).