February 25, 2013
No Comments
As you might be aware, the UK PHP Conference was held in London last weekend. Having been the lucky winner of a ticket from PHP NW I went along.
The Venue
The conference was held in The Brewery in London city centre, a well suited conference centre with a variety of different conference rooms. An old Whitbread brewery, it was modern and practical while still maintaining enough of its history to keep its character. Overall the venue was great and it was good to have the variety of different rooms for all of the different events
The talks
The talks on offer were on a wide variety of subjects, varying from the very specific libraries to things of use to any PHP developer. Each of the talks was led by a volunteer giving up their time to speak about a subject for which they held a passion, it came across very well and as ever is good to hear from active members of the community.
Talks I found to be of particular interest were
- The Future of the PHP Development Environment A practical introduction to Vagrant, demonstrating its purpose in creating a reliable, predictable server environment. Covered was the installation, configuration and use of the software
- OpenStreetMap for the Web An enthusiastic demonstration of the power of having openly accessible data of the world’s maps and the APIs which enable a plethora of different applications
- A Hands-On Introduction to Writing Unit Tests Using PHPUnit A very practical (with live coding and demonstration of the Unit Tests, a risky move that paid off) intro to unit tests in PHP. Aimed at the very beginner, it started slowly with very simple tests but showed the power of them in much bigger projects
In summary
I thoroughly enjoyed the day, and would recommend anyone wanting to attend in future. The talks will be posted on the PHP UK website in the coming weeks so you should be able to get a sample of the events there
February 8, 2013
No Comments
JSON is a well structure interoperable way of passing data between systems. It’s a good way of passing large amount of data into a web page for JavaScript to process and between system on the internet regardless of there architecture or programming language.
JSON is one of the hardest data structure to read directly as a human in it’s RAW format compared to other formal grammars such as XML, YAML or ini.
Fortunately there is an easy trick that you can do with Chrome, Firefox with Firebug and even Opera and IE9!
Take the JSON you have, wrap it in “eval()” the same as you would if you where writing a web page (because it is just JavaScript after all) run it and every modern browser this was tested with should allow you to inspect the resulting data structure.
Example JSON:
{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
Firefox with Firebug:
|
Chrome:
|
Opera:
|
Internet Explorer 9:
Note: To see this structure you need to click the “Add watcher” link displayed upon running the code.
|
February 27, 2012
No Comments
If you have ever had to browse for a coding solution, you will have come across the problem of “smart” quotes replacing normal punctuation. Thankfully there is a simple fix to to this, a userscript called DumbQuotes which can be downloaded here.
This will convert all of the single & double quotes into something more code friendly.
If you are looking for a light weight, fast, PHP based yet fully featured issue tracking system, Elastik is a great choice. Here is a quick start guide for it’s usage.
The Problem
When working on any type of development project be it a design, programming even construction problems within the project will almost certainly arise. Keeping track of the problems that people have brought to attention can be hard to do effectively. Issues might be raised by email, IM or in person. Keeping track of the issues that have been raised can be a real problem if you don’t keep on top of it. Even on a single person project, it’s so easy to forget that some one pointed out a small problem. Using email and noting systems to track what issues have been raised can work for a small project but will quickly become difficult to work with one there is more than a single person on the project, such system of working will also fall apart if the number of current issues becomes large. Using a system designed to rectify the issue really is a boon. It not only allows each issue and it’s status to be tracked, but also standardises the method of communication so that every one knows where to look to see what is happening with an issue. Ultimately, the best benefit of using a tracking system is not losing/forgetting about any issues that have be raised.
Elastik’s intended use is for multi-client multi-project purposes. That is to say Elastik can have many clients using it, each client having multiple projects.
Creating Account and Account Permissons
Account creation of elastic is done via email on a invitation only basis. Only System, Project or Client Administrators can invite additional users. Elastik has quite granular user permissions. To start with there are three types of users Administrator, Client users/administrators and Project users/administrators. In terms of access levels Project users/administrator are the ‘lowest’ permission’ed user as they only have access to a single project. Client users/administrators have access to all the projects that a client has and system administrators have global access. On top of these levels of access, Client users can also be administrators of the client and the same applies to project users. Elastik also has granular per-project and per-client permissions settings available. In summary, if you want to tightly control access to the tracking system, you can do it in great detail.
Setting up a client project
Elastik uses the concept that each project belongs to a client. Even if you are making an application for your self, there is still a client involved (you!). So before you can create a project you need to create a client. This is a way of grouping projects together as well as defining a client owner. When creating a client you are required to supply an email address. The person who owns this address will be given client level access and will have client access permissions, and there for access to all project that belong to that client. Client administrators can change settings related to the client and all the projects that belong to that client. If you do not wish to setup any client level access you can enter a existing system administrator’s email address and then add a new user to the client/project later. All this also applies when creating a new project.
Managing Issues
Tickets is what issue tracking is all about. Ultimately working with tickets can be as straight forward as you like or as complex. Using the default ticket categories and schema will be enough for most situations and will even be workable even if they are not. However, Elastik allows you to create much more complex ticket structure which should make it flexible enough for use with any project.
Customising Tickets
The default ticket structure is that a ticket can either be a new feature request or a problem within already existing functionality. You can either expand or shrink this as necessary on a per client or project basis.
The default ticket schema is that a ticket can either be “Open” or “Closed”. You can customise this to have as many different ticket status as you like. Elastik also has the idea that it should be logically impossible to go one ticket status to a particular other one. For example, it would not logically make sense for a ticket to be able to transition from “Closed” to “New Ticket” as the ticket already exists so it is not a new ticket at all. In this example you would probably want to change to “Re-opened ticket”. Elastik allows you to specify that “New Ticket” is the default status of a new ticket and that it impossible for a ticket’s status to be “New Ticket” after is has already been changed, transitions can be defined to be quite complex.
Extensions
Elastik has a modular design and comes pre-packaged with a number of modules all of which are disabled by default, presumably because some people may simply not want them. Below are two that web developers who often spend allot of time communicating with clients may find particularly useful.
E-Mail Summaries
By default Elastik only uses email for setting up accounts and nothing else. The email summaries module will send emails to individuals periodically informing them of changes made on a project or client. This does require a cron/scheduled task be setup on a computer to function however. This is particularly useful to keep clients up to date with changes to issues and also keep you up to date on issues the client is raising.
One minor draw back is that by default it will only email changes to tickets to people who are watching the ticket. However, you can enable it to send every one on a project all updates whether they are watching it or not in the projects configuration.
File Attachments
The file attachments extensions allows files to be uploaded to the hosting server and attached (associated) with a ticket. Files are attached after ticket creation which does reduce the steamlined-iness of the process, but you can attach more than one file to a ticket.
The one thing to watch out for when using this extension is that it defaults to not allowing any uploads and you have to explicitly define what file extension types can be uploaded on a per-client or per-project basis. You will most likely want to enable all image formats so client can upload screen shots of the issue.
Summary
If you are having any amount of trouble communicating and keeping track of issues for any type of project, using Elastik is definitely a good way to solve this.
Elastik is fast, customisable, flexible, functionally complete despite been at version 0.4.1. No major draw backs despite been incomplete. Personally I would like to see it keep the light weight approach so that it doesn’t matter how old/rubbish a clients computer might be and they are using IE6 it will still be fast. It will also mean that if your server is over loaded, it will still run fast. Of course, if it doesn’t do what you want out of the box, it is PHP based and open source, you can either write a new module for it or fork it and start hacking at the code.
Bash provides many powerful functions but how can you use these easily?
If you spend any time using the command line, either to manage a server or on the desktop, you have to quickly become proficient with bash. You will also find the you carry out many of the same tasks time after time.
This can quickly become tiresome and error prone, particularly if you are piping several commands together. However, bash provides several ways to make your life easier.
Aliases
If you are always going to be using the same flags with a command, you can ensure that they are called by default by using an alias.
An alias is a shorthand that bash uses in order to fire commands. Several are included as standard with Ubuntu (and other distros) and are stored in your .bashrc file. You can edit these and add your own as the need occurs.
An example of this is that I prefer the human readable file sizes when using ls. You can turn these on using the -h flag, but you have to include this every time you run the command. To make things easier I changed the alias for ll (long list) to read as follows
alias ll="ls -ahlF"
This way I don’t need to remember to put the flag in.
As you build up more of these commands, it makes sense to store them in a separate file so you can easily find and edit them. You can place all of you aliases in a file called .bash_aliases and they will be available for use.
Scripts
Aliases are fine for quickly calling simple commands, but many times you will need to carry out more complex tasks. In order to do this, you can make a script, and then call that.
By placing everything into a script, you benefit from being able to carry out more complicated functions, and the easy of calling it from a single command.
An example of this would be if you wanted to compare two branches in git, and see all of the files that had been created or modified, but not the files that had been deleted. You create a file called git_get_changed_files and put the following in
#!/bin/bash
local ORIGINAL CHANGED FILES
ORIGINAL="$1"
CHANGED="$2"
FILES=$(git diff --name-status $ORIGINAL..$CHANGED public/ | awk '{print $1"@@@@@"$2}')
for FILE in $FILES
do
echo "$FILE" | awk -F "@@@@@" '{ i=""; if ($1 != "D") print $2}'
done
If you place this in your home/bin folder you will then be able to run git_get_changed_files like a normal command.
A Global Function Library
You can expand on this by creating a global library of functions. This works by putting all of the functions into a folder, where each file is a namespace for different functions.
This can be achieved by creating a script that will source of all of the files in a folder, like so:
#!/bin/bash
for f in $(ls /path/to/folder/);
do source /path/to/folder/$f;
done
Call this script func and place it in your home/bin folder. Then you put a script in the folder referenced in the file and use the following structure.
function parse.(){ # auto complete helper, second argument is a grep against the function list
if [[ '' == "$@" ]]
then
echo "Parse Namespaced Functions List"
cat $BASH_SOURCE | grep "^function[^(]" | awk '{j=" USAGE:"; for (i=5; i<=NF; i++) j=j" "$i; print $2" "j}'
else
echo "Parse Functions Matching: $@"
cat $BASH_SOURCE | grep "^function[^(]" | awk '{j=" USAGE:"; for (i=5; i<=NF; i++) j=j" "$i; print $2" "j}' | grep $@
fi
}
function parse.access_log_top_ten_code() { # Show the top ten code from access_log: useage ...code $FILE $CODE
FILE=$1
CODE=$2
echo "Count the top ten $CODE'd pages"
cat $FILE | awk '{ i=($9=="$CODE" ) ? $7 : ""; print i; }' | sort | uniq -c | sort -n | tail -n 11 | head -n 10
}
In this folder a namespace of parse is created. Running
func parse.
Will list all of the function in the file. Running func parse. log will list all of the function that contain log in the function name or description. When the files are listed, the function name will be displayed with the comment along side it.
As the func command sources all of the different files, you are able to makes use of the functions across different namespaces. This means that you can create a file that will format output and then use that in with your git functions.
I hope that you are able to make use of this idea, and build your own library of functions. Anything that you thing would be useful to add to the library, please mention below.
March 3, 2008
40 Comments
Sometime in MySQL you want to copy an entire table from one database to a separate database. One example of when you would want to do this is if you are making a backup of a table before you apply some kind of processing to that table. There is nothing worse than ruining a database table and losing valuable information. To make an instant backup is so easy that there really is no excuse not to make backups at important or risky stages.
Here is how to do it:
[mysql]
DROP TABLE IF EXISTS backup_db.backup_table;
CREATE TABLE backup_db.backup_table SELECT * FROM live_db.live_table;
[/mysql]
This will delete your existing backup table completely, then will remake it copying all structural information and content from the live_table in the live_db.
Related Blogs
http://www.phpclasses.org/browse/package/4017.html
http://www.sematopia.com/?p=61
http://www.yinfor.com/blog/archives/2008/02/mysql_backup_and_recovery_meth.html
February 26, 2008
No Comments
My favourite platform for web development has to be Ubuntu Linux. As a desktop system to work on (rather than play) I think it is unsurpassed. However there are some times that you really need to use windows. For example my laptop seems to refuse to install Ubuntu which means that I am forced to stick with windows XP for the time being. However windows can be a perfectly adequate web development environment.
One of the trickiest aspects of web development on windows used to be setting up the Apache, MySQL and PHP stack. However this is no longer the case thanks to the wonderful little package called XAMPP. This is short for XP, Apache, MySQL, PHP and Perl.
Xampp allows you to run a webserver on your local computer. This allows for superfast editing and checking of your web application. Anyone who has repeatededly edited and FTP’ed a file to a webserver will know that it can quickly become a pain and definitely slows down your performance as a programmer.
By editing the file locally and simply refreshing your web browser you can quickly see how your web design is looking or ensure that your PHP is not throwing any errors etc.
Furthermore, using XAMPP allows you to start and stop the entire web server stack easily from one simple control panel.
Download XAMPP from here
February 21, 2008
No Comments
osCommerce is an awesome ecommerce package. It has many critics and this is mainly due to the fact that unlike many modern open source packages it is not really ready to run “out of the box”. That is a drawback, however it is not really so much of a drawback when you take into account that anyone that would want to establish an ecommerce presence is going to be very keen to modify the package to make the site look and feel unique.
This is where osCommerce really shines as it is very easy to modify and there is a huge user base with literally thousands of plugin modifications or “contributions” in osCommerce speak which can help you to make your store work in exactly the way you want.
There are some things though that are highly recommened for every osCommerce store though:
1. Search Engine Optimisation
As standard, osCommerce really is not very search engine friendly. Search engine friendliness is the first step towards search engine optimisation. This of course means that there are a fair few things you need to do to your store to make it truly search engine optimised. However, they are worth the hassle (especially if you get us to do it for you) and once you have some SEO modifications implemented, you will be able to start to get indexed and gaining visitors from the search engines without having to pay per click.
2. Speed
There are a few key things that every store should really do to speed up osCommerce. If however you expect your store to have a lot of visitors and/or display a lot of products and categories then you will really benefit from giving the front end of the site a bit of an overhaul in certain areas. The speed improvements possible are really quite dramatic and can make the difference between your visitors sailing through the site straight to the page or product they want or them getting bogged down, bored and finally leaving before they even get where they want to be.
3. Security
osCommerce is pretty secure out of the box. The admin side obviously needs some password protection and can be made more secure if it is felt neccessary. An easy modification is to simply rename the admin folder into something that only you know.
4. User Friendly
The osCommerce ecommerce system is fairly user friendly from the outset. One area that many people prefer to modify though is the checkout procedure which can be a bit long winded for some.
5. Easy Administration
Admin side there is a whole host of things that you can do to make your and your employees’ lives easier. For example adding products one by one using the admin tool can be a bit tedious and slow. Easypopulate is an awesome addition to any osCommerce store allowing easy population and editing of the product catalogue using your favourite spreadsheet program. We like Open Office for this task.
Part of the joy of running your own web site is that you are free to edit, change, personalise and improve it to your hearts content. Some things you will add because they make a real difference to your bottom line and some things you will add just because you think its a good idea or will be cool. The great thing with osCommerce is that you can edit it and make it work the way you want.
If you are not confident with PHP or the change you want to make is a bit more complex than you can handle, then we are more than happy to get in there and give you a hand!
February 18, 2008
No Comments
Many web masters will be familiar with Photoshop by Adobe. This highly featured photo editing and graphics application is incredibly powerful, but also incredibly expensive. If you find that the main thing you do with photoshop is basic product image cropping, resizing, trimming, rotating and watermarking then there is a better way!
If your server uses PHP and has GD functionality enabled, you can use the power of the GD library to automatically do all of these things for you. Furthermore the GD library is faster than using photoshop and as the process actually runs on your server, you do not have to worry about uploading files. Your employees can directly manipulate product photos from your web store administration screens.
Chances are the main thing you will need to do is to take one hi res source image and create one or more resized versions of it to be displayed as thumbnails or product info page images. Your Hi Res images can then be displayed as a pop up or lightbox.
There are many PHP GD thumbnail classes etc out there and you will easily find one by searching the major search engines.
Here are the basic concepts though:
1. Grab an image into memory from an external jpeg file
Before we can do anything we have to get an image into the memory. This then gives us something to work with.
//grab image into memory, $img = full url to jpeg image
if(!$jpeg = imagecreatefromjpeg($img)){
echo '
<h2 style="color: red">Failed to Grab Image ' . $img . '</h2>
';
exit;
}
2. Manipulate Image however you want
There are all kinds of things you can do with the image you have loaded using the GD library.
3. Display the Image or Save to Disk
Once you have manipulated the image and have it looking the way you want, you can either serve it up or save it to the disk. Of course you can also do both if you prefer.
//save image
$save_path = 'Your Save Path';
imagejpeg($jpeg,$save_path);
//display image
header ("Content-Type: image/jpeg");
imagejpeg($jpeg);
//destroy image to free memory
imagedestroy($jpeg);
Related Resources
http://uk3.php.net/gd
http://www.phpclasses.org/browse/package/1365.html
http://www.phpit.net/article/image-manipulation-php-gd-part1/
February 14, 2008
3 Comments
One of the most useful and powerful things you can do with PHP is to create a programme which will simulate a web browser and can grab data, post data to forms and generally interact with other web sites – automatically.
For PHP to be able to work like this it must have the CURL library installed and active. It is the CURL library which actually handles all of the interaction and PHP is my scripting language of choice for interacting with CURL.
A simple CURL function is like this:
function curl($url){
$timeout = '300'; //how long before CURL gives up on this page
$go = curl_init();
curl_setopt ($go, CURLOPT_URL, $url);
curl_setopt ($go, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($go, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($go, CURLOPT_TIMEOUT, $timeout);
$page = curl_exec($go);
curl_close($go);
return $page;
}
This function when called and echoed will output the entire html of the $url specified.
For grabbing data from this page to be inserted into a database (for example when spidering a suppliers web site for product information to be inserted into your site) we then use regular expressions to find what we are looking for and then insert that into the database.
so for example if we wanted to grab the product title and we knew that this was wrapped in a h1 tag with the class “product title” we could use this regexp to grab this:
$page = curl($url);
$pattern = '%
<h1 class="product_title">(.+?)</h1>
%i';
preg_match($pattern,$page,$matches);
print_r($matches); //we can see the entire array of matches and choose which we want to insert into the database
We can also Post data to web sites using curl. This allows us to do all kinds of things including grabbing data that is displayed on the submission of post forms. Here is an example Curl Post Function:
function curl_post($url,$post_data){
$timeout = '300'; //how long before CURL gives up on this page
$go = curl_init();
curl_setopt ($go, CURLOPT_URL, $url);
curl_setopt ($go, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($go, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($go, CURLOPT_TIMEOUT, $timeout);
//now for the post section
curl_setopt($go, CURLOPT_POST, true);
curl_setopt($go, CURLOPT_POSTFIELDS, $post_data);
$page = curl_exec($go);
curl_close($go);
return $page;
}
It can be tricky to figure out exactly what data should be in the post string. To help you out though is this incredibly handy addon for firefox: Live Http Headers.
This addon lets you see exactly what is going on between your browser and the web site you are visiting. This can quickly and easily give you the information you need to replicate the same behaviour with your CURL script.
Edmonds Commerce specialise in working with PHP and CURL. If you have any spidering, screen scraping or other application that requires PHP to actively interact with other web sites – get in touch today to see how we can help you benefit from this incredibly powerful technique.
Related Resources
http://www.phpfour.com/blog/2008/01/20/php-http-class/
http://www.phpclasses.org/browse/package/1988.html
http://www.phpit.net/article/using-curl-php/
http://skeymedia.com/intro-to-curl-with-php/
curl,
firefox,
php,
programming,
spidering, By:
admin
3 Comments
Tags:
addon,
curl,
firefox,
get,
headers,
http,
mysql,
php,
post,
screen scraping,
spider,
spidering