Applications often require a series of forms to be completed, with the final outcome being determined by the inputs from all the forms along the way. Furthermore, your web site may have several visitors filling in a series of forms at the same time. It's vital that you should be able to remember information from form to form, and also be able to tell your users apart from one another. This module covers the techniques used to "maintain state", and the PHP functions provided to help you do so.

Moving from mysql to mysqli - simple worked exampleWe have a big codebase of PHP code that uses MySQL databases, much of it written using mysql_ functions which were not object orineted in nature, and have been paralleled by the more recent mysqli_ functions and methods for quite some time now. The older mysql library is now deprecated, with warnings ...

Passing variable between PHP pages - hidden fields, cookies and sessionsVariables within a program are lost when the program exits ... unless the program takes some sort of action to save them. And if it does, you've then got to have another program pick them up to use them somehow.
Web applications in PHP are a series of short-running programs. A program is run each time ...

Hot answers in PHPWe've got a lot - a LOT of demonstrations and source code on our site. Here are a few of the most popular ones all brought together in a single listing. Enjoy ...
Searching - how similar are two words? - try about source
Does a particular URL exist? - try source
Uploading an image to a database ...

Multiple page web applications - maintaining state - PHPAs you want to do more in an online application, you need to link a series of forms together. There's a limit to how much you can expect your users to enter on one page, and almost inevitably they'll want feedback as they go through a series of steps. So multiple forms is natural, with each encouraging ...

Telling which ServerAlias your visitor used - useful during merging domainsSomeone tells you a website to visit - "go to wellhousemanor.co.uk" they say. And you'll naturally add "www." in front of it, calling up www.wellhousemanor.co.uk rather than wellhousemanor.co.uk ... or perhaps you won't.
Web servers are normally set up by default to respond to a single specific host ...

Easy session example in PHP - keeping each customers data apartWhen you go to a shop, you're not kept waiting outside until the previous customer leaves. The shop will have multiple shopping baskets / trollies, or muliple assistants. And that's the way you want it when you visit a web site too. This means that the web site programmer has to be very careful to ...

3 digit HTTP status codes - what are they, which are most common, which should be a concern?We often talk during web related course / course modules about the status codes that are returned by servers - but I don't think I've ever written up a (near?) complete list. They are all three digit codes, and they're grouped by hundreds - so 200 series code are success, 400 codes are requests that ...

Sending out an email containing HTML from within a PHP pageThe mail function in PHP allows you to send a plain text email in a single function call from within your script but please read the cautions at the end of this post before you do so!. How do you send out something in HTML format, though?
The mail function takes an additional (fourth) parameter of extra ...

Downloading a report from the web for further local analysisThere are many applications where users want to use a piece of web based software to explore data sets - to research information and come up with tables of results and lists of data which they then want to examine in more detail - explore further.
How do they do that further exploration - perhaps through ...

Uploading a document or image to its own URL via a browserOne of the major requirements of a committee / working group such as the TransWilts Community Rail Partnership is to be able to share information - and a forum (such as Simple Machines) does that admirably. So after yesterday's meeting, I put up a forum for our initial work.
But one of the side issues ...

How to build a test harness into your PHPI was writing a PHP demonstration yesterday, and I wanted to include a test harness with my class definitions that I could leave in place on the live code on my server, to be silently skipped over ... but which I could re-use at a later date for testing class changes, etc.
How did I do it? An environment ...

Shipping a test harness with your class in PHPIf you're writing a class (or group of classes) into a file you include into your main application(s) in PHP, you'll be recommended to keep the included files out of the document path so that they can't be run on their own (or, more dangerous, browsed as text if you have a different extension applied ...

Global, Superglobal, Session variables - scope and persistance in PHP"Global" is a poor choice of word! It really means "share this variable in this function with the variable of the same name at the top level" but there isn't a simple, easy better word for that than "Global". If you bring in code within an include / require file, then any top level code's variables ...

Sudoku helper or sudoku cheatHave you seen the sudoku puzzles that are being published in the Daily Telegraph and (I think) the Los Angeles times? There's a whole group of addicts out there and helper and forum web sites too. Lisa pointed the puzzle out to me recently (no, she's not an addict!) with the thought of providing a ...

Caching an XML feedIf you're using PHP to add a newsfeed to your site, please be kind to the news feed supplier and cache the feed if your page is a popular one. I posted a couple of weeks ago ... a link to a piece of code that grabs a news feed .... and I've enhanced it now to do the caching. Here's the modified code:
$feed ...

Reading a news or blog feed (RSS) in your PHP pageIf you want to quote the latest articles from an RSS (XML based) news feed on your site, is it difficult? No - it can easily be programmed in PHP. I was exploring this yesterday and have the source code showing just how easy it is in our training resources.
Want to try it out? This link runs the ...

Copyright and theft of images, bandwidth and members.Some poor home school kid in the USA just got the sharp end of my tongue .. I had just signed up to a political forum and so had he (he said) and he wrote:
Also I was wondering if you would join my political forum at http://www.xxx.org .(I know this is an advert but it's really hard to get a new board ...

Spotting a denial of service attackOur web site traffic rose from 37000 hits last Wednesday to 64000 hits on Thursday. Good sales and marketing activity on our part? No - it's a potential problem; all the extra traffic came from a single location and my immediate concerns included:
* Possible denial of service, where all the bandwidth ...

http, https and ajp - comparison and choiceIn a web scenario, client to server traffic is usually carried using an http (HyperText Transfer Protocol) transport. That's both from browser to public facing server, but also in ongoing transfers from the public facing server to other servers which provide content or run business logic in many applications.
But ...

Downloading data for use in Excel (from PHP / MySQL)Requirement: To allow a visitor to your web site to click on a link that downloads data from the server into a Microsoft Excel spreadhseet.
Solutions - I'm going to offer you two:
a) To send the output as a text file - delimited by tabs between columns, and with new lines for each new line. Add appropriate ...

Keeping staff up to date on hotel room statusCommunication - communication of the immediate status of hotel rooms, of events that have happened out of the norm, of the reasons behind each of our terms and conditions, and much more is all a vital part of running our hotel - and it's not made easier by the fact that we are (and must be) run be a ...

Script to present commonly used images - PHP"What are the most popular pages in your picture library, and where are they used from?" An interesting question and I could analyze raw web logs to find out the answers. However, most of our images are managed by a script that retains extra information such as an image title, and uses are logged ... ...

Single login and single threaded models - Java and PHPOn Apache Tomcat courses, I find myself explaining "single login" models and on some Java courses I explain single threaded models. In both cases, there are times that newcomers to the technologies ask "does it matter".
Answer - for a small and quiet application suite, probably not - but as the ...

PHP / Web 2 loggingIt' gone quiet here for 48 hours, hasn't it? The final run up to the first week of courses for the new year and we've been catching up with everything from cleaning blinds to updating web cam systems and ironing out final issues and catching up on other paperwork. Lots of good background and not ...

Simple page password protection - PHPSo many demonstrations are too clever - the wood gets lost for the trees and the newcomer is left not knowing what the important bits are. So a request overnight for a REAL simple way of password protecting some content based on a single password that can be changed [in the code] from time to time provided ...

.php or .html extension? Morally Static PagesYou'll find many pages on our web site with .php extensions, and many more with .html extensions. "So the .php files contain executable content and the .html files do not" you may think. No - not exactly; in practise almost every page actually contains some PHP / Script elements. So WHAT, WHY, HOW?
WHAT ...

PHP header() function - uses and new restrictionsPHP's header function allows you to change the headers on your returned content so that (examples)
• The browser receives not HTML but plain text, a .jpg image or a file to save locally
example: header ('Content-type: Application/Octet-stream');
• You can change a "404" missing ...

Updating a page strictly every minute (PHP, Perl) For the Clock exercise that I mentioned in my last entry, I need to update the page once a minute. Exactly once a minute. At the start of the minute.
I'm using client pull, where the browser asks for the next page - so my basic request is:
<meta http-equiv="refresh" content="60; url=/clock/now.php">
but ...

Improving searches - from OR to AND?There's a search box on the top of all our regular pages - put a word in there, submit the form, and you'll be offered a selection of appropriate pages ... and if there's nothing relevant, you'll be offered nearby spellings. Try a search for Ireland and a search for Irelond.
Search for multiple words, ...

PHP Image upload script"How do I upload an image to my server?" That's a common question and I have provided many answers before ... but today I wrote a very simple script that shows just the pertinent points of uploading an image.
<?php
if ($_REQUEST[completed] == 1) {
$newname = uniqid("whc").".jpg";
...

Passing GET parameters through Apache mod_rewriteIf you're using Apache mod_rewrite to redirect a series of URLs to a single script, did you know that you can pass values entered onto a form via the GET method through as well? Simply add on %{QUERY_STRING} onto the end of your new URL. You'll need to add a ? into the target URL to ensure that the ...

Of course I'll tell you by emailIt's about the time that I should be writing the next issue of "Of Course" - our twice yearly newsletter about Open Source training, and the news from Melksham and our training centre. But there's so much happening - with Well House Manor opening earlier this month, and with an initial 3 months 'running ...

Image maps for navigation - a straightforward exampleImage maps can be a really simple and effective way of selecting from a series of options - a little bit more flexible that straightforward HTML links. We use an image map on every one of of main "Well House Consultants" pages - click on any on the languages named in our banner, and you'll be taken ...

Finding the language preference of a web site visitorI can (and do) identify the country of origin of over 98% of the visitors to our web site using the maxmind database. But does that tell me what their native language is? No - it only gives me a clue.
A further clue is available from the accept-language header that's returned by browsers, and is available ...

Morning image, afternoon image"I want to show one picture on my website in the morning and a different one in the afternoon".
Doesn't that sound like it's a simple request ... yet - as ever with computing - it raises a whole lot more questions, with a raft of possible answers.
* Do you want the time from the server? Where's ...

Using PHP to output images, XML, Style sheets, etcPHP is usually used to generate a stream of HTML to be sent to the browser, but if you use the header function within your page you can use it to send any other type you wish. For example, if I include
header("Content-type: image/jpeg");
then I can script the production of images. Easy? Yes, but ...

Adding PHP tags to an old cgi programOur web site has grown over the years, and we've developed a number of very useful scripts such as our quotation system that's written in Perl and our live server test program in Tcl and Expect. Now we require to run those scripts within the current look and feel, with its printer friendly flexibility, ...

Maintaining state.Cookie examples.Headers, Heads and Bodies.Uses of Cookies.Accessibility - a practical example of cookies.Example using PHP built-in session functions.Why email from the server?.Informing the system owner.Emailing system users.Other users.Emailing from the server.Configuration.Sending a simple email.Adding to the headers.Sending attachments.A practical example of mailing from PHP.

Complete learning

If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.