Important alert: (current site time 7/15/2013 11:26:40 AM EDT)
 

VB icon

Session ID

Email
Submitted on: 11/6/2000 9:33:44 AM
By: Benoit Gauthier  
Level: Beginner
User Rating: By 5 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 26545
 
     Generate a random string of caracters to use for identifying your users on your web site. Can also be used to generate passwords.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Session ID
//**************************************
No Copyrights, use as is.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Session ID
// Description:Generate a random string of caracters to use for identifying your users on your web site. Can also be used to generate passwords.
// By: Benoit Gauthier
//
// Inputs:nSize , number of caracters for the string default =24
//
// Returns:A string with the random caracters in it.
//
// Assumes:Uses PHP.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=225&lngWId=8//for details.//**************************************

<?
	//----------------------------------------------------
	// Function GetSID()
	//
	// Parameters : $nSize number of caracters, default 24
	// Return value : 24 caracters string
	//
	// Description : This function returns a random string
	// of 24 caracters that can be used to identify users
	// on your web site in a more secure way. You can also 
	// use this function to generate passwords.
	//----------------------------------------------------
	function GetSID ($nSize=24) {
		
		// Randomize
		mt_srand ((double) microtime() * 1000000);
		for ($i=1; $i<=$nSize; $i++) {
			
			// if you wish to add numbers in your string, 
			// uncomment the two lines that are commented
			// in the if statement
			$nRandom = mt_rand(1,30);
			if ($nRandom <= 10) {
				// Uppercase letters
				$sessionID .= chr(mt_rand(65,90));
		//	} elseif ($nRandom <= 20) {
		//		$sessionID .= mt_rand(0,9);
			} else {
				// Lowercase letters
				$sessionID .= chr(mt_rand(97,122));
			}
			
		}		
		return $sessionID;
	}
	// Test the function 
	echo GetSID(16);
?>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
12/29/2000 1:47:12 AMJohn Lim

I like the code but I have some issues.

1. The only randomness comes from the initial random seed. It is really necessary to call mt_rand so many times? Why not return the first mt_rand? There is nothing random about calling mt_rand multiple times if the seed does not change. You need to set the seed again within the loop to make it more random.

2. On a multi-processor system, it is possible that 2 people be given the same id.

A possible improvement is to add to the random number seed the ip number of the client.

Bye, John
(If this comment was disrespectful, please report it.)

 
10/30/2001 4:59:12 AMbawy

Umm, this is the most pointless 5 million lines of code, you could just write and have the EXACT SAME RESULT!!! Learn PHP Ian so you can maintain some integrity on your web site!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.