├── example.png
├── README.md
├── ajaxCrypto.php
└── ajaxCrypto.js
/example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nowanon/ajaxCrypto/HEAD/example.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ajaxCrypto
2 | ## Encode Ajax Request Data & Decode PHP post-get
3 |
4 | Example video: https://www.youtube.com/watch?v=v0PQbwXCvLs
5 |
6 | JavaScript Request Encode Data
7 | ```html
8 |
9 |
12 | ```
13 |
14 | Php Decode Data
15 | ```php
16 | require_once("ajaxCrypto.php");
17 | if($ajaxCrypto->post){
18 | echo var_dump($ajaxCrypto->post);
19 | }
20 | if($ajaxCrypto->get){
21 | echo var_dump($ajaxCrypto->get);
22 | }
23 | ```
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ajaxCrypto.php:
--------------------------------------------------------------------------------
1 | uniord(mb_substr($string,0,1, "utf-8"));
34 | $string = mb_substr($string,1,$strlen, "utf-8");
35 | $strlen = mb_strlen($string);
36 | }
37 | return $array;
38 | }
39 |
40 | private function decode($str, $sifre){
41 | $str = $this->mbStringToArray($str);
42 | $r = "";
43 | $b = 0;
44 | for($x = 0; $x < count($str); $x++){
45 | $r .= $this->unichr($str[$x] - $sifre[$b]);
46 | $b++;
47 | if(!isset($sifre[$b])){
48 | $b = 0;
49 | }
50 | }
51 | return $r;
52 | }
53 |
54 | function __construct(){
55 | if(count($_POST) >= 2 && array_key_exists("ajaxCrypto",$_POST)){
56 | $sifre = $this->mbStringToArray($_POST["ajaxCrypto"]);
57 | unset($_POST["ajaxCrypto"]);
58 | foreach(array_keys($_POST) as $i){
59 | $this->post[$this->decode($i, $sifre)] = $this->decode($_POST[$i], $sifre);
60 | }
61 | }
62 | if(count($_GET) >= 2 && array_key_exists("ajaxCrypto",$_GET)){
63 | $sifre = $this->mbStringToArray($_GET["ajaxCrypto"]);
64 | unset($_GET["ajaxCrypto"]);
65 | foreach(array_keys($_GET) as $i){
66 | $this->get[$this->decode($i, $sifre)] = $this->decode($_GET[$i], $sifre);
67 | }
68 | }
69 | }
70 |
71 | }
72 |
73 | $ajaxCrypto = new ajaxCrypto();
74 |
75 |
76 | ?>
--------------------------------------------------------------------------------
/ajaxCrypto.js:
--------------------------------------------------------------------------------
1 | /*
2 | @author: ihaci
3 | ajaxCrypto 1.0
4 | */
5 |
6 | (function () {
7 | "use strict";
8 | if (typeof window.ajaxCrypto === "undefined") {
9 | if (typeof window.encodeURIComponent === "undefined" || typeof window.XMLHttpRequest === "undefined" || typeof Object.keys === "undefined") {
10 | throw "Old browser detected!";
11 | }
12 |
13 | function setPW() {
14 | var r = {
15 | data: [],
16 | out: ""
17 | };
18 | for (var i = 0; i < 20; i++) {
19 | r.data[i] = Math.floor(Math.random() * (9999 + 1 - 1) + 1);
20 | r.out += String.fromCharCode(r.data[i]);
21 | }
22 | return r;
23 | }
24 |
25 | function e(data, pw) {
26 | var r = "",
27 | bas = 0;
28 | for (var i = 0; i < data.length; i++) {
29 | r += String.fromCharCode(data[i].charCodeAt(0) + pw[bas]);
30 | bas = bas + 1;
31 | if (!pw[bas]) {
32 | bas = 0;
33 | }
34 | }
35 | return r;
36 | }
37 | window.ajaxCrypto = function (method, url, data, header, success, error) {
38 | if (method && url) {
39 | if (method.toLowerCase() === "get" || method.toLowerCase() === "post") {
40 | var pw = setPW();
41 | var realData = "";
42 | var xhr = new XMLHttpRequest();
43 | if (typeof data === "object" && data) {
44 | Object.keys(data).forEach(function (key) {
45 | realData += encodeURIComponent(e(key + "", pw.data)) + "=" + encodeURIComponent(e(data[key + ""] + "", pw.data)) + "&"
46 | });
47 | realData += "ajaxCrypto=" + encodeURIComponent(pw.out);
48 | }
49 | if (method.toLowerCase() === "get" && data) {
50 | url += "?" + realData;
51 | }
52 | xhr.open(method, url, true);
53 | if (typeof header === "object" && header) {
54 | Object.keys(header).forEach(function (key) {
55 | xhr.setRequestHeader(key, header[key]);
56 | });
57 | }
58 | if (method.toLowerCase() === "post") {
59 | xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
60 | }
61 | xhr.onreadystatechange = function () {
62 | if (xhr.readyState === 4) {
63 | if (xhr.status === 200) {
64 | if (typeof success === "function") {
65 | success(xhr);
66 | }
67 | } else {
68 | if (typeof error === "function") {
69 | error(xhr);
70 | }
71 | }
72 | }
73 | }
74 | xhr.send(realData);
75 | } else {
76 | throw "-> Unkown method!";
77 | }
78 | } else {
79 | if (!method) {
80 | throw "-> Method not set!";
81 | }
82 | if (!url) {
83 | throw "-> Url not set!";
84 | }
85 | }
86 | }
87 | }
88 | })();
--------------------------------------------------------------------------------