Random url


#1

Is it possible to make url variable and unique when we open it. Generally we can see this in some professional website.like;

http://url.com/token=727iah8q9082
http://url.com/token=7w9k82151718
http://url.com/token=72716817isk91u

#2

You could create a custom function to generate the random string.

function randstr($len, $type = 'mix'){	// $len = length of string; $type = type of characters to include, default to mix
	if($len < 1){ return false ;}		// Fail on invalid string length
	$alph = range('a', 'z') ;			// Set alphabetical range
	$numb = range(0, 9) ;				// Set numeric range
	$mix = array_merge($alph, $numb) ;	// Merge both for alphanumeric (mix) range
	$arrays = array('alph' => $alph, 'numb' => $numb, 'mix' => $mix) ;	// Array of various range types
	if(!key_exists(strtolower($type), $arrays)){ return false ;}		// Fail on invalid type given
	$range = $arrays[$type];	// Set the range type to use
	$str = '';					// Create empty string to start with
	for($len > 0; $len--;){		// Add to the string until required length is met
		$ran = array_rand($range);	// Pick a random value from the chosen range
		$str .= $range[$ran];		// Add it to the string
	}
	return $str ;	// And return the complete string
}

#3

You could use something like shortid too


#4

The function does not guarantee the token will be unique (there is diminishing likelihood with greater length), but that could be added, though the best way would depend on how this is to be used and how the tokens (used) are stored.


#5

Thank you for your answer. But it not work when I paste this code in php page.

I edit like this;

<?php
[YOUR_CODE]
?>

This is correct format?


#6

That will include the function in your script.
But when and how you call the function depends upon your needs.

If you are not familiar with php functions, read about them here:-
http://php.net/manual/en/language.functions.php


#7

I want apply this when any one open this website then show random string in address bar.


#8

What have you tried so far?
And how did that go?


#9

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.