Encrypting a string in PHP [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionOkay, so I've got a php script something like the following:
<?php
$name = "Steve Jobs";
header('Location: http://www.apple.com/custom.php&name=' . $name);
?>
Now, I happen to find it kind of embarrasing having the URL of this custom "Hi there, Steve Jobs" page have a URL of http://www.apple.com/custom.php&name=Steve%20Jobs
, and would therefore like to encode it.
Is there an easy way to make this name be more obscure? Is there a way to avoid passing it in the URL altogether?
To avoid passing variables around in the URL you could use session variables using the $_SESSION
array.
You can check out the PHP docs for some basic usage, as well as other examples.
The easiest way is to base64_encode($name), then base64_decode on the other end.
First, you can store the name in cookie or session, read it on the next page, and remove it from session/cookie. In this way, it won't be included in the URL anyway
If you just do not want to pass it as a plain text, you can use base64_encode() and base64_decode(). But this won't stop any experienced user to read the actual value.
Use mcrypt() to crypt the message, base64_encode() to make the crypted version ASCII, include it in the URL, and on the next page decode and then decrypt. Be carefull, a lot of chippers use seed value, which also have to be saved and send to the next page, in order to be able to decrypt.
All scenarios assume the next page is part of the same application (or both applications are yours). If you want to encrypt the string for any URL, you can't, as the page you are redirecting to won't be able to read it.
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 ("TEXT PLAIN", true);
echo $enc_txt."\n";
// KjUkNTg1IiEwMyQlKTNAYGAKYAo=
echo encrypt_decrypt ($enc_txt, false);
// TEXT PLAIN
精彩评论