Archive for the ‘Net: Techy: PHP’ category

PHP: Making faster PHP scripts

February 18th, 2008

Making the web has a couple of articles about how to make PHP scripts run faster and more efficiently. Checking the lists show I’m already doing quite a few of the items listed:

Page one

Multiple arguments with echo (i.e. using “,” instead of “.”)
I don’t currently do this, but it’s only 0.396% faster
Reduce function calls (i.e. find out the size of an array before a loop, not in the “for statement”)
I do do this, but not as much as I should – especially with a 54.095% difference!
Avoid using variables if they aren’t needed and unset them once you’ve finished with them
I try to do this, but there doesn’t appear (from the site) to be any speed penalties in not doing this.
Use single quotes (instead of double quotes – ” )
I’ve done this for ages as I knew PHP didn’t have to parse the string – but I didn’t know it was only a 0.696% speed difference.
str_replace() vs ereg_replace() and preg_replace()
I tend to use preg_replace instead of ereg_replace anyway, and I’m aware that using any sort of regular expressions is slow. Therefore I do try and use str_replace instead – after all, it’s a 59.224% saving in speed!

» Read more: PHP: Making faster PHP scripts

Coding: Password Security in Cookies

December 22nd, 2007

Via, I came across a nice detailed section on how to create a secure cookie and password system (which will be implemented in WordPress 2.4):

Cookies will be based on the secure cookie protocol described here. The cookie is structured like so:

user name|expiration time|HMAC( user name|expiration time, k)
where k = HMAC(user name|expiration time, sk)
and where sk is a secret key

The new cookie protocol will allow us to enforce expirations server-side, mass invalidate all cookies, and offer high-level confidentiality. Read the Liu paper for details on the protocol… …In conjunction with the new cookies, password hashing will be improved by moving to phpass. phpass provides password stretching and salting. These make brute-forcing your password hashes impractical should someone get access to your database.

Techy: 10 Absolute Nos! For Freelancers

December 20th, 2007

Wake Up Later has a list of 10 Absolute “Nos!” for Freelances which include (with my comments):

  1. Can you show me a mock-up to help us choose a designer/developer?
    When I worked for a web hosting company, we did waste a lot of time doing web site designs for people as “mock ups” and a few times we did see the ideas “recycled” in their finished design even though they hadn’t paid us. The percentage of “mock ups to completed projects” was quite low as well – meaning wasted time. Oh – and the number of people that said “I don’t like that mock up, can you do another”… Grrr… On the plus side, I’ve just realised that an insurance company that I wrote the billing system for nearly a year ago whilst at my previous employer is still in use, despite the pet insurance company having very little (if anything) to do with my previous employer now. Go me!
  2. Can you give us a discount rate?
    Am I the only person in the world that thinks “The price you see is the price you pay”? You don’t expect Tescos or Sainsburys to “haggle” the price of your food do you? Well, don’t expect web designers, programmers, hosting companies etc to do the same!
  3. Will you register and host my site?
    I slightly disagree with this one – the designer will be able to register the domain name and host it with a third party: but as long as they make it totally clear they are just performing the “payment side” of things and the client needs to contact the appropriate company if there is any “non-design/code” issues then it should be ok.
  4. Can you copy this site?
    Straight coping is a “no-no”, but trying to get a “similar look” to a site isn’t too bad. I.e. if you are doing a shopping cart: do you like the look of Amazon, Tesco, Play will help speed the design work along.
  5. Can I pay for my e-commerce site from my website sales?
    A big no from me here as well! If the designer/freelancer says to the customer “I’ll do it cheaper if you’ll split the sales” that means the designer/freelancer thinks the customer has a very good ideas. However, if the customers asks for it – then the customer must think it’s not such a good idea and hence doesn’t really want to risk their money.
  6. I have a great idea. Do you want to…?
    To me, this’ll depend on the circumstances. If they came to me (as a programmer) and said they’d like to partner for me and they can supply the design and backend content, and I’ve got to figure out how to get the content online and handle the promotion of it – then I may do. However, if it’s a case of “I want a shop to sell books via Amazon. I can design the site, can you do the back end…” then IMHO they won’t be contributing that much to the project. If they added their own book reviews then that’s another kettle of fish.
  7. Do you have an IM account?
    I’ve practically given up on IM clients (such as Yahoo, AOL, MSN Messenger and ICQ) mainly because of the Spam (Yahoo especially) and the fact that when I am logged in I’m not always available to chat (away from the machine) or I’m busy working on something. I tend to have my PC on a “always ready” state (i.e. text edit, browsers etc already loaded and positioned) so opening and closing Trillian (which I used to use to log me into all the different networks) will be an extra thing to remember…
  8. Can I just pay the whole amount when it’s done?
  9. Is there any way you could get this done tonight or this weekend?
  10. Can I be sure you won’t use this work in anything else?
    I tend to do the same as Samuel (the other of the original post) in that “(1) their code has utilized code from other projects which I haven’t charged them for, and (2) I will probably use code from their project on other projects, and (3) they own the code and implementation of the project (finished website), but not the actual code pieces (login system, image uploader, etc.). I pride myself in productivity and speed, and I need to use other code all the time to accomplish this.”. However, in most circumstances I do “copy my own code” but in a slightly different manner – so the “jist” of the code may be the same, it’ll be slightly different for each implmentation.

Techy: Discontinuation of PHP4

July 13th, 2007

I’ve already blogged about 13 facts about Friday the 13th which is paraskevidekatriaphobia or just triskaidekaphobia if you only fear the number 13. But Friday the 13th of July 2007 will go down as a “notable date” for some web developers – it’s the day that the End of Life of PHP4 was announced.

PHP4 as a programming language will become discontinued on the 31st of December this year – so if you’ve got an essential program which depends on PHP4 – contact the developers to make it PHP5 compatible now (after all, PHP5 has been out 3 years and they are now working on PHP6!). Ok, some people will be stuck (if I remember correctly, osCommerce doesn’t work brilliantly on PHP5 but I may be mistaken), but unfortunately that’s life…

Techy: Background Processes In PHP

July 10th, 2007

From What You’re Doing Is Rather Desperate is a very nice way of running a background task in PHP. I’ve implemented compression of a .tar file in the following method using this code:

$ps=runinbackground("gzip -v $tarfile -c 1> ".$tmp."download.tar.gz 2>".$tmp."download.tar.log");
while (isprocessrunning($ps)) {
if ($count>8) { $count=0; }
function runinbackground($command) {
$command="nohup $command & echo $!";
return ($PID);
function isprocessrunning($PID) {
exec("ps $PID",$processstate);

I can then monitor (within the loop) the download.tar.log file if necessary. Hope it helps someone else.

%d bloggers like this: