09 March 2011

How to encrypt decrypt url or code in php

When developing web applications sometimes we encounter situations where we need to encrypt/decrypt url or some code or some information. For such situations I wrote a simple php script using the default php functions:
  • base64_encode()
  • base64_decode()
Now the use of these functions is quite simple. For example if you want to pass some critical information like password in the url like:
http://www.example.com/test.php?password=skills2earn
Then you can encode it using the following code:
http://www.example.com/test.php?password=<?php echo base64_encode('skills2earn'); ?>
The output of the above code will be:

http://www.example.com/test.php?password=c2tpbGxzMmVhcm4=

Now when you want to decode/decrypt it you can do it using the following code:
<?php
echo base64_decode('c2tpbGxzMmVhcm4=');
?>
The output of the above code will be:
skills2earn

Well guyz that was a very simple php encryption and decryption technique, if you have any queries or suggestions, please feel free to comment.

3 comments:

asfund said...

this was very helpful for me. thank you

siva said...

nice post but small suggestion from my perspective is

if i copy encrypted data form url and i pass the same value in "base64_decode()" in my php file then i will get encrypted text, i.e original password i can see

so its unsecure, its my option

Anonymous said...

yes, ryt i agree with SIVA,