Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

ASP Equivalent of php command uniqid()

Hello,

I'm trying to convert a php script into classic ASP.  I need to find the equivalent uniqid() in ASP.

The function is defined as:

The uniqid() function generates a unique ID based on the microtime (current time in microseconds).

Many Thanks.
Avatar of Badotz
Badotz
Flag of United States of America image

This is how I do it:

var stamp = get_time_stamp();

Functions below...
	function pad_me(how) { 
		//
		// Expects:
		// how.chr		Pad character
		// how.len		Length of pad string
		//
		// Returns
		// how.result	String of (how.chr) characters of length (how.len)
		//
		how.result = ''; 
		if (how.len == undefined || how.len < 2) { how.len = 2; }
		if (how.chr == undefined ) { how.chr = '0'; }
		for (var i = 0; i < how.len; i++) { 
			how.result += how.chr; 
		} 
		return how; 
	}
	//
	function zee_me(how) { 
		//
		// Expects:
		// how.txt		String to fill
		// how.chr		Pad character
		// how.len		Length of pad string
		//
		// Returns
		// how.result	String of (how.len) (how.chr) characters
		//
		var txt = pad_me(how).result + how.txt;
		how.result = txt.substr(txt.length - how.len);
		//
		return how; 
	}
	//
	function get_time_stamp() {
		//
		// Returns
		// job.result		Time stamp
		// job.parts		YYYY,MM,DD,hh,mm,ss,mili
		//
		var d = new Date();
		var how = { 'txt':'', 'chr':'0', 'len':2 };
		var job = { 'parts':[] };
		//
		job.parts.push(d.getFullYear());
		//
		how.txt = d.getMonth() + 1;	job.parts.push(zee_me(how).result);
		how.txt = d.getDate();		job.parts.push(zee_me(how).result);
		how.txt = d.getHours();		job.parts.push(zee_me(how).result);
		how.txt = d.getMinutes();	job.parts.push(zee_me(how).result);
		how.txt = d.getSeconds();	job.parts.push(zee_me(how).result);
		//
		how.len = 4;
		how.txt = d.getMilliseconds();	job.parts.push(zee_me(how).result);
		//
		job.result = job.parts.join('');
		//
		return job;
	}

Open in new window

Avatar of sybe
sybe

VBScript can not measure microseconds. The minimal unit of time measured in VBScript is 100/6 milliseconds (about 17 milliseconds).

Javascript which can also be used in Classic ASP) has a minimal time unit of 1 millisecond.

Depending on how many of those id's need to be generated, it might or might not be 'unique'
Right, but the chance of duplicates is very slim.

Since this is to duplicate a PHP function, one wonders: what is the time resolution of PHP? It can't be any finer than JavaScript? And PHP is strictly server-side, where JavaScript can work in both client AND server.

Add the value of the SessionID to the date value, or generate a "random" number with JavaScript and add that to the date value - either way, you improve the "uniqueness" of the value, but there are no guarantees...

You could investigate GuidGen (or something similar) that does guarantee a unique 32-bit value, but that might be overkill.
Avatar of andyw27

ASKER

Thanks for the code, I've tried it but the output I get is a blank page with:

[object Object]

Here is the code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
// JavaScript Document
 
	function pad_me(how) { 
		//
		// Expects:
		// how.chr		Pad character
		// how.len		Length of pad string
		//
		// Returns
		// how.result	String of (how.chr) characters of length (how.len)
		//
		how.result = ''; 
		if (how.len == undefined || how.len < 2) { how.len = 2; }
		if (how.chr == undefined ) { how.chr = '0'; }
		for (var i = 0; i < how.len; i++) { 
			how.result += how.chr; 
		} 
		return how; 
	}
	//
	function zee_me(how) { 
		//
		// Expects:
		// how.txt		String to fill
		// how.chr		Pad character
		// how.len		Length of pad string
		//
		// Returns
		// how.result	String of (how.len) (how.chr) characters
		//
		var txt = pad_me(how).result + how.txt;
		how.result = txt.substr(txt.length - how.len);
		//
		return how; 
	}
	//
	function get_time_stamp() {
		//
		// Returns
		// job.result		Time stamp
		// job.parts		YYYY,MM,DD,hh,mm,ss,mili
		//
		var d = new Date();
		var how = { 'txt':'', 'chr':'0', 'len':2 };
		var job = { 'parts':[] };
		//
		job.parts.push(d.getFullYear());
		//
		how.txt = d.getMonth() + 1;	job.parts.push(zee_me(how).result);
		how.txt = d.getDate();		job.parts.push(zee_me(how).result);
		how.txt = d.getHours();		job.parts.push(zee_me(how).result);
		how.txt = d.getMinutes();	job.parts.push(zee_me(how).result);
		how.txt = d.getSeconds();	job.parts.push(zee_me(how).result);
		//
		how.len = 4;
		how.txt = d.getMilliseconds();	job.parts.push(zee_me(how).result);
		//
		job.result = job.parts.join('');
		//
		return job;
	}
</script>
</head>
 
<body>
<script type="text/vbscript">
Dim stamp
stamp = get_time_stamp()
document.write(stamp)
</script>
 
</body>
</html>

Open in new window

Look at the comments in "get_time_stamp()" - see that it returns a JSON object?

Notice also that is JavaScript, not VBScript?

Change:

document.write(stamp)

to:

document.write(stamp.result);

for the complete timestamp, or

document.write(stamp.parts.join('));

to see the individual components.
And, of course, :

document.write(stamp.parts.join('));

should be:

document.write(stamp.parts.join(','));
ASKER CERTIFIED SOLUTION
Avatar of sybe
sybe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial