Knowledge – Binary Robot http://binary-robot.com Thu, 22 Aug 2013 21:37:45 +0000 en-US hourly 1 https://wordpress.org/?v=4.6.29 What do margin:5px 0; and margin:5px 0 0; mean? http://binary-robot.com/what-do-margin5px-0-and-margin5px-0-0-mean/ Wed, 27 Mar 2013 21:31:38 +0000 http://binary-robot.com/?p=114 According to Box Model:

  • If there is only one value, it applies to all sides.
  • If there are two values, the top and bottom margins are set to the first value and the right andleft margins are set to the second.
  • If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third.
  • If there are four values, they apply to the toprightbottom, and left, respectively.
body { margin: 2em }         /* all margins set to 2em */
body { margin: 1em 2em }     /* top & bottom = 1em, right & left = 2em */
body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */

This is defined by the CSS standard, so it should be consistent across all browsers that implements CSS correctly. For browser compatibilities, check out blooberry’s CSS Support History and quirksmode. According to blooberry, margin was first implemented in IE3, so IE6 should be fine.

]]>
Test your sendmail functionality from within PHP http://binary-robot.com/test-your-sendmail-functionality-from-within-php/ Tue, 26 Mar 2013 01:29:23 +0000 http://binary-robot.com/?p=106 Some people have reported problems with sending mail from PHP.

This appears to be particularly prevalent on Windows servers but also on some Linux and other Unix servers.

The attached scripts are small PHP programs you can install on your web server to test whether it can send email from PHP (and whether you actually receive the mail).

Here’s what to do:

  1. Download testmail.php.
  2. Edit it and enter your own email address where the script tells you.
  3. Upload it to your web server (call it “testmail.php”).
  4. Open the script with your web browser.
  5. If it tells you the mail function failed OR you don’t receive the test message,
  6. repeat the above steps with testmail2.php. If neither work, you can contact your hosting provider and ask them why these simple scripts don’t work.

If testmail.php doesn’t work but testmail2.php does work, you’ll need to set SENDMAIL_F_OPTION to “true” in formmail.php. In the configuration section look for:

PHP Code:
define("SENDMAIL_F_OPTION",false);
and change it to:

PHP Code:
define(“SENDMAIL_F_OPTION”,true);
(You need FormMail version 4.09 or later to use the SENDMAIL_F_OPTION feature.)

If you have version 7.11 or later then set SET_SENDER_FROM_EMAIL to “true” too. Look for:

PHP Code:
define("SET_SENDER_FROM_EMAIL",false);
and change it to:

PHP Code:
define("SET_SENDER_FROM_EMAIL",true);

 

Download –> [download id=”1″]

]]>
Singleton Class http://binary-robot.com/singleton-class/ Fri, 08 Mar 2013 02:32:08 +0000 http://binary-robot.com/?p=65 The Singleton Design Pattern ensures that only a single instance of a given object can exist.

It does this by making the class constructor private so that it [the singleton itself] has full control over when the class instance is created.  In order to gain access to an instance of a class that implements the Singleton pattern, the developer must call a shared/static method of that Singleton class.

A VB example of a Singleton

Public Class SingletonSample

 

‘shared members

Private Shared _instance As New SingletonSample

 

Public Shared Function Instance() As SingletonSample

Return _instance

End Function

 

‘instance members

Private Sub New()

‘public instantiation disallowed

End Sub

 

‘other instance members

‘…

 

End Class

A C# example of a Singleton

public class SingletonSample

{

//shared members

private static SingletonSample _instance = new SingletonSample();

 

public static SingletonSample Instance()

{

return _instance;

}

 

//instance members

private SingletonSample()

{

//public instantiation disallowed

}

 

//other instance members

//…

}

 

With the Singleton Design Pattern in place, the developer can easily access that single object instance without needing to worry about inadvertently creating multiple instances, and provides a global point of access to it.

VB – Dim mySingleton As SingletonSample = SingletonSample.Instance

C# – SingletonSample mySingleton = SingletonSample.Instance();

]]>
Integrated Active Directory Security in ASP.Net http://binary-robot.com/integrated-active-directory-security-in-asp-net/ Fri, 08 Mar 2013 02:30:45 +0000 http://binary-robot.com/?p=62 This describes how to secure your Asp.Net application if you’re inside an Active Directory environment, and you want to use the integrated sign on, so your users don’t have to remember yet another login/password. How to limit access to your application This explains how to limit access to your application to members of a certain role/group. First you need to change your web.config so that it uses windows authentication, and make it only allow members of a specific Active Directory group. Here is the cut down web.config, showing the changes to the system.web section:

<?xml version="1.0"?> 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
  ...etc... 
  <system.web> 
    <authentication mode="Windows" /> 
    <authorization> 
      <allow roles="MYDOMAIN\MyAppUsers" /> 
      <deny users="*" /> 
    </authorization> 
  </system.web> 
</configuration>

Then you want to make a landing page for users who aren’t allowed to log in. This is just a plain HTML page, you can call it ‘noaccess.htm’. Now you want to make it send disallowed people to this ‘noaccess.htm’. Open or create the Global.asax page, and change/add this function:

// This redirects people without access to the noaccess.htm page 
protected void Application_EndRequest(Object sender, EventArgs e) 
{ 
  HttpContext context = HttpContext.Current; 
  if (context.Response.Status.Substring(0, 3).Equals("401")) 
  { 
    context.Response.ClearContent(); 
    context.Response.Write("<scr" + 
      "ipt language=javascript> self.location='noaccess.htm'; </sc" + 
      "ript>"); 
  } 
}

How to figure out who is logged in Quite simple this one – it is stored in this variable, accessible from your aspx.cs code-behind classes (Update – FIXED):

HttpContext.Current.Request.ServerVariables["AUTH_USER"];

Say you’ve got a literal control on your webform, and you want to show the currently logged on user, you could do this:

literalLoggedOnUser.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"];

How to give different permissions to different users Say you want to give some people more access than others, you’ll want to create another AD group for these users: “MYDOMAIN\MyAppAdmins”. Open up your web.config and add the appSettings section like so:

<?xml version="1.0"?> 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
  <appSettings> 
    <add key="AdminGroup" value="MYDOMAIN\MyAppAdmins"/> 
  </appSettings> 
  ...etc... 
</configuration>

The reason for adding the name of the admin group in the web.config is so that if you have different AD groups for the development vs production environments, it’ll be a simple matter of only changing the web.config. The principle adhered to here is that the only difference between development and production should be the web.config file. Then you’ll use this piece of code to check if the current user is in that group:

string group = ConfigurationManager.AppSettings["AdminGroup"]; 
if (User.IsInRole(group)) 
{ 
  // this user is an administrator 
} 
else 
{ 
  // this user is a common pleb 
}

Hint: You’re most likely to place code like that in the Page_Load function for the admin pages. Notes This is for C# ASP.Net 2. Some things may be different with VB.Net or .Net 1.1 Redirect to noaccess.htm:

 

credit: http://www.splinter.com.au/easy-integrated-active-directory-security-in/

]]>