Ive seen on some PHP codes, that when u view a topic or a post it is a random numver like 100044536....instead of just a numeric 1 or 2, 3, 4, 5 and so on...how do you generate random ID's?

    function gen_pswd($length="15")
    {
    	unset($new_pswd);
    	for( $j=0; $j < $length; $j++ )
    	{
    		if( ($eh = rand(71,122)) < 97 )
    		{
    			$new_pswd .= substr($eh, 0, 1); 
    		}
    		else
    		{
    			$new_pswd .= chr($eh);
    		}
    	}
    	return( $new_pswd );
    }
    
    

    That one is for alpha and numeric, but it can be easily modified.

      just wondering...
      may that have been session ids what you saw?
      these are created automatically (and appended to the url depending on the session settings) whenever you use sessions.

        yes, come to think of it...how do you generate them?

          sorry for the brief answer, there'd probably be more to say on what's going on internally - you simply use session_start(); and the session id is generated for you and either stored in a cookie or appended to the url.

          see http://www.php.net/manual/en/ref.session.php

            My guess is that it's not a random numer. The reason I say this is that in order to do that randomly you would have to geneerate a random number then check that it's not already used, that would be a lot of time for a forum of any size. Then you also loose all of the sorting abilities you get with an autonumber field.

            It it probably some hashed together number. Like puting the userid, forumid and post id all rogether in one string. If you start the auto numbering of userid and forumid in the thousands then you'll get numbers like the one posted above.

              Write a Reply...