Archive for the ‘PHP Scripts’ Category

Application Access Levels with PHP

Saturday, February 28th, 2009

Hello all

This week we got a job in to write an intranet site for a large Insurance company in the UK.

It had 60 or so applications that needed writing with access to each being given on a per user basis.

So for example user “A” could see applications 1 and 3, and user B could only use application 2.

So how do you do this easily?

We started off having a link table, it had User ‘A’ mapped to application 1 and so on. Which is ok but they have 5000+ staff and 60 applications so that would be a table with 300,000 rows in it, which in its self in MySql is nothing really, it just seemed excessive.

What we came up with is a bitwise solution so each application had a bit assigned to it. So if you remember your binary “0 1 2 4” etc. Each application was assigned a binary number, so application 1 was “1” in binary, application 2 was “10” ,application 3 was “100” and so on

So if a user as an access level of 7, that would translate in to a binary number of 111 so they could access application 1, 2 and 3. If a user had an access level of 4 for example they could only access application 3.

Binary (Base 2) Application
0 Superuser
1 Application 1
2 Application 2
4 Application 3
8 Application 4

We will post the class over the next few weeks if any body else wants to use it.

Happy coding

Is a date in a given range using PHP

Monday, July 21st, 2008

To find if a date is in a given data range, the quikest way is to turn all dates to Epoch dates and then do a compare.

So to test if 31/12/2007 is in a date range of 10-10-2000 and 10-10-2008 do the following

$From=”31-12-2007″;
$To=”10-10-2008″;
$Checkdate=”01-01-2008″;

if (
( mktime(0,0,0, date(”m”,strtotime($From)),date(”d”,strtotime($From)),date(”Y”,strtotime($From)))
<=
mktime(0,0,0,date(”m”,strtotime($Checkdate)),date(”d”,strtotime($Checkdate)),date(”Y”,strtotime($Checkdate))) )

&&

(mktime(0,0,0,date(”m”,strtotime($To)),date(”d”,strtotime($To)),date(”Y”,strtotime($To)))
>=
mktime(0,0,0,date(”m”,strtotime($Checkdate)),date(”d”,strtotime($Checkdate)),date(”Y”,strtotime($Checkdate))) ) ) {

print “In Date Range”;

} else {

print “Not in Range”;
}

Hope this helps.

How to make php code run in .html files

Wednesday, July 2nd, 2008

This is a fairly simple one. If your website is hosted on an Apache Web Server then you can just edit or add a file called .htaccess. Inside this file you will need to add a line

addType application/x-httpd-php .php .phtml .html

This tells the web server that files ending in .php .phtml and .html are to be checked for PHP code.

By using the .htaccess method you will not need to restart apache to pick up the changes as apache looks for the .htaccess file each time a page is requested.

If you have to create an .htaccess file you will need to make sure the file permissions on it are 644. Or if setting via an ftp package Owner = Read/Write and the Group and Other will be set to READ only.

You should now be able to put sections anywhere in your html pages.

So why would you want to do this? There are a number of reasons, some of which are

  • Standard Header and Footers
  • Standard Menus
  • Displaying Date & Time
  • Displaying information from a database

Get Working Days between 2 dates with PHP

Monday, June 30th, 2008

We needed to work out the working days between two dates for a small calendar application we had to write. After looking round the net for a few minuets we decided to write a quick function for ourself.

It takes 3 inputs. A From Date (Epoch) , a To Date (Epoch) and an array with Bank Holiday dates in it. By the time it has finished it has a variable $WorkingDays with the number of working days in the give range.

The main logic goes like this

while ( $StartTime <= $EndTime ) {
if (
date(”D”,$StartTime) == “Sat” ||
date(”D”,$StartTime) == “Sun” ||
array_key_exists(date(”Ymd”,$StartTime),$BankHolidays) ) {

$IgnorDays++;

} else {
$WorkingDays++;

}

$StartTime=$StartTime+86400;
}

Why we like PHP

Saturday, June 28th, 2008

For many years we have used many different programming  languages  including COBOL , Rexx ,  VB , Perl .
About 6 years ago we decided to try PHP in place  of PERL for our CGI scripts and was amazed at the speed difference between the 2.

Even now we find new functions each day last one we found was the ” array_key_exists ” a very useful little function that saves checking all keys in an array.
We used it in a small calendar application for checking if a date was a bank holiday
More to Follow….