Tag Archives: PHP

Changing the include_path

PHP has a feature to change the include_path programatically. The problem is that the path separator is : on unix and ; on windows. Luckily there is a constant PATH_SEPARATOR to overcome this issue. Here is how i would do it:

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$include_paths = array(
  '.',
  '/home/users/timvw/phpincs',
  '/home/users/timvw/pear',
  ini_get('include_path')
);

ini_set('include_path', implode(PATH_SEPARATOR, $include_paths));

?>

Introducing the masterpage

Most websites have the same layout and an area with dynamic content. So most people choose for the following solution: generate a couple of template files for the static content and then write the code for the dynamic content and include the static templates. Here is how the code for a contact and an aboutme page would look like:

contact.php
<?php
include('header.php');
include('leftpanel.php');
// code for contact page
include('rightpanel.php');
include('footer.php');
?>
aboutme.php
<?php
include('header.php');
include('leftpanel.php');
//something about me
include('rightpanel.php');
include('footer.php');
?>

This code is pretty flexible. But what happens if you want to change your layout to something without a rightpanel? Right, you have to edit each page and remove the include call. A couple of weeks ago i discovered the concept of tiles and immediately realised this is useful for php too. Here is an example implementation:

contact.php
<?php
require_once('init.php');
$tiles = array();
$tiles['main'] = PAGE_CONTENT . '/contact.html';
include('masterpage.php');
?>
aboutme.php
<?php
require_once('init.php');
$tiles = array();
$tiles['main'] = PAGE_CONTENT . '/aboutme.html';
include('masterpage.php');
?>
masterpage.php
<?php
if (!isset($tiles)) $tiles = array();
if (!isset($tiles['header']) $tiles['headers'] = PAGE_CONTENT . '/default-header.html';
if (!isset($tiles['leftpanel']) $tiles['headers'] = PAGE_CONTENT . '/default-leftpanel.html';
if (!isset($tiles['main']) $tiles['headers'] = PAGE_CONTENT . '/default-main.html';
if (!isset($tiles['footer']) $tiles['headers'] = PAGE_CONTENT . '/default-footer.html';
include($tiles['header']);
include($tiles['leftpanel']);
include($tiles['main']);
include($tiles['footer.php']);
?>

As you can see, this allows the programmer to change the layout in a single file, the masterpage. If a programmer wants to change the content of a specific area of a page all he has to do is change the $tiles array.

Odd behaviour with arrays

A while ago i was really stumbled by the behaviour of a server. This problem solved itself after the sysadmin noticed that he forgot to upgrade ionCube after a php upgrade.Here is the code that i ran:

$array = array();
$array[] = array('name' => 'row1', 'value' => '1');
$array[] = array('name' => 'row2', 'value' => '3');
$array[] = array('name' => 'row3', 'value' => '2');

foreach($array as $row)
{
  print_r($row);
  echo "<br />";
}

The expected output is:

Array ( [name] => row1 [value] => 1 )
Array ( [name] => row2 [value] => 3 )
Array ( [name] => row3 [value] => 2 )

For some odd reason this is the output i got:

Array ( [0] => Array ( [name] => row1 [value] => 1 ) [1] => 0 )
Array ( [0] => Array ( [name] => row2 [value] => 3 ) [1] => 1 )

Currency convertor

I discovered that the European Central Bank (ECB) has a page with Euro foreign exchange rates. I thought it would be a nice exercise to write a little script that works with the XML data source. Get currencyconvertor.txt now!

Tests if $host is a proxy server

Generating JavaScript strings

Well, I’ve always experienced the generating JavaScript strings with PHP as a PITA. An example, which requires you to take care of the escaping of quotes, is the string: ‘O’Reilly has nice books’. Today i had this brilliant idea to do it as following:

<?php
$str = addslashes("Hello peter's cats");
echo "<script type='text/javascript'>";
echo "alert('$str')";
echo "";
?>

Read from STDIN without echoing the input back

Today i was looking for a way to read passwords from a PHP-CLI script. So it was important the password didn’t appear on the console. I wrote a ttyecho function that uses stty to change the terminal line settings.

Scriptable browser

Last couple of days i’ve been trying out Simple Test. It allowed me stop stop echo and print_r variables all over the place. The package also has a Scriptable Browser.

At smscity.be you can earn credits each day. Therefor you have to visit their site and click some links. I wrote a smscity.txt script that does this for me.

Now all i had to do is make sure this script is executed each day, so i edited my crontab. It looks like:

###############################################################################
#
# Author: Tim Van Wassenhove
#
###############################################################################
# $Id:
###############################################################################

  @reboot                               /usr/bin/fetchmail -d 1800
  0,10,20,30,40,50      *  *  *  *      /usr/bin/wget -O /dev/null http://timvw/cron/blogmarks.php &gt; /dev/null 2&gt;&amp;1
  30                   02  *  *  *      /usr/bin/wget -O /dev/null http://timvw/cron/smscity.php &gt; /dev/null 2&gt;&amp;1

# *                     *  *  *  *      *
# |                     |  |  |  |      |
# |                     |  |  |  |      +----- command to be executed
# |                     |  |  |  +------------ day of week (1 - 7) (monday = 1)
# |                     |  |  +--------------- month (1 - 12)
# |                     |  +------------------ day of month (1 - 31)
# |                     +--------------------- hour (0 - 23)
# +------------------------------------------- min (0 - 59)

I am the manual

I found this in my inbox on my favorite PHP forum, DevNetwork

I am the manual

Hi timvw,
you've won an award!

I Am The Manual Award


And the beauty is, you now have a badge to show for it! Smile

You can download it and use them as your avatar or in your signature:

Congratulations
patrikG

_________________
Forum Rules
How to get what you want!
PHP Manual!
This isn't the code you're looking for.

Exploring the FTP functions

I’m still drowning in the work, and exams are coming close too, but i decided to blog something about the FTP functions in PHP. The script will download all the files that are available on the remote server.

// make sure we have time enough to execute this script
set_time_limit(1200);

// connect to the ftp server
$ftp = ftp_connect('ftp.scarlet.be');
ftp_login($ftp, 'anonymous', 'password');

// get the files that are available here
$local = glob('*.*');

// get the files that are available there
$remote = ftp_nlist($ftp, '.');

// get the files there that are not availble here
foreach($remote as $file)
{
  if (!in_array($file, $local))
  {
    // we don't have the file, thus download it
    ftp_get($ftp, $file, $file, FTP_BINARY);
  }
}

// close the connection
ftp_close($ftp);