Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP md5 hash a confirmation URL

Posted on 2010-08-20
4
Medium Priority
?
412 Views
Last Modified: 2013-12-13
Hi,

(using PHP and MySQL)

When a unknown user creates an order on my website I want to send an email confirmation to them that will change the order state for that row to 'Active'. I am doing this to make sure that the user entered in their email address correctly(for billing later).

I am unsure of a secure way to do this as I only want the state to be able to change 1 time from "inactive' to 'Active'.

The part that I cannot figure out is how to securely send the url to change the state of the order to the customer.

How would I md5 hash the order id and maybe the page name? instead of looking like this:

http://www.website.com/activate.php?id=3&state=Active

Any suggestions on doing it a better way or help in figuring it out would be gratefully appreciated.

thanks.
0
Comment
Question by:Solutionabc
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:JtR
ID: 33489366
if i understand your problem than you just want to make an email confirmation (opt in) for a specific action, right?

you need to save your parameters in the database together with a "randomly" generated hash.
then you send a link on a script with the hash as id to the users email.

when the users runs the script than the script has to read out the action parameters from the database and start the action. and then remove the handle from the database when the action is finished.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 33489400
Make a hash of the current time() and the email address.  Store the current time(), the email address and the md5() hash in your data base, along with a column that is named "verified" and says FALSE.  Send an email to the client with a link to the verification page that looks something like this (note the md5() code of the hash.

Click here to verify your order:
http://example.com/verify.php?q=6eea9b7ef19179a06954edd0f6c05ceb

In the "verify.php" script, find the value in $_GET["q"] and use it to query the data base for a SELECT match on the md5() hash.  If it is not found, issue an error message.  If it is found, change the column named "verified" to say TRUE.  And send another email to the client saying "Thank You!"

As you code this, remember that email addresses are not case-sensitive, but md5() hashes treat the input as case-sensitive.  So you might want to use strtoupper() on the inputs to the md5() function calls.
0
 
LVL 3

Expert Comment

by:JtR
ID: 33489492
If you do it like Ray Paseur suggest then don't forget to clean/validate the $_SET['q'] before writing it to the MySQL query! (risk of SQL INJECTION).

i would not save the email, i would only save the Hash and the identifiers for the action that you want to do when the address is verified. you also should add a colum for a timeout so that the login handle "expires" after a while.

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33491266
If you do not save the email, you won't be able to send a "Thank You" message.  I cannot think of any application design that would deliberately lose a piece of data that is this important.  The idea about having a date for expiring the offer is a good one.  You could even have a cron job that runs once a day and looks for orders that are nearing expiration.  You could send a reminder message ("Only three more days...") to those clients.

I suppose it should go without saying that any script that takes data from the URL ($_GET["q"] is the correct variable in the example, not $_SET) needs to filter that data.  In the case of an expected md5() string, you can see that it consists of letters and numbers only, is 32 bytes long.  These are easy things to test for, and a string that fails these tests can simply be discarded.  For general advice and assistance in cleaning up external input, the PHP filter functions are most valuable.
http://us2.php.net/manual/en/filter.filters.php

Best regards, ~Ray
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses four methods for overlaying images in a container on a web page
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question