Discount !! E-commerce App With Backend Source Code Video and Voice Chatting App Firebase Chatting App Source Code Complete Gym App BLoC State Management Source Code Complete Study App Buy Ticket Booking App Source Code Payment App Buy Travel App With Backend Source Code Complete Chat App Udemy Course Special Offer Discount !! Online Learning Course App (BLoC) Discount !! Online Learning Course App (Riverpod) Discount !! Online Learning Course App (Getx)
Generating access token is easy in PHP, the only thing matters is that how secure is the token. There are many alogrithm that can generate secure code in PHP.
What is access token?
It's token generated by your server, that's used to communicate between your app and server. With access token, app identifies with the server that, it's legit. Then we can use this access token to verify a lot of thing during communication.
In our case we will use some built in functions to do it. The functions are
The combination of these three functions will make a secured powerful access token. Let's take a look at them one by one and then we combine them together
It's a built in function that generates random number. It takes two parameters. First one is the minimum number and the second one is the maximum number.
We will generate random number that represent million
That would generate a random number from 1000000 to 9999999.
The above function generates an identifier. It's also unique but not 100% gauranteed. Let's see the an example.
<?php // generating unique id echo uniqid(); ?>
It will have output like below
In your case it should be different. It depends on your machine hardware clock.
Since uniqid() does not return gaurantee to return unique identifier, we can combine rand() and uniqid() together.
We may combine them like below syntax
With the identifier we are connecting a random number. It should be pretty unique.
How to use MD5 in PHP? To calculate the MD5 hash of a string PHP has a pre-defined function md5(). The md5() function calculates the MD5 hash of a string input and returns the hash hexadecimal number. The md5() function uses the MD5 Message-Digest Algorithm.
Combine uniqid(), rand() and md5()
Combining all the above functions we may generate a secure token which would be used to communicate between app and server.
$access_token = md5(uniqid().rand(1000000, 9999999));
This token should be saved in the database.