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.

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