Dan is a web developer in London. He is interested in all things Internet, Linux and Mac.
RSS icon Email icon Home icon
  • Learning PHP – Part 6: Functions

    Posted on November 3rd, 2009 Dan No comments

    Functions are a very important PHP feature, and are the building blocks to some very powerful programming techniques. First of all, what is a function?

    In the tutorials so far, the PHP scripts have run from the top down. All instructions are performed in the order they appear in the file.

    A function can be thought of as a reusable ‘black box’. You (optionally) give it some data (called arguments or parameters), it performs an operation and (optionally) gives you a result.

    This is illustrated in the following diagram:
    function

    Read the rest of this entry »

  • Learning PHP – Part 5: Your first dynamic web page

    Posted on November 3rd, 2009 Dan No comments

    After the previous parts of this tutorial, you should be familiar with PHP variables and arrays.

    Now we will use these concepts to actually make a working web page. This page will contain a form that you can submit, and we will write PHP code to handle the form contents.

    Read the rest of this entry »

  • Learning PHP – Part 4: Controlling flow

    Posted on November 2nd, 2009 Dan No comments

    So now we’ve looked at variables and arrays, it’s time to make the code a little bit smarter.

    We’ve already seen how the foreach() can loop (iterate) over items in an array, but there are other ways to alter the flow of the code as well.

    Conditional

    One of the most common PHP statements is the “if”. In a nutshell, “if” does the following:

    if (some condition is true) { run this code }

    Read the rest of this entry »

  • Symptoms of a WordPress Hack

    Posted on November 2nd, 2009 Dan 2 comments

    I came to my site on Sunday morning ready to write the latest in my PHP tutorial, only to find a nasty looking 500 server error. I couldn’t even log into the admin panel.

    After a while tinkering with various configuration settings and trying to get something to come up, I started searching the web. Turns out, earlier versions of WordPress were vulnerable to specific hacks that could let attackers create their own admin users.

    Read the rest of this entry »

  • Learning PHP – Part 3: Array basics

    Posted on October 26th, 2009 Dan No comments

    In part 2, you were introduced to the concept of variables and the string, integer and floating point types. In part 3 we’ll go through a more advanced type; the array.

    Read the rest of this entry »

  • Learning PHP – Part 2: Variable Basics

    Posted on October 25th, 2009 Dan 1 comment

    After part 1, you should be familiar with the echo statement, which outputs a string to the browser:

    echo 'This is a string';

    (Note that from now on the opening (<?php) and closing (?>) PHP tags are omitted from examples, but should still be included in any source code you create)

    On its own, it does not appear to be useful; after all we could have just written ‘This is a string’ in HTML for the same effect. The real power comes from using PHP variables.
    Read the rest of this entry »

  • Learning PHP – Part 1: Introduction

    Posted on October 24th, 2009 Dan 2 comments

    So, you’ve got to grips with the basics of HTML and CSS, and now you want to actually make your web page do something? Well, PHP is a great way to do that. In this series of blog posts, I will introduce the PHP language and teach many useful techniques that are used in professional PHP development.

    Read the rest of this entry »

  • Unsetting http headers in PHP

    Posted on August 6th, 2008 Dan 3 comments

    I just came across a subtle issue affecting Internet Explorer users (well, fancy that!) and HTTPS connections.

    One of my clients has a site that downloads a series of results as a CSV file, which they open in Excel. Unfortunately, Internet Explorer was refusing to download the file, and was presenting an error message reading “Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.”

    To add to my confusion, this was happening on the live server (PHP4), but not on my dev server (PHP5) which both use the same code.

    In the end, I happened upon a Microsoft Knowledge Base article that explained the problem. Basically, IE obeys any “no-cache” headers you send to the browser. Without caching the file, Office applications cannot open the file when served over HTTPS.

    How to solve the issue? Remove the cache header(s) – but how?

    The PHP manual doesn’t make it entirely clear, and I found the solution by accident. To remove a header, use the same syntax as for setting a header, but only include a space after the colon.

    For example, for the “Pragma” header:

    header('pragma: ');

    Note: You must include the space after the “:” or the header will not be unset.

  • Step by step: Moving code between Subversion repositories

    Posted on July 23rd, 2008 Dan 4 comments

    As many coders will tell you, there reaches a point where you realise that you absolutely, positively must keep your code in a a revision control system. In my working life, I’ve used Microsoft’s ageing SourceSafe and more recently the vastly superior SubVersion (SVN).

    There’s many powerful GUIs out there which you can use to interact with SVN, and make the checking out and checking in very easy. If you primarily use a desktop GUI (like me), then chances are you use a SVN client GUI to interact with SVN on a day-to-day basis.

    But what happens when you need to move code between repositories?

    Read the rest of this entry »

  • HTTP Authentication in PHP

    Posted on June 12th, 2007 Dan No comments

    I’ve just discovered, totally by accident, how to get HTTP Authentication (when the browser pops up a dialog asking for the username and password – usually set with a .htaccess file) values within PHP. Previously, I’d just assumed that the authentication was a “black box” and I was unable to use it within my scripts. I had done some experimentation to see if any of the information was present in the _POST or _COOKIE arrays to no avail.

    Read the rest of this entry »