Archive for the ‘PHP Tutorials’ Category

How to Create a Random Date in PHP

Thursday, February 7th, 2008

While browsing some forums the other day, I came across this question - “How do I find a random date in PHP?”

This is a pretty simple operation, if we make use of a few built-in PHP functions - time(), strtotime(), and date(). In this article, we’ll see how these three can work together to find a random date within a given range.
(more…)

How to Send E-Mail in PHP: A “Share This” Form

Wednesday, February 6th, 2008

This question comes up a lot in forums - how do you send e-mail in PHP?

There are plenty of reasons you’d want to do this. Perhaps you want to send a newsletter to your users. Or you want people to be able to leave feedback through e-mail. Or you just want your users to be able to e-mail the page (or a link to it) to a friend.

How do we do this? The mail() function.
(more…)

Understanding the Difference Between MD5 and Crypt Functions

Saturday, February 2nd, 2008

If you’re working with a user-management system in PHP (or any scripting language), than you’re likely using either the md5() or crypt() function to encode and store the user’s password.

An oft-asked question - and oft-misunderstood - is what is the difference? Is one better than the other?
(more…)

How to Create a Random Password for Users

Friday, February 1st, 2008

If you’re working on a user-management system, you may find it useful to be able to create a random password for users.

Some systems generate these initially and have the user log in to set a permanent password. You might also have a “Reset” button, where the script generates a random password and e-mails it to the user.

This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers. Or, if you’re impatient, jump straight to the function’s source code
(more…)

How to Use PHP To Dynamically Resize an Image

Wednesday, January 30th, 2008

Screenshot of a Dzone PageOne of the most annoying things about working with images is getting them into the right size. Screenshots start out huge - and you need to resize them to an appropriate size for your website.

Some blogs automatically resize the pictures for you into a thumbnail - like the image to the right. What if you want the image a different size, though? You could allow the browser to resize it for you, or - better yet - write your own php script to resize the image dynamically.
(more…)

How to Pass All Elements of a Form to the End in a Multi-Page Form

Sunday, January 27th, 2008

Sometimes forms get long, unsightly, and intimidating. It’d be great if you could cut them up into three or four parts - with a few fields on each page.

In order to do that, you need to figure out some way of storing or passing along the information from the first pages. I’ve heard a bunch of crazy ideas - from storing the information in session variables to writing each part to the database.

The easiest thing to do is attach a very brief php function to the bottom of each page. It’ll do all the work for you and continue to post every item the user has entered. (more…)

Why Is My htaccess File Causing an Internal Server Error 500?

Friday, January 25th, 2008

This was the odd problem that I faced when I switched hosts last month. My other website (Babel) was working fine. I uploaded everything to the new host, and every page came up with an “Internal Server Error 500.”

I eventually narrowed the problem down to the htaccess file. I wasn’t quite sure why it was breaking everything… but if I renamed it, the files worked fine.
(more…)

How to Use the Crypt() Function to Encrypt and Check Passwords

Tuesday, January 22nd, 2008

Every php-script that involves some kind of user login and database interaction has one very important feature - password checking and encryption.

There are a bunch of ways you could create and check passwords - from an insecure string in a database to an encrypted “hash” that you check against user input. This tutorial will show you how to use the crypt() function to store and check passwords in a php script.
(more…)

How to Write a PHP Script to Send a Trackback

Tuesday, January 8th, 2008

Don’t know what a trackback is? Check out this set of articles for a bit more information.

Assuming you do know what a trackback is, how do you send one - technically speaking? And how can we write a php script to send one?

A trackback is a simple ping - an HTTP Request. It is specially formatted with a limited amount of information. The receiving server is set up to handle that information and use it to create the comment based on your trackback ping.
(more…)