Showing posts with label form. Show all posts
Showing posts with label form. Show all posts
File Upload using PHP and MySQL

File Upload using PHP and MySQL

This system enable user to;
1. Log in to the system
2. Upload files
3. Delete files
4. Download files
If you are interested on this project please modify it by adding;
1. User manager module
2. User activities tracking module
3. More security features
NB: Files are saved in server and only location is saved to database.
USERNAME: admin
PASSWORD: 123456
BT - Developers
How to Create Secure Registration Page in PHP/MySQL Part III

How to Create Secure Registration Page in PHP/MySQL Part III

In our last two articles, we discuss on how to create a registration page using mysql andmysqli extension and how to secure it using mysql_real_escape_string or mysqli_real_escape_string.
This time we will modify our code to use PDO instead of mysql or mysqli extension.
Before we begin, let’s give some few advantages of using PDO in favor of mysqli.
  • Portability – supports 12 different drivers
  • Prepared statements – no need to use real_escape_string
  • Object Oriented
  • Named parameters
  • Support stored procedures
PDO and mysqli has little to no difference at all except that PDO is more portable. So, if you want to connect to multiple databases without using different drivers, it’s preferable to use PDO.
Now, here’s the code of using PDO with little changes from our previous tutorial.
registration3.html

How to Create Secure Login Page in PHP/MySQL Part II

How to Create Secure Login Page in PHP/MySQL Part II

This is a continuation of the topic that I have discuss yesterday on How to Create Secure Login Page in PHP/MySQL. Since PDO is too complicated compared with mysqli, I decided to separate this tutorial.
So, here we go.
login.html
Modify the code on our previous tutorial from:
to
login3.php
  1. <?php
  2. $username = $_POST['username'];
  3. $password = $_POST['password'];
  4.  
  5. $conn = new PDO('mysql:host=localhost;dbname=login', 'root', '');
  6.  
  7. $query = "SELECT password, salt
  8. FROM member
  9. WHERE username = :username";
  10.  
  11. $result = $conn->prepare($query);
  12. $result->bindParam(":username", $username);
  13. $result->execute();
  14.  
  15. $number_of_rows = $result->rowCount();
  16.  
  17. if($number_of_rows == 0) // User not found. So, redirect to login_form again.
  18. {
  19. header('Location: login.html');
  20. }
  21.  
  22. $userData = $result->fetch(PDO::FETCH_ASSOC);
  23.  
  24. $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
  25.  
  26. if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again.
  27. {
  28. header('Location: login.html');
  29. }else{ // Redirect to home page after successful login.
  30. header('Location: home.html');
  31. }
  32. ?>
As you can see above, there are some changes that are far different compared to mysqli.
Take this example:
In mysqli we use this code:
$result = $mysqli->query($query);
This is the equivalent in PDO:
$result = $conn->prepare($query);
Another is difference on how to fetch the record. In mysqli:
$userData = mysqli_fetch_array($result, MYSQL_ASSOC);
In PDO:
$userData  = $result->fetch(PDO::FETCH_ASSOC);
How to Create Secure Registration Page in PHP/MySQL Part III

How to Create Secure Registration Page in PHP/MySQL Part III

In our last two articles, we discuss on how to create a registration page using mysql andmysqli extension and how to secure it using mysql_real_escape_string or mysqli_real_escape_string.
This time we will modify our code to use PDO instead of mysql or mysqli extension.
Before we begin, let’s give some few advantages of using PDO in favor of mysqli.
  • Portability – supports 12 different drivers
  • Prepared statements – no need to use real_escape_string
  • Object Oriented
  • Named parameters
  • Support stored procedures
PDO and mysqli has little to no difference at all except that PDO is more portable. So, if you want to connect to multiple databases without using different drivers, it’s preferable to use PDO.
Now, here’s the code of using PDO with little changes from our previous tutorial.
registration3.html
Just change the line from previous tutorial:
to:
register3.php
  1. <?php
  2. //retrieve our DATA FROM POST
  3. $username = $_POST['username'];
  4. $password1 = $_POST['password1'];
  5. $password2 = $_POST['password2'];
  6. $email = $_POST['email'];
  7.  
  8. IF($password1 != $password2)
  9. header('Location: registration.html');
  10.  
  11. IF(strlen($username) > 30)
  12. header('Location: registration.html');
  13.  
  14. $hash = hash('sha256', $password1);
  15.  
  16. FUNCTION createSalt()
  17. {
  18. $text = md5(uniqid(rand(), TRUE));
  19. RETURN substr($text, 0, 3);
  20. }
  21.  
  22. $salt = createSalt();
  23. $password = hash('sha256', $salt . $hash);
  24.  
  25. $conn = NEW PDO('mysql:host=localhost;dbname=login', 'root', '');
  26.  
  27. $qry = $conn->PREPARE('INSERT INTO member (username, password, email, salt) VALUES (?, ?, ?, ?)');
  28. $qry->EXECUTE(array($username, $password, $email, $salt));
  29.  
  30. header('Location: login.php');
  31. ?>
In our next tutorial, we will discuss on how to create a secure login page based on the three tutorials about how to create a secure registration page.
Create Registration Page in PHP

Create Registration Page in PHP

This is a simple tutorial that will teach you on how to create a simple registration form using PHP/MySQL with JavaScript for input validation. This tutorial will not teach you on how to create a good design but rather to give you knowledge on how to create a fully functional registration form.

Creating our Table

First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Click create table and name it as simple_login.
3. Then name the database as "simple_login".
4. After creating a database name, click the SQL and paste the below code.
  1. CREATE TABLE IF NOT EXISTS `member` (
  2. `mem_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(30) NOT NULL,
  4. `password` varchar(30) NOT NULL,
  5. `fname` varchar(30) NOT NULL,
  6. `lname` varchar(30) NOT NULL,
  7. `address` varchar(100) NOT NULL,
  8. `contact` varchar(30) NOT NULL,
  9. `picture` varchar(100) NOT NULL,
  10. `gender` varchar(10) NOT NULL,
  11. PRIMARY KEY (`mem_id`)
  12. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Creating The Form

Next step is to create a form and save it as index.php. To create a form, open your HTML code editor and paste the code below after the tag.
  1. <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
  2. <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
  3. <tr>
  4. <td colspan="2">
  5. <div align="center">
  6. <?php
  7. $remarks=$_GET['remarks'];
  8. if ($remarks==null and $remarks=="")
  9. {
  10. echo 'Register Here';
  11. }
  12. if ($remarks=='success')
  13. {
  14. echo 'Registration Success';
  15. }
  16. ?>
  17. </div></td>
  18. </tr>
  19. <tr>
  20. <td width="95"><div align="right">First Name:</div></td>
  21. <td width="171"><input type="text" name="fname" /></td>
  22. </tr>
  23. <tr>
  24. <td><div align="right">Last Name:</div></td>
  25. <td><input type="text" name="lname" /></td>
  26. </tr>
  27. <tr>
  28. <td><div align="right">Gender:</div></td>
  29. <td><input type="text" name="mname" /></td>
  30. </tr>
  31. <tr>
  32. <td><div align="right">Address:</div></td>
  33. <td><input type="text" name="address" /></td>
  34. </tr>
  35. <tr>
  36. <td><div align="right">Contact No.:</div></td>
  37. <td><input type="text" name="contact" /></td>
  38. </tr>
  39. <tr>
  40. <td><div align="right">Picture:</div></td>
  41. <td><input type="text" name="pic" /></td>
  42. </tr>
  43. <tr>
  44. <td><div align="right">Username:</div></td>
  45. <td><input type="text" name="username" /></td>
  46. </tr>
  47. <tr>
  48. <td><div align="right">Password:</div></td>
  49. <td><input type="text" name="password" /></td>
  50. </tr>
  51. <tr>
  52. <td><div align="right"></div></td>
  53. <td><input name="submit" type="submit" value="Submit" /></td>
  54. </tr>
  55. </table>
  56. </form>

Creating our Connection

Next step is to create a database connection and save it as "connection.php". This file is used to connect our form to database. This file serves as a bridge between our form and our database.
  1. <?php
  2. $mysql_hostname = "localhost";
  3. $mysql_user = "root";
  4. $mysql_password = "";
  5. $mysql_database = "registration";
  6. $prefix = "";
  7. $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
  8. mysql_select_db($mysql_database, $bd) or die("Could not select database");
  9. ?>

Writing Our Save Script

Next step is to create our script that save our input data to database and save it as code_exec.php.
  1. <?php
  2. include('connection.php');
  3. $fname=$_POST['fname'];
  4. $lname=$_POST['lname'];
  5. $mname=$_POST['mname'];
  6. $address=$_POST['address'];
  7. $contact=$_POST['contact'];
  8. $pic=$_POST['pic'];
  9. $username=$_POST['username'];
  10. $password=$_POST['password'];
  11. mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password')");
  12. header("location: index.php?remarks=success");
  13. mysql_close($con);
  14. ?>

Validating The Input

To add some input validation using javascript, add the code below in the head tag of your index.php file. Input validation is used to make sure that all input field are filled out before saving the to database.
  1. <script type="text/javascript">
  2. function validateForm()
  3. {
  4. var a=document.forms["reg"]["fname"].value;
  5. var b=document.forms["reg"]["lname"].value;
  6. var c=document.forms["reg"]["mname"].value;
  7. var d=document.forms["reg"]["address"].value;
  8. var e=document.forms["reg"]["contact"].value;
  9. var f=document.forms["reg"]["pic"].value;
  10. var g=document.forms["reg"]["pic"].value;
  11. var h=document.forms["reg"]["pic"].value;
  12. if ((a==null || a=="") && (b==null || b=="") && (c==null || c=="") && (d==null || d=="") && (e==null || e=="") && (f==null || f==""))
  13. {
  14. alert("All Field must be filled out");
  15. return false;
  16. }
  17. if (a==null || a=="")
  18. {
  19. alert("First name must be filled out");
  20. return false;
  21. }
  22. if (b==null || b=="")
  23. {
  24. alert("Last name must be filled out");
  25. return false;
  26. }
  27. if (c==null || c=="")
  28. {
  29. alert("Gender name must be filled out");
  30. return false;
  31. }
  32. if (d==null || d=="")
  33. {
  34. alert("address must be filled out");
  35. return false;
  36. }
  37. if (e==null || e=="")
  38. {
  39. alert("contact must be filled out");
  40. return false;
  41. }
  42. if (f==null || f=="")
  43. {
  44. alert("picture must be filled out");
  45. return false;
  46. }
  47. if (g==null || g=="")
  48. {
  49. alert("username must be filled out");
  50. return false;
  51. }
  52. if (h==null || h=="")
  53. {
  54. alert("password must be filled out");
  55. return false;
  56. }
  57. }
  58. </script>
That's it! You've been successfully created your simple registration form with javascript for the input validation.

Featured Post

Download Music Sharing System PHP Source code

Hello welcome again to my blog, today i`m happy to share my first project that I develop by using codeigniter (MVC based php framework)...

Popular Posts