问题描述:

so I already managed how to decode and encode, this is done via 2 functions.

So here is the example

http://goldextra.com/b/id_color.php

If you enter a word, it will get encoded, as shown in the result page.

so if one enters = testword

the url will be http://goldextra.com/b/balloony.php?name=testword

The thing is, instead I want the url to look like this

http://goldextra.com/b/balloony.php?name=k484z52646w5i474

(of course the site itself should not show the encryption, but the word)

How do I have to change the code I have now, and do I have to change code in the Form site or the result site?

FORM CODE

 <form action="balloony.php" class="sign-up" method="get" class="sign-up">

<h1 class="sign-up-title">Whats the word?</h1>

<input type="text" class="sign-up-input" placeholder="Enter your baloony" name="name" autofocus>

<input type="submit" value="baloony it!" class="sign-up-button">

RESULT SITE

<?php

function encode($string,$key) {

$key = sha1($key);

$strLen = strlen($string);

$keyLen = strlen($key);

for ($i = 0; $i < $strLen; $i++) {

$ordStr = ord(substr($string,$i,1));

if ($j == $keyLen) { $j = 0; }

$ordKey = ord(substr($key,$j,1));

$j++;

$hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));

}

return $hash;

}

function decode($string,$key) {

$key = sha1($key);

$strLen = strlen($string);

$keyLen = strlen($key);

for ($i = 0; $i < $strLen; $i+=2) {

$ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));

if ($j == $keyLen) { $j = 0; }

$ordKey = ord(substr($key,$j,1));

$j++;

$hash .= chr($ordStr - $ordKey);

}

return $hash;

}

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><? echo htmlspecialchars($_GET["name"]) . '!'; ?></title>

<style type="text/css">

.BIG {

font-size: 250px;

font-weight: bold;

font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;

text-align: center;

}

</style>

</head>

<body class="BIG">

<?php

$name = htmlspecialchars($_GET['name']);

$urlENC = encode( $name ,"whateverkey");

$urlDEC = decode( $urlENC ,"whateverkey");

echo htmlspecialchars($_GET["url"]) . '';

echo $urlENC; ?>

<br />

<? echo $urlDEC; ?>

网友答案:

So yes, you need to do it using javascript, but you can't use SHA because it will generate a hash, and you won't be able to get your data back on the server side. For my example I'll be using hex, which isn't very safe (it's very simple to decode) :

Javascript function :

function stringToHex (tmp) {
    var str = '';
    var tmp_len = tmp.length;
    var c = 0;
    for (var i=0; i < tmp_len; i++) {
        c = tmp.charCodeAt(i);
        str += c.toString(16);
    }
    return str;
}

Use this on your parameter before posting.

Now, PHP side :

function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

There you go.

相关阅读:
Top