2

I know this is possible and I researched it in many websites, but didn't find anything that could do exactly what I need.

I need to encode and store two parameters in URL and then retrieve it with $_GET method.

The parameters are not sensitive data. My purpose is mainly to obfuscate it, so the visitors can't read it without website accessing.

I have something like this:

site.com/?name=john&food=banana%20split

and I need it to become something like this:

site.com/g57HT90dw8lC5p

How can I do this? Thanks.

(Sorry for my bad english)

EDIT: I should explain it a bit more.

  1. I know I'll have to use mod_rewrite and I got that part covered;
  2. The second parameter is a complete sentence (with spaces, punctuation, etc.);
  3. I need this to create a Christmas Card, so none of these parameters require secure methods. What I really need to store is just a name and a simple message.
5
  • by the looks of the url's you want mod_rewrite as well. and perhaps POST or sessions, to make them 'invisible'
    – user557846
    Nov 20, 2012 at 19:23
  • mod_rewrite cannot hide your data you are rewriting, isn't it? :) Nov 20, 2012 at 19:26
  • yes but hes changing the url, with out mod_rewrite g57HT90dw8lC5p is a directory\file
    – user557846
    Nov 20, 2012 at 19:29
  • Yes, I know I need mod_rewrite and I got that pate covered. I just need to merge parameters and encode it. :) Nov 20, 2012 at 19:38
  • depending on the message length you may have an issue with how long the url is. why not store the data in a db, and an id in the url
    – user557846
    Nov 20, 2012 at 20:14

5 Answers 5

4

This is wrong approach - you said it is not sensitive so you better fix your code it will not be hacked if someone send you crafted data. Obfuscating prevents average users from doing changes but noone skilled be fooled by this. Also many browsers GET is limited (so can be configured PHP and server) so this may hit the wall sooner than you want. But if you insist you want this approach, you can put all variables into json_array, and then encode using base64.

$myData = array('foo'=>1, 'bar'=>'hax0r');
$arg = base64_encode( json_encode($myData) );

http://site.com/secret=$arg

and back:

$myData = json_decode( base64_decode( $_GET['secret'] ) );

But again - this is wrong by design.

6
  • 2
    it is wrong by design assuming you'd want any protection. But OP explicitly stated there is no sensitive data and only goal is to obfuscate, so I don't get why you think it is wrong by design?
    – eis
    Nov 20, 2012 at 19:28
  • 1
    If it is not sensitive why is he willing to do this in first place? Nov 20, 2012 at 19:29
  • maybe just to have more funny- or cool-looking urls, or a birthday present. I don't know, but I don't think there's anything wrong with it, as long as you do your parameter checks also.
    – eis
    Nov 20, 2012 at 19:35
  • Sure, there may be a "for fun" explanation, still if he is doing it seriously, then it's just plain wrong for reasons I gave. Nov 20, 2012 at 19:37
  • I edited my question, to explain my purpose a bit more. Yes, I don't need a secure method at all. I just want to obfuscate the data, because it will the text for a Christmas Card I'm doing. So I suppose that just encoding (and merging it in just one string) will be enough to do the trick. But is this may bring danger to my wbesite/server, I will not use it certainly. So, can I proceed with this piece of code? Nov 20, 2012 at 19:44
2

Append the two string values together; then use some encoding function like str_rot13 or whatever

1

You could just uuencode it.

On create

$converted = convert_uuencode('?name=john&food=banana');
// 6/VYA;64]:F]H;B9F;V]D/6)A;F%N80``

And on decoding

$query = $_SERVER['QUERY_STRING'];
// 6/VYA;64]:F]H;B9F;V]D/6)A;F%N80``
$real = convert_uudecode($query);
// ?name=john&food=banana

The point of uuencoding is to make data safe for network transmissions. It does not provide protection, but it will make them something else than clear text.

1

Try these PHP functions convert_uuencode and convert_uudecode

It is much safer than using only base64

function encrypt_decrypt ($data, $encrypt) {
    if ($encrypt == true) {
        $output = base64_encode (convert_uuencode ($data));
    } else {
        $output = convert_uudecode (base64_decode ($data));
    }
    return $output;
}

$enc_txt = encrypt_decrypt ("name=john&food=banana%20split", true);
echo $enc_txt."\n";
// PTtGJU05M1VKO1ZBTilGOU87VjBdOEYlTjg2WUEpMyhQPFchTDo3MGAKYAo=
echo encrypt_decrypt ($enc_txt, false);
// name=john&food=banana%20split
4
  • I already got my problem solved, but thanks anyway, if I need this in the future, sure I will use this instead of base64. But why are these two functions safer than base64, btw? Mar 29, 2014 at 0:26
  • because Base64 is very common and anyone can take the information and make a base64_decode to you URL and know what you are sending to your web page
    – atiruz
    Mar 31, 2014 at 3:13
  • you might even change some characters that only you know, so nobody else will know what you are sending
    – atiruz
    Mar 31, 2014 at 3:16
  • Oh nice! Thanks for the explanation. The purpose of this question was to help me on something that didn't required anything related to security. It was for a christmas card and the base64 was only to mask the query string, so it couldn't spoil the message to the user before the right time. Mar 31, 2014 at 6:34
0

Look at PHPs Doc Base64Encode . just encode the string, and decode it when reading the query.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.