I'm creating a form that deals with account creation in SQL and I can't seem to figure out how to encrypt the password once the form is completed and submitted.
<?php
//Connects To MySQL
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Selects The Specified Database
mysql_select_db("my_db", $con);
//Inserts Account Creation Info Into The Accounts Table
$sql="INSERT INTO accounts (Login, Password, Email)
VALUES
('$_POST[login]','$_POST[password]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Your Account Has Been Created!";
mysql_close($con)
?>
Can anyone help me out? I just want the password to be encrypted when it's sent to the DB
Form Data Encryption Question
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Form Data Encryption Question
You mean like:
Code: Select all
$sql="INSERT INTO accounts (Login, Password, Email)
VALUES
('$_POST[login]',MD5($_POST[password]),'$_POST[email]')";
Re: Form Data Encryption Question
That was my first guess, but when I changed it to that I began receiving this error upon submitting the form.
"tylerrr" being what I put into the password field.
Code: Select all
Error: Unknown column 'tylerrr' in 'field list'-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Form Data Encryption Question
Code: Select all
$sql="INSERT INTO accounts (Login, Password, Email)
VALUES
('$_POST[login]',MD5('$_POST[password]'),'$_POST[email]')";
Re: Form Data Encryption Question
Ah thank you!
I tried this before but was getting a column data error, and just realized it was a problem in my navicat parameters and not with the code ^_^
I tried this before but was getting a column data error, and just realized it was a problem in my navicat parameters and not with the code ^_^
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: Form Data Encryption Question
I'm going to pick your code apart in the interest of education. It would appear as though you are just getting started, so its better to learn this type of thing now. Some of my comments are personal preference rather than a right or wrong way, so keep that in mind.
For a production environment website, I hesitate to static code variables that might change. Some of which, are the username, password, host, and database. I prefer to put this type of data into a seperate includes file. That way, if I need to change my login credentials, I can change it in 1 place and the changes are made globally. If you take this approach, be sure you understand file security. I like to place this type of file just outside of my web root.
The next step is to understand the difference between a hash an encryption. A hash is basically 1 way. You use an algorithm to scramble a bit of text and you cannot ever unscramble it. This is good for storing passwords! You dont want anyone who has access to the database to see plain text passwords. As the user registers, hash their password and store it in the database. On every subsequent login, hash the users input and then compare it to the hash in the database. If they match, the passwords are the same and only the end user knows what the unscrambled password is.
Encryption is 2 way. You encrypt information with the intent of decrypting it later. This typically requires a public and private key and if used in a heavily accessed environment, can add more load to the web server. Use it sparingly and only for private information. There are books written dedicated to this subject and I cannot begin to explain it.
Lastly, you are using the mysql extension. Though there is nothing wrong with using it, it has been superceded by the mysqli extension. The both do the same thing but the mysqli should yield better performance. I wouldnt be suprised to see the older mysql extension go away in the future. you can check if mysqli is installed by using phpinfo(), but it is so common place now, that I'd be surprised if it wasnt available.
Good luck!
For a production environment website, I hesitate to static code variables that might change. Some of which, are the username, password, host, and database. I prefer to put this type of data into a seperate includes file. That way, if I need to change my login credentials, I can change it in 1 place and the changes are made globally. If you take this approach, be sure you understand file security. I like to place this type of file just outside of my web root.
Code: Select all
<?php
// File Name: config.php
# Define Login Credentials
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");
define("DB_HOST", "localhost");
define("DB_DATABASE", "my_db");
?>Code: Select all
<?php
# Include config.php file
include_once("config.php");
# Connect To MySQL Server
// Notice the use of the defined constants from the included config.php file.
$con = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
if (!$con) {
/* This is fine for a development environment, but when you upload this to the public,
* be sure to remove this. Outputting any php error strings to the user
* can expose information about your site, which could help an attacker.
*
* exit(); will stop the script quietly and not output any error message.
* That is a better solution for production environments, coupled with an error logger.
*/
die('Could not connect: ' . mysql_error());
}
# Selects The Specified Database
mysql_select_db(DB_DATABASE, $con);
# Inserts Account Creation Info Into The Accounts Table
/* This next line is bad practice!
* You are wide open to SQL Injection!
*/
$sql="INSERT INTO accounts (Login, Password, Email) VALUES ('$_POST[login]','$_POST[password]','$_POST[email]')";
/* At the VERY least, do something like this:
* Notice the use of backticks, single quotes, terminating semi-colon, and escaping of values.
* This should be considered standard practice. Typically you would do more data validation,
* as you dont want someone to enter erronious or potentially harmful data into your form.
*
* NEVER trust user inputted data!
*/
$sql = sprintf("INSERT INTO `accounts` (`Login`,`Password`,`Email`) VALUES ('%s','%s','%s');",
mysql_real_escape_String($_POST['login'], $con),
mysql_real_escape_String(sha1($_POST['password']), $con),
mysql_real_escape_String($_POST['email'], $con));
# Execute Query
/* I prefer this method. It really only matters in a few instances, but for insert statements
* it allows you to retrieve the insert id, from your database.
*/
$result = mysql_query($sql,$con);
if (!$result) {
// See comments above outputting php errors to the user in a production environment.
die('Error: ' . mysql_error());
}
# Close the Database Connection
mysql_close($con);
# Account has been created.
echo "Your Account Has Been Created!";
?>Encryption is 2 way. You encrypt information with the intent of decrypting it later. This typically requires a public and private key and if used in a heavily accessed environment, can add more load to the web server. Use it sparingly and only for private information. There are books written dedicated to this subject and I cannot begin to explain it.
Lastly, you are using the mysql extension. Though there is nothing wrong with using it, it has been superceded by the mysqli extension. The both do the same thing but the mysqli should yield better performance. I wouldnt be suprised to see the older mysql extension go away in the future. you can check if mysqli is installed by using phpinfo(), but it is so common place now, that I'd be surprised if it wasnt available.
Good luck!