Giter VIP home page Giter VIP logo

sqids-php's Introduction

Latest Version Build Status Monthly Downloads

Sqids (pronounced "squids") is a small library that lets you generate unique IDs from numbers. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.

Features:

  • Encode multiple numbers - generate short IDs from one or several non-negative numbers
  • Quick decoding - easily decode IDs back into numbers
  • Unique IDs - generate unique IDs by shuffling the alphabet once
  • ID padding - provide minimum length to make IDs more uniform
  • URL safe - auto-generated IDs do not contain common profanity
  • Randomized output - Sequential input provides nonconsecutive IDs
  • Many implementations - Support for 40+ programming languages

๐Ÿงฐ Use-cases

Good for:

  • Generating IDs for public URLs (eg: link shortening)
  • Generating IDs for internal systems (eg: event tracking)
  • Decoding for quicker database lookups (eg: by primary keys)

Not good for:

  • Sensitive data (this is not an encryption library)
  • User IDs (can be decoded revealing user count)

๐Ÿš€ Getting started

Require this package, with Composer, in the root directory of your project.

composer require sqids/sqids

Then you can import the class into your application:

use Sqids\Sqids;
$sqids = new Sqids();

Important

Sqids require either the bcmath or gmp extension in order to work.

๐Ÿ‘ฉโ€๐Ÿ’ป Examples

Simple encode & decode:

$sqids = new Sqids();
$id = $sqids->encode([1, 2, 3]); // "86Rf07"
$numbers = $sqids->decode($id); // [1, 2, 3]

Note

๐Ÿšง Because of the algorithm's design, multiple IDs can decode back into the same sequence of numbers. If it's important to your design that IDs are canonical, you have to manually re-encode decoded numbers and check that the generated ID matches.

Enforce a minimum length for IDs:

$sqids = new Sqids(minLength: 10);
$id = $sqids->encode([1, 2, 3]); // "86Rf07xd4z"
$numbers = $sqids->decode($id); // [1, 2, 3]

Randomize IDs by providing a custom alphabet:

$sqids = new Sqids(alphabet: 'FxnXM1kBN6cuhsAvjW3Co7l2RePyY8DwaU04Tzt9fHQrqSVKdpimLGIJOgb5ZE');
$id = $sqids->encode([1, 2, 3]); // "B4aajs"
$numbers = $sqids->decode($id); // [1, 2, 3]

Prevent specific words from appearing anywhere in the auto-generated IDs:

$sqids = new Sqids(blocklist: ['86Rf07']);
$id = $sqids->encode([1, 2, 3]); // "se8ojk"
$numbers = $sqids->decode($id); // [1, 2, 3]

๐Ÿ“ License

MIT

sqids-php's People

Contributors

4kimov avatar vinkla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqids-php's Issues

More letters than set - thoughts?

Hi guys,

I've been testing both Hashids and Sqids these days. And I have a bit of a problem with Sqids.
My alphabet with Sqids consists of shuffled small-caps alphabet and numbers. And regardless of shuffling when set to 6 characters it always start producting 7 char id from the same position... And it's just so low number to look like it run out of combinations (35936).

slika

This is not the case for Hashids, which easily generated ids for more than a 1M ids, so even the last one have 6 chars.

What is the deal here? Is the Hashids have much higher chance of creating duplicates or something else?
Can I force Sqids to create ids strictly to 6 chars?

Downgrade to PHP 8.1?

The php version would have to be downgraded to php 8.1, which is still active until Nov 25, 2023, and would prevent Symfony, for example, from working by default (via config.platform).

Is it in preparation?

I've come from this link: hashids/hashids.github.io#44. As a result, I was wondering whether you've considered implementing this feature yourself in PHP, or whether you're waiting for a new maintainer to step in.

As soon as this feature is available in PHP, I'd be delighted to contribute by adapting it for Symfony, if that suits your needs. I look forward to collaborating on this project.

best practices for implementation shuffle length and id length?

Hello,

I would like to seek advice on implementation based on some of the caveats mentioned in documentation where identical hash outputs are the same:

  • Should id lengths beyond a certain size be split into arrays?
  • Is there a relation between shuffle length and max id length support?

Thanks for replying

I need PHP 8.1

The production environment is using 8.1, which is difficult to upgrade to 8.2

Publishing v0.1.0

@vinkla The main lib & tests are there, there's not much extra to add from the spec. Anything else would be PHP-specific. A few questions for you before we publish v0.1.0:

  1. Is there a good way to export DEFAULT_BLOCKLIST so it can be used outside of the package like the other 2 default constants?
  2. The encoding/decoding tests are passing (which means they're producing the right IDs), but I'm not sure what the upper limit for PHP ints would be. Is bcmath/gmp usage correct along with max upper bound?
  3. Same for mb_ functions. The spec does not promise Unicode support (too many issues). Anything to adjust in this lib regarding that? Is it even worth having mb_ functions?

Issue - Inconsistent Decoding with Appended Characters in Sqid

When appending additional characters to the Sqid (Sequential ID), the decoding process consistently results in the same ID, regardless of the appended characters. For instance, if the original ID is "000," appending characters like "fsadqwop321" still yields the decoded ID as "1." This behavior appears inconsistent with the intended decoding process.

Context

I am utilizing route binding in my application to decode IDs, employing the following function:

public function decode($hash)
{
    $decoded = $this->sqids->decode($hash);
    if (empty($decoded)) {
        // Handle invalid sqids
        throw new InvalidArgumentException('Invalid hashid provided.');
    }
    return $decoded[0];
}

Expected Behavior

The decoding process should only be successful if the Sqid matches exactly. Appending additional characters should not alter the decoding result.

Steps to Reproduce

Generate a Sqid, e.g., "000."
Append random characters to the Sqid, such as "000fsadqwop321."
Decode the modified Sqid using the provided decoding function.
Observe that the decoded ID remains the same, despite the appended characters.

Does not generate unique ID

๐Ÿšง Because of the algorithm's design, multiple IDs can decode back into the same sequence of numbers. If it's important to your design that IDs are canonical, you have to manually re-encode decoded numbers and check that the generated ID matches.

The above text is mentioned in the ReadMe file. Can you please elaborate on it a little more?

I was planning to use it with the primary key, but if multiple encoded IDs are decoded as the same key, then this might not be useful. Do you have plans to fix this issue soon?

In the previous version of this library (https://github.com/vinkla/hashids) did not mention this issue, is this new to sqids?

Encoded value can have characters behind it when decoding

Hi there!

First of all, great features! Love it.

I came across the following. When I encode a database id, for example 1, I get an encoded value back. I've set my own alphabet and a pad of 8.

Now when I want to decode it I saw that I can place as many characters behind it as I want.

Example
ID: 1
SQID: 9fl48ghy

When I decode 9fl48ghy, I get [1].
But when I decode 9fl48ghyjf0293jf09sjd0f9j209fj, I get also [1].

Also when I change the last character it still passes? ๐Ÿ˜ฑ (9fl48ghy --> 9fl48gha)

Is this normal behavior or is it a bug?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.