Google Maps - dragging a marker to new position

Something that took me a while to figure out was how to drag a marker on a map to a new position and then retrieve it's lat/lng (version 3).

This assumes you have the map variable set as an instance of google.maps.Map and latlng which is the current position to show on the map as an instance of google.maps.LatLng.

var marker = new google.maps.Marker({
	map: map,
	draggable: true,
	position: latlng
});

google.maps.event.addListener(marker, 'dragend', function(){
	var position = marker.getPosition();
	map.setCenter(position); // set the map center to it's new position
	alert(position.lat() + ',' + position.lng()); // here are the new lat/lng strings
});

Simple PHP caching improved

There are many tools around that will help cache your web pages to increase speed and performance (most notably Memcached) however these usually require installation on your web server, thus rendering them useless for people who use shared hosting and don't have access to the server.

Using some simple PHP code we can quite effectively cache a webpage, useful for sites that are database driven and have a large amount of queries or high volumes of traffic.

First we need to setup the folder where the cached files are stored, making sure it has read/write permissions (CHMOD to 0777 - see your FTP client documentation on how to do this).

Next create a file called cache.php which contains the following code:

Read more »

Flintstone - A key/value database store using flat files for PHP

XEWeb are proud to announce the release of Flintstone, a key/value database store using flat files for PHP.

Some of the features include:

  • Memory efficient
  • File locking
  • Caching
  • Gzip compression
  • Easy to use

All it requires is PHP 5 and read/write file permissions.

Take a look at example usage below:

<?php

try {

    // Load flintstone
    $db = new Flintstone(array('dir' => '/path/to/database/dir/'));

    // Set keys
    $db->load('users')->set('bob', array('email' => 'bob@site.com', 'password' => '123456'));
    $db->load('users')->set('joe', array('email' => 'joe@site.com', 'password' => 'test'));
    $db->load('settings')->set('site_offline', 1);
    $db->load('settings')->set('site_back', '3 days');

    // Retrieve keys
    $user = $db->load('users')->get('bob');
    echo 'Bob, your email is ' . $user['email'];

    $offline = $db->load('settings')->get('site_offline');
    if ($offline == 1) {
        echo 'Sorry, the website is offline<br>';
        echo 'We will be back in ' . $db->load('settings')->get('site_back');
    }

    // Delete a key
    $db->load('users')->delete('joe');

    // Flush database
    $db->load('users')->flush();
}
catch (Exception $e) {
    echo 'Exception: ' . $e->getMessage();
}

For more information and to download visit this page.

Generate a random string A-Z, 0-9 in PHP

Often I find that generating a random string can be tedious, there seem to be so many ways to do this.

I wrote this simple function to do the work, and it can be easily modified.

<?php
/*
 * Create a random string
 * @author	XEWeb <>
 * @param $length the length of the string to create
 * @return $str the string
 */
function randomString($length = 6) {
	$str = "";
	$characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
	$max = count($characters) - 1;
	for ($i = 0; $i < $length; $i++) {
		$rand = mt_rand(0, $max);
		$str .= $characters[$rand];
	}
	return $str;
}

You can remove any of the ranges in $characters (for example if you didn't want uppercase letters delete the range('A','Z')) or put in your own array of characters.

Bash commands to detect script injections and malware

Not so long ago this site and other domains hosted on my server were injected with malware PHP scripts that caused all sorts of damage, including amending javascript files to display ads to people who visited my sites.

These 2 bash commands saved my life and I would like to share them with the world.

The first one will find any javascript file that contains the string "eval(unescape" which is the most common way of injecting malicious code. The second is a similar method for PHP files.

find . -name "*.js" | xargs grep -l "eval(unescape"
find . -name "*.php" | xargs grep -l "eval(base64_decode"

Seek and destroy!