Articles Tagged with “Php”
29/01/2008: Looking Ahead to PHP 6
This is my look at what’s planned for the forthcoming revision to the PHP language.
Removal of Deprecated Features
PHP 6 includes a lot of tidying up, removing features of the language that have caused annoyance, confusion and security headaches. Although these changes are too numerous to list here, and the list will probably change before the official release, here are three of the major ones:
- PHP has for some time included two different regular expression libraries: POSIX Regex and PCRE. PCRE is both faster and more capable, so in PHP 6, the POSIX Regex library will be removed from the PHP core and exist only as an optional extension.
- PHP includes a feature called register_globals, which automatically creates global variables for any data provided to a script through an HTTP…
19/08/2007: TrivialEncoder/0.2
An update to my PHP encryption class. Despite the name, it’s becoming a pretty sophisticated encyption machine. New encryption algorithms added:
- Vigenerè cypher
- One-Time Pad
- Bruce Schneier’s Superencyption
- Various other methods using the MCrypt library
The TrivialEncoderManager class has been obsoleted by TE_Machine, an abstract class with several different child classes for encoding, decoding and analysis. TE_Machine has a greatly improved parser for methods, which has made it a lot easier for me to add additional functionality such as the ability to analyse an encryption technique to see how strong it would be, and to check whether the output would be ASCII-safe. TrivialEncoderManager is still present, but is deprecated and will be removed next release.
12/08/2007: PHP Debugging with Style -OR- How I Learned to Stop Worrying and Love the Bug
PHP lets you define your own error handler, so I decided to get a bit fancy. MegaErrorHandler (MEH) outputs its errors as specially-formatted HTML comments, with the details of the error encoded using JSON.
A small client-side script, with an associated stylesheet then pulls this data out of the comments and formats it as a nice little interactive bug-viewing console, allowing you to view a stack trace for each bug, inspect superglobals, view the syntax-highlighted source code for the file where the error occurred, check the list of defined constants and other useful things…
01/08/2007: PHP Encryption Class
Here’s a simple PHP library for encoding and decoding text.
Examples:
<?php
$trivialencoder_auto = FALSE;
include 'TrivialEncoder.class.php';
$manager = new TrivialEncoderManager;
$plaintext = 'Hello world';
// Chain together Triple DES, Memfrob and Base64 encoding.
// Note: 3DES encoding is passed an (optional) key "mysecretkey".
$encoding = '3des mysecretkey;memfrob;base64';
$cyphertext = $manager->encode($encoding, $plaintext);
echo $manager->decode($encoding, $cyphertext); // Hello world
?>
20/07/2007: Parsing an HTML Table with PEAR’s XML_HTMLSax3
Here’s an example of how to parse an HTML table into an array using the PEAR module XML_HTMLSax3. It supports the <tr>, <td> and <th> elements and the rowspan and colspan attributes…
19/07/2007: PHP Domain Class
On Usenet an often-asked question is how to programmatically determine the “domain” of a particular hostname. That is, excluding the components traditionally thought of as subdomains. As an example, groups.google.com and www.google.com both have a domain of google.com.
Invariably, one answer comes back stating that you just need to chop off everything from the front, leaving only the last two components. But then someone will chime in pointing out that groups.google.co.uk would be left as just co.uk that way, when what is really wanted is google.co.uk. And the eventual resolution of the argument will be “it just can’t be done”.
The problem is that there’s technically no difference between a domain and a subdomain: it’s simply a matter of convention. Fortunately, this issue is actually quite important to browser programmers, as it’s a key issue in cookie security: browsers must allow subdomains within a domain to share cookie data, but not allow cookies…
18/07/2007: PHP Linkifier
Here’s a nifty bit of code I wrote a while back (utilising a function from PHP SmartyPants) that takes a bunch of HTML, searches it for some word or phrase that you specify, and turns all occurances of it into a link, unless it’s already a link!…
17/07/2007: PHP vs Perl
Here’s a simple program which calculates, to eight decimal places, the value of the Golden Ratio φ implemented in both PHP and Perl, to demonstrate their similarities…
17/07/2007: Pretty Printing for PHP
Here’s a PHP function for reformatting the whitespace in PHP code…
10/05/2007: EXIF is Cool
If you’re not aware of EXIF, here’s what it is:
EXIF stands for Exchangeable Image File Format, and is a standard for storing interchange information in image files, especially those using JPEG compression. Most digital cameras now use the EXIF format.
That is, many images, including most of the JPEGs you get off modern digital cameras include a huge barrage of interesting information about the picture, such as:
- the date and time the photo was taken;
- the longitude and latitude where it was taken;
- whether or not the flash was fired; and
- what model of camera was used.
Pretty nifty, eh? But how to get all this information out? Well, PHP offers some fairly good EXIF functions. I rather like Masahide Kanzaki’s EXIF2RDF tool and am happy to announce that version 0.2.0 of demiblog will include a version of this for its photoblogging functionality. demiblog is very focused on providing good metadata, and EXIF2RDF will help the photoblogging parts to keep their end of the bargain!
08/04/2007: How PHP programmers get things wrong
Firstly, three disclaimers: PHP is a great programming language, one of my favourites — this website is written in PHP; there are many great PHP programmers out there, some of whom probably never get things wrong; I probably get things wrong a lot of the time.
The majority of the database-backed Open Source PHP projects that I’ve used/examined make the same flaw again and again…
08/04/2007: Hurrah! A Blog for Toby!
At last, my new CMS is at a stage when I’m able to actually start publishing with it. Now that I have an easy-ish tool to publish with, you can expect that this website will be updated more frequently and with more and better content.
Updating this website in the past has been a major pain. I took a look at installing an off-the-shelf blogging engine to help me run the site, and after much research decided that Wordpress was the best of breed. After two days playing with it, I abandoned it — it didn’t do everything I wanted out of a blogging engine, and the mess of PHP code would have made modifications to Wordpress painful.
And so in early 2006, I decided to embark upon my own blogging engine. I posted my initial ideas to Usenet in February 2006 and asked for feedback. I got a few useful suggestions and started development later that month, calling the project…
04/04/2007: Re: Building a “modular” PHP site
Tyno Gendo wrote:
I have been pondering over building a “modular” site which accepts add-ons built by other people. I was wondering if anyone has any links to any reading material on how you build this kind of facility into your site?
The basic technique is this:
Firstly, provide a plugin registration function, which we’ll call, say, “plugin_register”. When a plugin is loaded, it will call your plugin_register function and tell your site at least the following information:
- How to use the plugin — i.e. provide a function name or a class name that the site can use to access the functionality of the plugin;
- When to use the plugin — this is normally done via a named hook…
21/03/2007: PHP UTF-8 Validation Library
lawrence k wrote:
What PHP code would give me this kind of 100% certainty?
I was bored so wrote this. I’m quite proud of myself, as I wrote it and ran it and it worked first time! :-)
It not only checks that the UTF-8 is valid, it forces it to be valid…
27/02/2007: Re: is PHP less secure than Perl, Python, or Ruby?
PHP is not inherently insecure, but because it’s very easy to write PHP, it has become rather a popular language amongst people with little, if any, formal training on how to program. Because of this, there are an awful lot of badly written PHP scripts out there; installing them may well open up your server to abuse….
27/02/2007: PHP4 vs PHP5 [was Re: Extending the mysqli class]
Michael Fesser wrote:
My scripts make use of many of the new OOP features in PHP 5, so they won’t run at all on PHP 4.
Ditto.
My current big pet project is http://demiblog.org/. This is PHP 5+ only and supports MySQL 5+ and PostgreSQL 8+. Although I do realise that earlier versions of these packages are still quite widely used out there, this project probably won’t hit the big 1.0 for another year or two, by which time, the hosting world will have probably moved on a lot. Supporting older versions of PHP and the database engines will cost development time and push back the project release date even later, by which time PHP 4 support will be even less relevant.
So although PHP 4 may still be alive right now, it’s counting down the days to his retirement when it can hand over the family business to his son PHP 5. He’s heard the happy news that his dranddaughter PHP 6 is planning on following in their footsteps too as soon as she graduates. Because of PHP 4’s impending retirements, he’s not taking on…
15/02/2007: Re: Random Map Generation
skulkrinbait@googlemail.com wrote:
My first prototype for doing this doesn’t give good results, the map being far too random, can someone help me out or point me to a good resource please?
Real geography is not random…
10/02/2007: Re: Parsing Question…
cjl wrote:
Short of writing a parser, which is clearly beyond me, what are some reasonable approaches to handling user input that will be executed?
Writing a parser is the best option in the long-run. If you were to attempt to interpret the user input some other way, like pure regular expressions, then you would fall into a lot of traps, and your interpreter would behave oddly in many cases.
A full parser is a much better option: it will behave far more reliably and would be a lot easier to extend, should you feel the need to add extra features to the language at a later date.
Although it’s a lot of work, there are some fairly well established methods on writing them. What you basically need to write is three fairly independent components: a tokeniser, a parser and an interpreter. None of these share any code in common, except for the definitions of a few constants and classes.
Firstly, a tokeniser, which reads the user input and splits it into a long list of tokens. Each token should have the…
19/12/2006: On The Legibility of PHP
David Segall wrote:
I do not deny that the programmer is the most significant factor in the readability of a program but would you really promote PHP as a language because it is easy to read?
I certainly wouldn’t advise against it because of legibility concerns.
Any language that supports comments (not all do) and allows the programmer to choose variable and function identifiers using arbitrary alphanumeric names (again, not all do) can be made very readable simply by using these facilities. With most common programming languages, you can easily clarify your code by having a policy that includes choosing sensible, readable function and variable names, using indentation and plenty of white space, adding liberal comments, keeping line lengths down to a reasonable length, keeping function sizes down to 20 or so lines (longer functions can often be split into two or more functions). Yes, even Perl!
For example, consider the following two (untested) PHP objects which are exactly…
25/03/2006: Sample PHP Source for Non-Interrupting Logins
Noozer wrote:
User opens a page. A session starts and they are asked to log in. They spend 30 minutes reading the page, then clicks a link. Since they were on the page for 20+ minutes their session ended. At that point they are asked to log in, and then taken to their chosen page as if never interrupted.
Reasonably easy, yes. This example is in PHP, but the same idea should work for other languages…
12/11/2005: Reusable Form Functions
Like me, you are probably confronted with many requests for surveys, questionnaires, feedback forms, registration forms and so forth: forms where the processing requirements are very simple (store in a database or e-mail to a particular address).
Despite the simple requirements, there is often quite a lot of work involved: crafting a database to store results, writing a function to store the data to the database, one to verify submitted data (e.g. make sure that if you’ve asked for an e-mail address, it contains an @-sign), and marking up the form itself (which can be quite a task if you want to make correct use of <label>, <th scope="blah">, etc.
So I introduce my reusable form functions, with the rather unglamorous name of inc_surveytool.php v1.0.
They are far from finished (expect a 1.1 and a 2.0 version…
22/08/2005: Reusable Table Functions
Ross wrote:
I am seeking your advice on the programming language for such an action. i prefer a web table capable of sorting by size, length and so on, therefore built-in function for sorting is appreciated.
Down below are a couple of nice PHP functions that may be of use. I’ve taken them from my CMS at work and munged them a little to make them independent of it. I’ve not tested these munged ones, so they may need a little adjusting.