ข้ามไปที่เนื้อหาหลัก

PHP password encrypt md5() and password_hash()

The MD5 algorithm not secure. Can be decoded with a big database.
So PHP has created a new function better than. But that's not the reason let us leave the MD5 function. It is also useful in encryption. Get the same only 32 digits code every time.


But for the security of the code. We need to merge data the data with secret key. When decrypt code, it can not understand. Encode md5() first and running password_hash() again.

This below is sample code



<?php
header('Content-Type: text/html; charset=utf-8');
function pass_encrypt($pass, $show=false){
//you secret word
$key1 = 'asdfasf';
$key2 = 'asdfasdf';
$loop = 1;
$reverse = strrev($pass);
if($show==true){echo '<br> กลับตัวอักษร &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ' , $reverse;}
for($i=0;$i<$loop;$i++){
$md5 = md5($reverse);
if($show==true){echo '<br> เข้ารหัสเป็น 32 หลัก  : ' , $md5;}
$reverse_md5 = strrev($md5);
if($show==true){echo '<br> กลับตัวอักษร &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ' , $reverse_md5;}
$salt = substr($reverse_md5, -13) . md5($key1) . substr($reverse_md5, 0, 19) . md5($key2);
if($show==true){echo '<br> สร้างข้อความใหม่  &nbsp;&nbsp;&nbsp; : ' , $salt;}
$new_md5 = md5($salt);
if($show==true){echo '<br> เข้ารหัสเป็น 32 หลัก    : ' , $new_md5;}
$reverse = strrev($new_md5);
if($show==true){echo '<br> กลับตัวอักษรอีกครั้ง &nbsp;: ' , $reverse;}
}
return md5($reverse);
}
$pass = "love999";
echo '<br> md5() ธรรมดา &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ' ,  md5($pass);
//เข้ารหัส md5 ก่อน
$encrypt = pass_encrypt($pass, true);
// และเข้ารหัส hash เพื่อนำไปบันทึกลงฐานข้อมูล
$hash = password_hash($encrypt, PASSWORD_DEFAULT);
echo '<br/><br/> ผลลัพธ์ :  <b>' . $hash .'</b>';
echo '<br/>ความยาวของตัวอักษร : <b>',strlen($hash),'</b>';
//ข้อมูลทดสอบ
$pass_in_db = '$2y$10$XO/2J2l2U70aZHRFixz32.1VU.GnfjM/Z/KifKalOdoZwctpZIYfC';
$post_data = "love999";
if (password_verify(pass_encrypt($post_data) , $pass_in_db)) {
echo '<br/><br/><span style="color:green">Password is valid!</span>';
} else {
echo '<br/><br/><span style="color:red">Invalid password.</span>';
}
?>


Function Reference


http://php.net/manual/en/function.strrev.php 
http://php.net/manual/en/function.md5.php 
http://php.net/manual/en/function.password-hash.php 
http://php.net/manual/en/function.password-verify.php






PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม

สนใจสั่งซื้อเพียง 4,500 บาท
http://fastcoding.phpcodemania.com/


ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย PHP สำหรับนำไปใช้ในงานต่างๆ

       สำหรับหลายท่านที่ขอโค้ดเข้ามาทาง Inbox ของเฟซบุ๊กแฟนเพจ หรือถามถึงระบบต่างๆหลังไมค์มานั้น ส่วนใหญ่ก็มีแจกอยู่แล้วในเว็บบอร์ด ThaiCreate.Com นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สร้างแบบฟอร์มล็อกอิน HTML + PHP

       ผมจะสร้างระบบตัวอย่างเพื่อแสดงให้เห็นการทำงานของสคริปต์ PHP ที่ใช้ในการล็อกอินอย่างง่ายๆ นะครับ โดยที่ผมจะสร้างโฟลเดอร์ใหม่ขึ้นในไดร์ฟที่เราได้กำหนดให้เป็น Directory Root สำหรับรันสคริปต์ PHP ซึ่งผมกำหนดเอาไว้ที่ D:\www ดังนั้นผมจะสร้างโฟลเดอร์ teacher_assistant ขึ้นมาใหม่จะได้พาธเป็น D:\www\teacher_assistant สำหรับการเข้าถึง จะใช้ URL ดังนี้ http://localhost/teacher_assistant หลังจากสร้างโฟลเดอร์เสร็จเรียบร้อยก็จะเริ่มกระบวนการเตรียมโครงสร้างโปรเจ็กต์ด้วยเครื่องมือช่วยเขียนโปรแกรมชื่อว่า eclipse ซึ่งดูวิธีการสร้างโปรเจ็กต์ได้จากบทความ เริ่มต้นสร้างโปรเจ็กต์ ด้วยโปรแกรม eclipse เครื่องมือเขียนโปรแกรม php ในบทความเป็นการสร้างโปรเจ็กต์ที่มีพาธเป็นโฟลเดอร์ login แต่ในที่นี้ต้องเป็นพาธให้ตรงกับโฟลเดอร์ใหม่ที่เราได้สร้างขึ้นด้วยนะครับ

PHP กับการคิดส่วนลดเป็นเปอร์เซ็น 5%, 10%, 20% ตามช่วงราคาที่กำหนด

<?php     $total_price = 1000;     $discount = 0;         if($total_price >= 500 && $total_price < 1001){         $discount = 5;     }elseif($total_price >= 1001 && $total_price <= 5000){         $discount = 10;     }elseif($total_price >= 5001){         $discount = 20;     }         $discount_bath = ($total_price*$discount)/100; ?> <pre> ซื้อสินค้าครบ 0 ถึง 499 บาท ไม่ได้ส่วนลด ซื้อสินค้าครบ 500 ถึง 1000 บาท ได้ส่วนลด 5% ซื้อสินค้าครบ 1001 ถึง 5000บาท ได้ส่วนลด 10% ซื้อสินค้าครบ 5001 บาทขึ้นไป ได้ส่วนลด 20% </pre> <h3>รวมราคาสินค้า = <?php echo number_format($total_price,2);?></h3> <h5>ส่วนลด = <?php echo $discount;?>%  (<?php echo $discount_bath;?> บาท)</h5> <h2>ราคาหลังส่วนลด = <?php echo number_format($total_price - $discount_bath,2);?>บาท</h2> "PHP ไม่ได้สร้างสุดยอดโปรแกรม แต่ PHP ช่วยให้งานคุณง่ายขึ้นต