2

Sorry if the title's unclear, couldn't think of anything better since I'm still new to this area. :)

Anyway, my question is this: I want to send some information from one page (let's call it 1.php) to another page (let's call it 2.php) using this (don't know the formal name, sorry):

http://localhost/X/2.php?user_id=5&user_type=2&ssn=1234567890&first_name=John&last_name=Doe

As you can see, the information is in plain text, which I dislike. Is there an easy way to encrypt the string after the question mark above in 1.php, and then let the 2.php (that gets the passed-along info) decrypt it? I'd like for it to be something along:

http://localhost/X/2.php?user_id=rj3i15k&user_type=8109fk1JIf&ssn=6893kfj399JFk...

Sorry if this is a stupid question. Many thanks in advance!

BeeDog
  • 1,835
  • 5
  • 17
  • 20
  • 1
    What is it you dislike: The possibility that data can be easily manipulated (in that case see @blue112's [answer](http://stackoverflow.com/questions/4143239/how-to-encrypt-information-passed-along-in-url-when-redirecting-in-php/4143284#4143284)) or is it that you don't want to reveal the data in the first place? – jensgram Nov 10 '10 at 10:11
  • Mostly the fact that the data is revealed in plaintext with my initial (stupid) solution. But using a $_SESSION array does solve it neatly. :) – BeeDog Nov 11 '10 at 08:31

5 Answers5

4

If you don't want information to be modified, use a hash string to verify them.

For instance :

$hash = sha1($user_id."haha".$user_type.$ssn.$first_name.$last_name);

The "haha" here, is a salt. Use a random string, it will be use so someone can't reuse your algorithm to inject fake data.

Then put this hash at the end of your url, eg

http://localhost/X/2.php?user_id=5&user_type=2&ssn=1234567890&first_name=John&last_name=Doe&hash=$hash`

When you'll get this information, make the hash again, and compare it to the hash sent : If the information was modified, the hash won't match.

blue112
  • 52,634
  • 3
  • 45
  • 54
  • 2
    +1 You should really emphasize the use of some kind of private [salt](http://en.wikipedia.org/wiki/Salt_%28cryptography%29) ("haha"), though. – jensgram Nov 10 '10 at 10:13
  • 2
    You need to put delimiters between the values, otherwise I can re-use the hash of `user_type=2, ssn=12345` to submit the values `user_type=21, ssn=2345` (and similar). – caf Nov 10 '10 at 10:30
  • 1
    Many thanks, this sounds like a good and easy-to-implement solution! If I may ask, is there any risk in blatantly showing the "user_id" (the primary key in the database) that openly? Showing off the first/last names and the Social Security Number this openly is also a bit worrying... :) – BeeDog Nov 10 '10 at 10:34
  • 2
    Better yet, use HMAC (HMAC-SHA-256, HMAC-SHA1, even HMAC-MD5) instead of reinventing it poorly. – hobbs Nov 10 '10 at 10:35
  • No, there's no risk since user can't inject SQL data (I hope for you he can't) – blue112 Nov 10 '10 at 12:25
3

Maybe you're going about it the wrong way.

Thought about storing the data in a serverside session variable? Or even in a database (if you're passing to another machine), then you just need to send the unique identifier of the database entry.

page2 will then read the session variable, or retrieve it out of the database again.

Basically, keep the data serverside and then you wont need to encrypt/decrypt.

Session Example:

page1

<?
session_start();

$_SESSION['pagedata'] = array(
    'user_id'=>5,
    'user_type'=>2,
    'ssn'=>1234567890,
    'first_name'=>'John',
    'last_name'=>'Doe'
    );

header('Location: page2.php');
?>

page2

<?
session_start();

$user_id = $_SESSION['pagedata']['user_id'];
$user_type = $_SESSION['pagedata']['user_type'];
$user_ssn = $_SESSION['pagedata']['user_ssn'];
$user_first_name = $_SESSION['pagedata']['first_name'];
$user_last_name = $_SESSION['pagedata']['last_name'];

// use variables to do stuff
?>
Oliver O'Neill
  • 1,229
  • 6
  • 11
  • This is actually a really good idea, I feel stupid for not thinking about it. It pretty much solves my key question of showing the information clearly in the URL. Thanks for your reply, and thanks to everyone else, really appreciated! – BeeDog Nov 11 '10 at 08:30
2

Its called GET, never relate 100% on 2 Way Decryption but this may help you Best way to use PHP to encrypt and decrypt passwords?

Community
  • 1
  • 1
Hannes
  • 8,147
  • 4
  • 33
  • 51
2

you could use base64_encode on the one side and bas64_decode on the other - just as one possibility - but note that this is only for "better looking" url als you want it (for me, this is ugly). this isn't encrypting your data for being more safe or something like that - to achive this, use https and don't confuse your users by doing such crazy stuff.

oezi
  • 51,017
  • 10
  • 98
  • 115
1

You should use $_SESSION.

rook
  • 66,304
  • 38
  • 162
  • 239