Archive for September, 2007

User Accounts with PHP and MySQL

Hey there! Welcome to Making the Web - my personal blog about website development. Feel free to subscribe to my RSS feed to keep up with the latest. Alternatively, subscribe by email. Hope you enjoy this article!

Having a user accounts feature is something that I'd recommend to almost any website. It is one of the best ways to make visitors really feel part of your site. And, it's also gives you an idea of how many "valuable" users you have.

In this article, I'll be telling you how to create a user accounts feature, complete with a login form, registration form and "user area." To make the system, you'll need PHP and MySQL.

__('Read the rest of this entry »')

Comments (11)

Becoming PHP6 Compatible

You may also want to read the Unofficial PHP6 Changelog to find out what is changing in version 6.

PHP developers, like all developers, want their scripts to be as compatible as possible. Often, this involves looking into the past, to see if scripts are backwards-compatible. But, we sometimes have to look into the future - to see what is changing, and to understand what we need to do to become compatible.

PHP6 is the latest, yet unreleased version of PHP. It is still under development and won't be released for some time yet. Nonetheless, it is still important that we consider the changes we know about at the moment, and write scripts which are compatible.

If you want to make use of PHP6 when it comes, you're going to have to write your new scripts so they are compatible, and possibly change some of your existing scripts. To start making your scripts PHP6 compatible, I've compiled a list of tips to follow when scripting:

Don't use register_globals
In PHP6, support for register_globals will be no more. There will be no option to turn it on or off - it will not exist. This change should not affect you, as you shouldn't really use register_globals anyway. If you don't already know, register_globals puts $_REQUEST into the global scope, so you can access the variables just like any other variable. Instead, you should access inputted data like this:

$_GET['input'];
$_POST['input'];
$_REQUEST['input'];

__('Read the rest of this entry »')

Comments (39)

More Tips for faster PHP scripts

This post is part 2 of a 3 part series. For the other parts, visit these posts:

Tips for faster PHP scripts
Even More Tips for faster PHP scripts

Here's the list:

6. Pre-increment is faster than post-increment
Try to pre-increment, rather than post-increment, where possible. It is faster because post-increment creates a temporary variable while in the process of incrementing. So, this:

++$var;

…is faster than this:

$var++;

This rule also applies to decrementation as well. To test this assertion, I created two for loops. The first for loop used the post-increment option, while the second for loop used pre-increment instead. The total number of iterations was 10,000,000. Here are the results:

Post-increment: 2.148 seconds
Pre-increment: 1.692 seconds
Time saved: 0.456 seconds; 21.23%

7. Regular Expressions for input validation?
It is always a good idea to try and avoid regular expressions, where possible and practical. There are functions in PHP which will do exactly what some regular expressions do, but faster. Take this example:

if(ereg('[0123456789]', $number)) {
// Is integer
}else{
// Is not integer
}

It is much faster to do this instead:

if(ctype_digit($number)) {
// Is integer
}else{
// Is not integer
}

To test this, I used ereg('[0123456789]', $number) 1,000,000 times, followed by using ctype_digit($number) 1,000,000 times. Here are the results:

Regular Expressions: 2.401 seconds
ctype_digit: 0.985 seconds
Time saved: 1.416 seconds; 58.98%

8. split() or explode()
The split() function supports regular expressions, while explode() does not. It is often faster to use explode() when you do not need to use regular expressions.I done yet another test. I used split() to split a string without regular expression requirements, and then used explode() to split the same string. I repeated this 1,000,000 times. My results are:

split(): 5.453 seconds
explode(): 3.556 seconds
Time saved: 1.897 seconds; 34.79%

9. Use time() rather than date('U')
When you want to get the current Unix timestamp, it is faster to use time() rather than date('U'). To test this, I used the time() function 100,000 times, followed by date('U') 100,000 times. My results are as follows:

date('U'): 19.162 seconds
time(): 0.057 seconds
Time saved: 19.105 seconds; 99.7%

10. Fastest type of loop
In PHP, there are a number of loops available for you to use. There are while loops, do-while loops, and for loops. To see which one of these were fastest, I used each of them to perform 100,000,000 iterations. These are the loops which I used:

while(++$a<100000000){}

for(;++$a<100000000;){}

do{}while(++$a<100000000)

Here are my results:

while(++$a<100000000){}: 15.519 seconds
for(;++$a<100000000;){}: 17.577 seconds
do{}while(++$a<100000000): 13.744 seconds

As you can see, my results show that a do-while loop is 21.81% faster, compared to a for loop.

Comments (31)

Top 5 Search Engine Optimization Tips

You may have heard many times about the ways you can optimize your site for search engines. Optimizing your site can allow the search engines to extract the important information from your page, making your site appear for more relevant search queries. Search engines may also better rank your page, so it appears at higher positions within results pages.

I have compiled a list of the best 5 ways (in my opinion) to optimize your site for the search engines:

  1. Put keywords in your title. Try to place keywords relevant to your site in your page title. This will give the search engines a better idea of what your site is about, and also allows searchers to quickly determine whether your site is relevant to them.
  2. Use Headings. Using headings in your webpage gives some structure to your page, and makes searching the page for relevant information quick. Try to place some relevant keywords in the headings, which give a clue to what the paragraphs below it are about.
  3. Use alt="" in images. As images can't be understood by search engines, you should provide an alternative textual description of the image. You can do this using the alt attribute of the HTML image tag. Here's an example:

    <img src="dog.jpg" alt="Dog jumping into the air" />

  4. Use META tags. You should use META tags within your page, to describe what the document is about, as well as provide relevant keywords and define a language.
  5. Use a sitemap. Create a sitemap to show all the pages of your site. I recommend you add your sitemap to Google's Sitemap service, or Yahoo's service.

Search Optimization

Comments (1)

Making the Web | Chalvedon School and Sixth Form College | Messenger History | GCSE(WIKI): Simple bitesize revision for secondary school, KS4 students. | Encrypt files for free - high-grade encryption
Who's Populating The Web?