{"id":52678,"date":"2021-07-11T03:01:56","date_gmt":"2021-07-10T17:01:56","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=52678"},"modified":"2021-07-11T14:01:20","modified_gmt":"2021-07-11T04:01:20","slug":"javascript-and-php-encoding-and-decoding-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-and-php-encoding-and-decoding-primer-tutorial\/","title":{"rendered":"Javascript and PHP Encoding and Decoding Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/encoding_decoding.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript and PHP Encoding and Decoding Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/encoding_decoding.jpg\" title=\"Javascript and PHP Encoding and Decoding Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript and PHP Encoding and Decoding Primer Tutorial<\/p><\/div>\n<p>The recent HTML form navigation work gobsmacked us mildly as we reconnected with the fact that a character like &#8220;~&#8221; is not changed (ie. encoded) by Javascript&#8217;s &#8220;encodeURIComponent&#8221; encoding function.  And then we figured that &#8220;~&#8221; can get you back to home directories on Linux and Unix, so presumably, could appear in a URL non-argument part (ie. the address of your web script).  There are others too, and so we decided to write a practical way for you to experiment with.<\/p>\n<p>Let&#8217;s show you the PHP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/encoding_decoding.php_GETME\" title=\"encoding_decoding.php\">&#8220;proof of concept&#8221; <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/encoding_decoding.php\" title=\"Click picture\">web application<\/a> code, it being pretty short, below &#8230;<\/p>\n<p><code><br \/>\n&lt;?php<br \/>\n\/\/ encoding_decoding.php<br \/>\n\/\/ User experimentation with encoding and decoding systems<br \/>\n\/\/ RJM Programming<br \/>\n\/\/ July, 2021<br \/>\n$pfrom=\"The quick brown fox jumps over the lazy dog and tilde (~) followed closely behind, dragging along 0 Mostel!?\";<br \/>\n$pto=\"\";<br \/>\n$pvia=\"\";<br \/>\n$blurb=\"Please choose an encode or decode option below ...\";<br \/>\nif (isset($_POST['from']) && isset($_POST['via'])) {<br \/>\n  $pfrom=str_replace('+',' ',urldecode($_POST['from']));<br \/>\n  $pvia=str_replace('+',' ',urldecode($_POST['via']));<br \/>\n  if (strpos(urldecode($_POST['via']), \"base64_encode\") !== false) {<br \/>\n    $pto=base64_encode($pfrom);<br \/>\n    $blurb=$pvia;<br \/>\n  } else if (strpos(urldecode($_POST['via']), \"base64_decode\") !== false) {<br \/>\n    $pto=base64_decode($pfrom);<br \/>\n    $blurb=$pvia;<br \/>\n  } else if (strpos(urldecode($_POST['via']), \"urlencode\") !== false) {<br \/>\n    $pto=urlencode($pfrom);<br \/>\n    $blurb=$pvia;<br \/>\n  } else if (strpos(urldecode($_POST['via']), \"urldecode\") !== false) {<br \/>\n    $pto=urldecode($pfrom);<br \/>\n    $blurb=$pvia;<br \/>\n  }<br \/>\n}<br \/>\n<br \/>\necho \"<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script type='text\/javascript'&gt;<br \/>\n  function consider(sio) {<br \/>\n  }<br \/>\n<br \/>     \u200b<br \/>\n  function maybeclient() {<br \/>\nocument.getElementById('tvia').value=document.getElementById('tvia').value.toLowerCase().replace('uri','URI').replace('component','Component');<br \/>\n    if (document.getElementById('tvia').value == '') {<br \/>\n      return false;<br \/>\n    } else if (document.getElementById('tvia').value.indexOf('base64') == 0) {<br \/>\n      return true;<br \/>\n    } else if (document.getElementById('tvia').value.indexOf('url') == 0) {<br \/>\n      return true;<br \/>\n    } else if (document.getElementById('tvia').value.indexOf('atob') == 0) {<br \/>\n      try {<br \/>\n      document.getElementById('tto').value=atob(document.getElementById('tfrom').value);<br \/>\n      } catch(ecvd) { alert('You have some invalid characters for atob to handle.'); }<br \/>\n    } else if (document.getElementById('tvia').value.indexOf('btoa') == 0) {<br \/>\n      document.getElementById('tto').value=btoa(document.getElementById('tfrom').value);<br \/>\n    } else if (document.getElementById('tvia').value.toUpperCase().indexOf('encodeURIComponent'.toUpperCase()) == 0) {<br \/>\n      document.getElementById('tto').value=encodeURIComponent(document.getElementById('tfrom').value);<br \/>\n    } else if (document.getElementById('tvia').value.toUpperCase().indexOf('decodeURIComponent'.toUpperCase()) == 0) {<br \/>\n      document.getElementById('tto').value=decodeURIComponent(document.getElementById('tfrom').value);<br \/>\n    } else if (document.getElementById('tvia').value.toUpperCase().indexOf('encodeURI'.toUpperCase()) == 0) {<br \/>\n      document.getElementById('tto').value=encodeURI(document.getElementById('tfrom').value);<br \/>\n    } else if (document.getElementById('tvia').value.toUpperCase().indexOf('decodeURI'.toUpperCase()) == 0) {<br \/>\n      document.getElementById('tto').value=decodeURI(document.getElementById('tfrom').value);<br \/>\n    }<br \/>\n    return false;<br \/>\n  }<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h1&gt;User experimentation with encoding and decoding systems&lt;\/h1&gt;<br \/>\n&lt;h3&gt;RJM Programming - July, 2021&lt;\/h3&gt;<br \/>\n&lt;form onsubmit='return maybeclient();' method=POST action=.\/encoding_decoding.php&gt;<br \/>\n&lt;table style=width:90%; border=60&gt;<br \/>\n&lt;tr&gt;&lt;th&gt;From ... &lt;\/th&gt;&lt;th&gt; ... &lt;input type=submit value=via&gt;&lt;\/input&gt; ... &lt;\/th&gt;&lt;th&gt; ... To&lt;\/th&gt;&lt;\/tr&gt;<br \/>\n&lt;tr&gt;&lt;td&gt;&lt;textarea rows=30 style=width:100%; name=from id=tfrom&gt;\" . $pfrom . \"&lt;\/textarea&gt;&lt;\/td&gt;&lt;td style=vertical-align:top;text-align:center;&gt;&lt;select size=11 name=via id=tvia onchange=consider(this);&gt;<br \/>\n&lt;option value='\" . strtoupper($pvia) . \"'&gt;\" . $blurb . \"&lt;\/option&gt;<br \/>\n&lt;option value='base64_encode'&gt;<a target=_blank title='Information from home of PHP' href='https:\/\/www.php.net\/manual\/en\/function.base64-encode.php'>base64_encode<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='base64_decode'&gt;<a target=_blank title='Information from home of PHP' href='https:\/\/www.php.net\/manual\/en\/function.base64-decode.php'>base64_decode<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='btoa'&gt;<a target=_blank title='Information from w3schools for Javascript function' href='https:\/\/www.w3schools.com\/jsref\/met_win_btoa.asp'>btoa<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='atob'&gt;<a target=_blank title='Information from w3schools for Javascript function' href='https:\/\/www.w3schools.com\/jsref\/met_win_atob.asp'>atob<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='urlencode'&gt;<a target=_blank title='Information from home of PHP' href='https:\/\/www.php.net\/manual\/en\/function.urlencode.php'>urlencode<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='urldecode'&gt;<a target=_blank title='Information from home of PHP' href='https:\/\/www.php.net\/manual\/en\/function.urldecode.php'>urldecode<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='encodeURIComponent'&gt;<a target=_blank title='Information from w3schools for Javascript function' href='https:\/\/www.w3schools.com\/jsref\/jsref_encodeuricomponent.asp'>encodeURIComponent<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='decodeURIComponent'&gt;<a target=_blank title='Information from w3schools for Javascript function' href='https:\/\/www.w3schools.com\/jsref\/jsref_decodeuricomponent.asp'>decodeURIComponent<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='encodeURI'&gt;<a target=_blank title='Information from w3schools for Javascript function' href='https:\/\/www.w3schools.com\/jsref\/jsref_encodeuri.asp'>encodeURI<\/a>&lt;\/option&gt;<br \/>\n&lt;option value='decodeURI'&gt;<a target=_blank title='Information from w3schools for Javascript function' href='https:\/\/www.w3schools.com\/jsref\/jsref_decodeuri.asp'>decodeURI<\/a>&lt;\/option&gt;<br \/>\n&lt;\/select&gt;&lt;\/td&gt;&lt;td&gt;&lt;textarea readonly rows=30 style=width:100%; name=to id=tto&gt;\" . $pto . \"&lt;\/textarea&gt;&lt;\/td&gt;&lt;\/tr&gt;<br \/>\n&lt;tr&gt;&lt;td colspan=3&gt;&lt;input id=ende value='Encode or Decode' type=submit&gt;&lt;\/input&gt;&lt;\/td&gt;&lt;\/tr&gt;<br \/>\n&lt;\/table&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;\";<br \/>\n<br \/>\n?><br \/>\n<\/code><\/p>\n<p>The space character is one to watch.  A client side HTML and Javascript form &#8220;encodeURIComponent&#8221;s any space to %20 but serverside PHP &#8220;urlencode&#8221;s a space to the &#8220;+&#8221; character.  This &#8220;+&#8221; is a legitimate character in data-URIs and we find we need to assume a &#8220;urldecode&#8221; of passed in characters, if they do not contain &#8230;<\/p>\n<ul>\n<li>data URIs &#8230; nor &#8230;<\/li>\n<li>Javascript code in the &#8220;head&#8221; element of a webpage<\/li>\n<li>some other real use of the &#8220;+&#8221; (eg. mathematical formula, north latitude sometimes, west longitude sometimes)<\/li>\n<\/ul>\n<p> &#8230; then we often, coming back from a form to serverside PHP, use &#8230;<\/p>\n<p><code><br \/>\n$varis = str_replace('+', ' ', urldecode($_POST['postedfieldname']));<br \/>\n<\/code><\/p>\n<p>Try it below if you like &#8230;<\/p>\n<p><iframe style=\"width:100%;height:800px;\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/encoding_decoding.php\"><\/iframe><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d52678' onclick='var dv=document.getElementById(\"d52678\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/encodeURIComponent\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d52678' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The recent HTML form navigation work gobsmacked us mildly as we reconnected with the fact that a character like &#8220;~&#8221; is not changed (ie. encoded) by Javascript&#8217;s &#8220;encodeURIComponent&#8221; encoding function. And then we figured that &#8220;~&#8221; can get you back &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-and-php-encoding-and-decoding-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[2830,1653,3697,2661,2829,212,1607,3699,1606,2749,3700,1605,652,932,997,1122,1319,1611,2831],"class_list":["post-52678","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-atob","tag-base64","tag-base64_decode","tag-base64_encode","tag-btoa","tag-client","tag-decode","tag-decodeuri","tag-decodeuricomponent","tag-encode","tag-encodeuri","tag-encodeuricomponent","tag-javascript","tag-php","tag-programming","tag-server","tag-tutorial","tag-urldecode","tag-urlencode"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/52678"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=52678"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/52678\/revisions"}],"predecessor-version":[{"id":52701,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/52678\/revisions\/52701"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=52678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=52678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=52678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}