Reputation: 380
I have seen in some websites like facebook, twitter and youtuBe. Codes are being passed in the URL like the below example.
Wondering if anyone know the complete logic behind it (PHP,MYSQL). Thanks in advance.
code : QeAuADr0yL
Eg: http://instagram.com/p/QeAuADr0yL
Upvotes: 2
Views: 417
Reputation: 1090
The most common codes passed in URLs are hashes. There are different algorithms, most people use md5 but there are more secure ones. You can make your hash and then if you want to, lets say, limit access to a page depending on the hash being correct, you can check the value.
url.com?x=5273909c02b8ce7ae42a4e21542b3326
if( $_GET['x'] == md5('hashcode') ) {
// it matches
}else{
// it doesn't
}
There are other hasing algos, here's some docs.
You can get these from the URL when used as part of the path via the .htaccess file.
The specific algo used for those specific sites may be custom, but there are many preset functions in PHP that will let you mimic this.
To generate a shorter one you could take the first several desired characters with the substr() function and use a database to make sure there are no collisions. When a new code is created, truncate it to the length desired and query the database to see if it has been used, insert if not.
Or you could write your own and use the same process with the database to make sure there are no collisions.
If you're going to have a lot of hashes, you might want to take some time making a good random string generator that incorporates all letters and numbers, the shorter your hash the greater the chance of collisions and the smaller the pool of possible combinations. Increasing the possible characters from 15^(characters used) with md5 to 36^(characters used) will greatly increase your pool of possible combinations.
Upvotes: 0
Reputation: 4801
When the request http://instagram.com/p/QeAuADr0yL gets parsed by the apache web server, using the code stored in the .htaccess file, it knows that the first parameter value is p
and the second parameter value is hdashfdsahgofosa
Your request may be translated as www.a.com/index.php?p1=a&p2=b
where p1
and p2
are variables and a
and b
are theyr values
these values are available using $_GET['a']
and $_GET['b']
method;
i will give you a full example of a htaccess file and the rules:
RewriteRule ^([a-zA-Z-]+)/(product)-([0-9]+)$ index.php?action=$1&product=$3 [NC,L]
the above code looks like www.a.com/aBcdE/product-1836455
in the browser
and it is being translated as :
$action = aBcde;
$product = 1836455;
the first parameter can have values beetween a-z and/or A-Z parameter 2 has values beetween a-z A-Z 0-9 -
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)/(producator)-([0-9]+)$ index.php?action=$1&producator=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(producator)-([0-9]+)$ index.php?action=$1&id=$2&producator=$4 [NC,L]
RewriteRule ^([a-zA-Z-]+)/(producatorx)-([0-9]+)$ index.php?action=$1&producatorx=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(producatorx)-([0-9]+)$ index.php?action=$1&id=$2&producatorx=$4 [NC,L]
RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)$ index.php?action=$1&id=$2 [NC,L]
RewriteRule ^(tag)/([a-z0-9]+)$ index.php?action=tag&tag=$2 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(update)-([a-z0-9]+)$ index.php?action=$1&id=$2&saction=update&code=$4 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(delete)-([a-z0-9]+)$ index.php?action=$1&id=$2&saction=delete&code=$4 [NC,L]
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/([a-zA-Z-]+)-([0-9]+)$ index.php?action=$1&id=$2&saction=$3&sid=$4 [NC,L]
RewriteRule ^(.*)-([0-9]+).html$ index.php?action=details&id=$2 [NC,L]
Upvotes: 1
Reputation: 86
http://instagram.com/p/QeAuADr0yL
The above link "QeAuADr0yL" specifies that unique identifier based on that it get the data.This identifier is encoded by using the md5() or sha1() or other..
Upvotes: 1
Reputation: 443
Are you referring to the 'GET' method?
If you see a website such as www.someurl.com/?param=value&otherparam=othervalue this is known as GET.
It creates an array that can be accessed in PHP using something like
<?php echo $_GET["param"]; ?>
This will print out the value of the param, in this case, "value".
Upvotes: 0