David Thomas Baker » PHP

Remember form data on error

Got an email with this question:

What I am seeking is an equally easy PHP inclusion that retains drop down and radio button entries when a form has an error on submission.

Continue Reading »
Posted by David Baker in: PHP | Tags:
Some ads...

Convert HTML hex colours to RGB

Simple function to convert HTML hex colors to RGB: Continue Reading »
Posted by David Baker in: PHP | Tags:

Random text background generator

Simple script to create randomised background text with customised colours.

view random text background generator hereĀ 

Posted by David Baker in: PHP | Tags:

How to cache images generated by PHP

When spitting out images with php the browser doesn't seem to cache the resulting image.

because the browsers were no caching the images.

Continue Reading »
Posted by David Baker in: PHP | Tags:

Cookies across multiple sub domains

Here's how to make the browser store cookies across multiple sub domains.

Continue Reading »
Posted by David Baker in: PHP | Tags: cookies |

Remember Form Data on Back Button

Sometimes it's handy to have the browser cache an entire page including anything that is entered into a form. What if a user has filled out an entire form but accidently clicks refresh or another link, and when they click back the form is empty. Anoying! This also works if you submit to an error checking page, and you want the browser to remember the form data instead of writing something in PHP. How to do this is below.

The easiest way to tell the browser that they can cache the page is by doing this:

header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime("+2 day")));

Only do this on pages that are not dynamic, as the browser will keep this page in cache for 2 days.

Put the above code AFTER any session_start(); if you are using sessions.

And that's how to control cache when back button clicked.

Posted by David Baker in: PHP | Tags:

suPHP + Plesk + apache vhosts

I installed suPHP on a plesk box, works great for all the client vhost websites, all their PHP scripts run with their respective user account privellages.

However, suPHP was enabled for every PHP script which ran under apache. This caused a few issues ...

Continue Reading »
Posted by David Baker in: PHP | Linux | Tags: plesk |

Geek Interview Questions

Will update this page each time I think of a cool/hard/fun interview question Continue Reading »
Posted by David Baker in: PHP | Linux | Tags: interview questions |

PHP - Simple Image Resize/Thumbnail Code Example

Here's a useful bit of code for resizing an uploaded image and also creating a thumbnail.

It currently supports png/jpg/gif. Could do with some improvements in the error handling department.

http://examples.dtbaker.com.au/view/simple_image_thumbnail_resize_example

Posted by David Baker in: PHP | Tags: thumbnails |

Simple forms in PHP - required field checking - PHP form generator

Here's a different way to generate forms using PHP, and to take care of all the required field validation too. It might help someone:

http://dtbaker.com.au/code/html_converter/example2.php

Posted by David Baker in: PHP | Tags: forms |

Uploading a file using Curl in PHP

Here's how to upload files using curl in php: (it's very easy)

Continue Reading »
Posted by David Baker in: PHP | Tags: curl |

Force File Download

Here's how to force the browser to download a file:

Continue Reading »
Posted by David Baker in: PHP | Tags:

Creating Oracle procedures with PHP/OCI

Spent ages trying to get this working.

The problem was the carriage return in the stored procedure sql code.

$sql = preg_replace('/\r/','',$sql);

fixed the problem.

 

Posted by David Baker in: PHP | Tags: oracle |

Gold Coast PHP Programming Position

Hi search engines!

Gold Coast PHP Programmer needed

I am looking for a computer nerd who knows his PHP inside out and is very familiar with Linux.

To immediately take on some exciting projects with the possibility of joining a well paid team of programmers here on the Gold Coast.

Experience with Oracle or any sort of multilingual programming would be nice. You must know what json is, used web services before and not be scared of getting dirty with some ajax. Must be able to debug with firebug and the web developer toolbar

Email me your details and we can have a chat.

Posted by David Baker in: PHP | Tags: jobs |

PHP - make test "FAIL oci_bind_array_by_name()"

All oci8 PHP test's fail when building php-5.2.5

# make test TESTS=ext/oci8

Running selected tests.
FAIL oci_bind_array_by_name() and invalid values 1 [ext/oci8/tests/array_bind_001.phpt]
FAIL oci_bind_array_by_name() and invalid values 2 [ext/oci8/tests/array_bind_002.phpt]
FAIL oci_bind_array_by_name() and invalid values 3 [ext/oci8/tests/array_bind_003.phpt]
FAIL oci_bind_array_by_name() and invalid values 4 [ext/oci8/tests/array_bind_004.phpt]
Continue Reading »
Posted by David Baker in: PHP | Tags:

Setting up an Oracle / PHP connection

So you want to connect to oracle through PHP. It's pretty easy... Continue Reading »
Posted by David Baker in: PHP | Tags:

Creating RPMs - Distributing PHP Code

Distributing PHP code with RPM's

Continue Reading »
Posted by David Baker in: PHP | Linux | Tags: rpm |

PHP fork - how to use pcntl_fork();

Here's a bit of PHP showing how to use pcntl_fork();

useful if you need your script to do more than one thing at the same time.

 

Continue Reading »
Posted by David Baker in: PHP | Tags:

PHP Stuff

When playing around and helping some guys at work learn more PHP I thought I would keep a record of some things we discussed.

Continue Reading »
Posted by David Baker in: PHP | Tags:

preg_match across multiple lines

Here's how to do php preg_match multi line

Say you have this html:

<select name="foo">
<option value="1">asdfa</option>
<option>asd</option>
</select>

and you want everything between the <select> box, use this php code:

preg_match('/<select name="foo">(.*)<\/select>/msU',$html,$matches);
print_r($matches);
Posted by David Baker in: PHP | Tags:

Debian - PHP - Connecting to MS SQL

How to connect PHP to a MS SQL server from a Linux server Continue Reading »
Posted by David Baker in: PHP | Tags: mssql |

Simple PHP MySQL Database Functions

A simple bit of code to make PHP database connections a little easier to use. Continue Reading »
Posted by David Baker in: PHP | Tags: mysql |

phpMailer - Language string failed to load

This usually means your phpMailer class cannot find the language file when it is trying to spit out a message.

eg: Language string failed to load: from_failed

easiest way to fix this is to set the language manually including the path to the language folder:

$mail = new PHPMailer();
$mail->SetLanguage("en", 'includes/phpMailer/language/');

Posted by David Baker in: PHP | Tags:

Update page while PHP script executes

If you have a script that runs for a long time and you would like to update the browser with some progress reports here is a very simple way of doing it:

script.php:

<html>
<body>
<div id="messages"></div>
</body>
<?php
function w($message){
?> <script language="javascript">
document.getElementById('messages').innerHTML =
document.getElementById('messages').innerHTML + "<?=$message;?>";
</script> <?php
@ob_flush(); @flush();
}
//example:
w("Here is a message");
sleep(5);
w("And another message");
sleep(5);
w("And yet another message");
?>
</html>

 

View an example here: click

Posted by David Baker in: PHP | Web | Tags:

Apache mod_rewrite - Melbourne IT - PHP-CGI

Ran into a problem trying to use mod_rewrite in .htaccess on a Melbourne IT hosting account.

They seem to run PHP as CGI instead of an Apache module. Read the differences here: http://blog.dreamhosters.com/kbase/index.cgi?area=2933

Here's the rewrite script that worked in the end:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,QSA]

The problem was a missing / before the index.php

Posted by David Baker in: PHP | Linux | Web | Tags: apache | mod_rewrite |

PHP5 and JSON

coming soon
Posted by David Baker in: PHP | JavaScript | Web | Tags:

Multilingual PHP5 with UTF8

coming soon
Posted by David Baker in: PHP | Web | Tags:

Search My Site

Featured Projects

Collector Comics is THE place to buy, sell, and research comics online. Launching soon.
Launch » Screenshot »
Webitor is a kick-ass easy to use, easy to extend, non-database driven Content Management System. Version 2 with reseller plan coming soon.
Launch » Version 1 »
GC Lounge is my own pet social network and test bed for social related code. Made by locals for locals.
Launch »
GG has indexed over half a million recent Trade Mark applications. This revolutionary tool is extremely valuable for industry experts.
Launch »
GCWiFi is the hub for techies interested in joining a ad-hoc wireless mesh spread across the Gold Coast
Launch »
Web based financial client management system
Launch »
OS Commerce modules and template modification
Launch »
View More Projects »

Daves Friends

BlueTeddy
WebSpark
GC Designer Forum
IPWealth Trademarks
Cheap Websites
Bonsai Shop
Webitor CMS Resellers
Hayley Crook
David Novakovic
Ben Novakovic
Richard Morwood
Gold Coast Linux
Aussie Click
CMS Website Design
Inspire Tuition
Gold Coast Music Network and Gold Coast Gig Guide

Chat with Dave

Powered by...

In the works

Webitor - Easy to use Content Management System with Shop, Newsletter, and Reseller plan