หน้าเว็บ

May 11, 2018

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



May 6, 2018

PHP Codeigniter and Template Parser Class call Variable out of Pairs

This below code is part of  View from CodeIgniter Docuemnt
https://www.codeigniter.com/userguide3/libraries/parser.html




and Controller like this




When run on web browser all array data will display at braces same key name.
and associative array will display in  {blog_entries} .......... {/blog_entries} 





But, site_url cannot display in {blog_entries} .......... {/blog_entries} 

Create test page controllers/Welcome.php
<?phpdefined('BASEPATH') OR exit('No direct script access allowed');class Welcome extends CI_Controller { public function index() { $this->load->helper('url'); $this->load->library('parser'); $data = array( 'site_url' => site_url(), 'blog_title'   => 'My Blog Title', 'blog_heading' => 'My Blog Heading', 'blog_entries' => array( array('title' => 'Title 1', 'body' => 'Body 1'), array('title' => 'Title 2', 'body' => 'Body 2'), array('title' => 'Title 3', 'body' => 'Body 3'), array('title' => 'Title 4', 'body' => 'Body 4'), array('title' => 'Title 5', 'body' => 'Body 5') ) ); $this->parser->parse('welcome_message', $data); }}?>

Create view file views/welcome_message.php
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>{blog_title}</title> <style type="text/css"> body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } </style></head><body>        <h3>H3 : {blog_heading}</h3> <p>ลิงค์ : {site_url}</p>        {blog_entries}            <h5>H5 : {title}</h5>            <p>P : {body}</p> <p>Link : {site_url}</p>        {/blog_entries}</body></html>

When reload page again {site_url} is a normal text not display link url





For solve this problems

Override parser class with below code

Create file MY_Parser.php at this path  application/libraries/MY_Parser.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');/** * Overrides the CI Template Parser to allow for multiple occurrences of the * same variable pair * */class MY_Parser extends CI_Parser { // -------------------------------------------------------------------- /**  * Parse a template  *  * Parses pseudo-variables contained in the specified template view,  * replacing them with the data in the second param  *  * @param string  * @param array  * @param bool  * @return string  */ public function parse($template, $data, $return = FALSE) { $template = $this->CI->load->view($template, $data, TRUE); $results = $this->_parse_double($template, $data); $results = $this->_parse($results, $data, TRUE); if ($return === FALSE) { $this->CI->output->append_output($results); } return $results; } // -------------------------------------------------------------------- /**  * Parse a single key/value  *  * @param string  * @param string  * @param string  * @return string  */ protected function _parse_double($results, $data) { $replace = array(); preg_match_all("/\{\{(.*?)\}\}/si", $results, $matches); foreach ($matches[1] as $match) { $key = '{{'.$match.'}}'; $replace[$key] = isset($data[$match]) ? $data[$match] : $key; } $results = strtr($results, $replace); return $results; }}// END Parser Class/* End of file MY_Parser.php *//* Location: ./application/libraries/MY_Parser.php */


Then edit your view file views/welcome_message.php
in Variable Pairs if call out of pairs variable used double braces

{{site_url}}


<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>{blog_title}</title> <style type="text/css"> body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } </style></head><body>        <h3>H3 : {blog_heading}</h3> <p>ลิงค์ : {site_url}</p>        {blog_entries}            <h5>H5 : {title}</h5>            <p>P : {body}</p> <p>Link : {{site_url}}</p>        {/blog_entries}</body></html>

Now go to reload page you can see link URL at {{site_url}}






Function Reference

How to get the shortest rather than longest possible regex match with preg_match()
{{something1}} something2 {{something3}} something4https://stackoverflow.com/questions/5897478/how-to-get-the-shortest-rather-than-longest-possible-regex-match-with-preg-match

Translate characters or replace substrings
http://php.net/manual/en/function.strtr.php

Dec 14, 2017

Translate