开发者

Looking for encrypt/decrypt functions

开发者 https://www.devze.com 2023-01-10 12:33 出处:网络
I have an array of IDs (e.g. array(1, 2, 10, 34, 100, 101)) that represents files ids of pictures in database.

I have an array of IDs (e.g. array(1, 2, 10, 34, 100, 101)) that represents files ids of pictures in database.

I would like to send a link to customer by开发者_开发知识库 email such that when he press this link he will see the pictures, but I don't want customer to see these files ids!

Thus, I thought to encrypt the array of pictures ids with some key that customer doesn't know, and when customer clicks the link my PHP script will get the encrypted string, decrypt it back to the pictures ids, and send customer a page with the pictures.

Are there any PHP built in functions that can do the encryption/decryption with custom key for me ?

Thanks !!


How about giving a name to your pictures in the db that is not the same as the id?

When a user uploads a file you give the file a random name like the current timestamp and md5. The you store the name in your database and give it a id. That way you get your sequential ids and you get filenames that are very hard to guess. That and prevent visitors from displaying the content of the images folder and you are pretty set.


PHP has the mcrypt extension. It can do this.

Note that in the method you describe, the customer will see the picture IDs when you send him the page.


If it were me implementing this I'd just give the customer a random hash, then relate that hash to particular IDs in a database table a bit like:

link_hash VARCHAR(40) NOT NULL,
link_id INT(11) NOT NULL

(one or more rows per link_hash to allow multiple IDs)

As Borealid mentioned in his answer, you'll also need to ensure that when they view the page they won't see the IDs in the picture URLs either. This will be a separate challenge.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号