[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP Equivalent of php command uniqid()

Posted on 2009-02-24
7
Medium Priority
?
1,689 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:andyw27
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 23723962
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

0
 
LVL 28

Expert Comment

by:sybe
ID: 23731516
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'
0
 
LVL 29

Expert Comment

by:Badotz
ID: 23732695
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:andyw27
ID: 23737620
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

0
 
LVL 29

Expert Comment

by:Badotz
ID: 23737900
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.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 23737908
And, of course, :

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

should be:

document.write(stamp.parts.join(','));
0
 
LVL 28

Accepted Solution

by:
sybe earned 2000 total points
ID: 23738827
If you want it in VBScript, fast and easy, and 17 milliseconds is unique enought, here you got it:

<%
Response.write UniqueTimeStamp()
 
Function UniqueTimeStamp()
    Dim dtNow
    dtNow = Now()
    UniqueTimeStamp = Year(dtNow) & Month(dtNow) & Day(dtNow) & Timer()
End Function
%>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question