Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:37:40+00:00 2026-06-09T04:37:40+00:00

Can anyone suggest a versatile PHP encrypt/decrypt algorithm that encrypts in the following way:

  • 0

Can anyone suggest a versatile PHP encrypt/decrypt algorithm that encrypts in the following way:

  1. it’s fast
  2. it’s short, similar to YouTube’s video ids
  3. can be used as a valid id (an elements attribute)
  4. can be used as part of a URL safely

Security is not the primary concern here. I’m just wanting to prevent the casual “hacker” from easily accessing certain pages by changing the URL (e.g. http://www.domain.com/?id=1 can easily be changed to http://www.domain.com/?id=2).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:37:43+00:00Added an answer on June 9, 2026 at 4:37 am

    If you really really really want to encrypt your primary key (Highly inefficient, will explain later) then use

    $url = substr(md5(uniqid($row['id'], true)),0,6);
    

    Where row[‘id’] is your primary key. This creates a url/html safe 6 character string, all will be unique (kind of, see below).

    Now. This is why you should NOT do this.

    1. Encryptions should always take place in the backend when uploading data to the sql database, not client side. The general rule is less client side processing the better. It is the difference clientside from pulling $row[‘url’] from your sql database where $row[‘id’] is the key, or pulling the id then running an encryption. That adds 1 more step client-side.
    2. Although highly unlikely, using an encryption like the one below has the potential to have duplicates. (If your site has 1000+ keys your chances of a duplicate is higher) so to prevent a duplicate you would need to encrypt your key, then do an sql search to retreive ALL of your keys, encrypt EACH key, then compare EVERY key to the current encrypted key. That adds 4x(however many keys you have) to your processing time.
    3. Really it is just bad form. If forever reason you wanted to search for a page based on the encrypted url, you would have to again retrieve ALL keys and encrypt + compare all of them.

    For everyone else USE THIS if you want efficiency

    I have the script to create the unique id

    $token = substr(md5(uniqid(rand(), true)),0,6); // creates a 6 digit token
    

    I use a mysql database to store previously used id’s, you could use any other kind of database to store the Id’s.

    function generateUniqueID () {
      $token = substr(md5(uniqid(rand(), true)),0,6); // creates a 6 digit token
      $query = "SELECT count(*) FROM table WHERE url = $token";
      $result = mysql_query($query, $connection) or die(mysql_error());
      $numResults = mysql_num_rows($result);
      if ($numResults) {
        generateUniqueID();
      }
    }
    

    Using this code you have ONE step client-side, to get the row where id then you receive the row[‘rl’].

    Please read up on program efficiency and take a look at the documentation for mysql, do so and you will get more happy clients 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone suggest a way to shave any time off this script? var countObject
Can anyone suggest some fast ways to calculate the lowest common factor(excluding 1) of
Can anyone suggest a book or a tutorial on creating Charts in PHP extracting
Can anyone suggest a CMS able to offer the following features: Free/open source Support
can anyone suggest the neatest way to do this comparison? I need to test
Can anyone suggest some clustering algorithm which can work with distance matrix as an
Can anyone suggest any good cURL based PHP browser / spider / crawler /
Can anyone suggest a pattern that can be used for writing a JavaScript API
Can anyone suggest how I can redirect any traffic the following path to another
Can anyone suggest a good control that can be used to display images (png's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.