Apache HTTP Logo

Setting Up LDAP With Active Directory On Apache

21st January 2008 - 5 minutes read time

Using a simple .htpasswd to password protect a directory or website is fine if you only have a few users, and they don't change very much. However, this quickly becomes impossible to maintain if you have lots of users. For example, if you wanted to secure access to the company Intranet you might spend quite some time trying to update your .htpasswd file. The best way to do this is to transfer all of the user administration over to an LDAP server and then get Apache to communicate with this directly. The Active Directory (AD) system that Microsoft uses allows LDAP communications, and as this is in use across many company networks it is an ideal candidate to use.

You first need to set of the LDAP modules on your Apache server. Uncomment or add the following lines in your http.conf file. You will need to make sure that the files actually exist as well.

PHP Logo

Alternate If Statements In PHP

20th January 2008 - 4 minutes read time

If you have programmed in PHP for any amount of time then you will be farmiliar with the if statement. The syntax is as follows:


if($something == $somethingelse){
  //do something
}elseif($something == $anotherthing){
  //do another thing
}else{
  // default action
}

The PHP engine also allows you to do what I call "lazy programming" where you don't need the curly braces. Only the line underneath the if statement is run if the if clause if met.


if($something == $somethingelse)
  // do something

The issue here is that when this is put into the rest of the program the code becomes almost unreadable and therefore unmaintainable. The curly brases make it much easier to see what a program is doing. For readability and maintenance, many developers consider it bad style not to include them.

PHP Logo

Aborting Connections In PHP

19th January 2008 - 3 minutes read time

Sometimes in PHP you will have to do some things that might take a little time. You will therefore have a little trouble with users closing the browser or moving to another page before the script has finished. In this case you will want to either continue to execute the script just shut it down depending on what the user has done.

PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Simply using an echo statement does not guarantee that information is sent. Use the flush() function after the echo call to force PHP to sent output information to the browser.

To run the script to the end no matter what the user has done use the ignore_user_abort() function with the parameter of true.

ignore_user_abort(true);
This might be useful if you are recording things to a database so that data integrity is still maintained.

JavaScript Working Text

18th January 2008 - 4 minutes read time

Letting the user now that something in the background is working is an essential part of website usability. If nothing at all happens then the user will more than likely either try again or go elsewhere. A good way of doing this is to have a little bit of text that says "Working" and animate dots behind it. Here is a function that will do this.


var strDots = '';
var dotLength = 10;
var timeout = 500;
function dots()
  { 
  strDots += '.';
  if ( strDots.length == dotLength ) {
    strDots = '.';
  }
  document.getElementById('working').innerHTML = "<b>Working"+strDots+"</b>";
  dotTimer = setTimeout('dots()', timeout);
}

You then need to add the following bit of HTML to your page to start the dots off. The first one will print off:


Working

Followed by:

PHP Logo

How To Read A Remote IP Address In PHP

16th January 2008 - 2 minutes read time

PHP keeps certain variables to do with server and networking in an associative array called SERVER. To find out the remote address of a user you can use the array identifier REMOTE_ADDR. This is used in the following manner.

$ipaddress = $_SERVER['REMOTE_ADDR'];

This IP address can be passed into the gethostbyaddr() function to find out host name associated with the specified IP address.

Apache HTTP Logo

Setting php.ini Location In Apache

15th January 2008 - 3 minutes read time

After installing PHP on Apache you can use the php.ini file to set various different options to do with PHP. When Apache starts it uses what is contained in this file to set up and run PHP.

On both Windows, Unix and Linux systems Apache will look in a number of default locations for the php.ini file before giving up. You can explicitly tell Apache 2.x where to look for the file by using the PHPIniDir directive in the http.conf file.


#
# This is the directory containing php.ini
#
PHPIniDir "/usr/local/apache/conf"

For Apache 1.3.x this can be set using the SetEnv PHPRC directive.


# specify the directory where php.ini is
SetEnv PHPRC /usr/local/apache/conf

This not only speeds up the time taken for Apache to start, but will also allow you to make sure that the php.ini file you are using is the one you are editing.

JavaScript Logo

Some Useful Maths Functions In JavaScript

14th January 2008 - 6 minutes read time

All of the maths functions in JavaScript are kept in a handy object called Math, which contains a number of different functions.

To get the absolute value of a number use the abs() function.


Math.abs(3.14159265) // returns 3.14159265

Rounding a number is done by either the round() function to round to the nearest integer, the ceil() function to round up to the nearest integer and the floor() function to round down to the nearest integer.


Math.ceil(3.14159265)    // returns 4
Math.floor(3.14159265)   // returns 3
Math.round(3.14159265) // returns 3

To find the exponent of a number use the exp() function.


Math.exp(3.14159265) // returns 23.140692549708973

The log() method returns the natural logarithm (base E) of a number.

PHP Logo

Redirecting The Page In PHP

13th January 2008 - 3 minutes read time

To redirect the current page to a different location you use the header() function in the following way:

header("http://www.hashbangcode.com");

You can use this function when you want to point the user to a different page. If you are writing a login script then this function would be useful to show the user a certain page depending on them entering the correct user information.

PHP Logo

Highlight Code In PHP

12th January 2008 - 3 minutes read time

When printing off source code there is a handy function that will parse the code and produce nice looking syntax highlighted code. There are actually two functions you can use. The highlight_string() function takes a string as a parameter and will print the highlighted code. The highlight_file() function takes a file name as a parameter, the contents of which are printed off with highlighted syntax. For now I will concentrate on the highlight_string() function, but the output of these two functions is the same.

To use the highlight_string() function just pass it a string. The following code:


highlight_string('<?php 
function checkslash($slashes_string){
  if (get_magic_quotes_gpc()==1) {
    return $slashes_string;
  }else{
    return addslashes($slashes_string);
  };
};
?>');

Will produce the following output:

JavaScript Logo

Multiple Google Analytics On Same Page

10th January 2008 - 2 minutes read time

To set up multiple Google Analytics tags on the same page you need to use the _uff = false; command in between the unchinTracker() calls to reset the tracker for the next account. The urchinTracker() function will send information on the page visit off to Google Analytics.

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">
  _uacct = "UA-XXXXX"; // First account details
  urchinTracker();
  _uff = 0; // Reset tracker for second account
  _uacct = "UA-YYYYY"; // Second account details
  urchinTracker();
</script>

You can do this for as many accounts as you like, but be aware that there will come a point when there will be a noticeable delay on the site when the calls to Google Analytics are done so don't do too many.