PHP Random string generation help required

13 replies
hello people

I am new to php and I just wonder I want combination of random number and random string, how can I generate it using php?

Any help regarding this is highly appreciated.
#generation #php #random #required #string
  • Profile picture of the author bugtrack
    You can use rand function of php to generate random number and you can take part of the session_id to generate random string , if you combine both of them it will give you required result
    {{ DiscussionBoard.errors[8248745].message }}
  • Profile picture of the author Iluminada
    @bug thank you for your help but if you dont mind can you give me an example of what you have explain in php code?

    Thanks in advance
    {{ DiscussionBoard.errors[8248752].message }}
    • Profile picture of the author bugtrack
      Ok here is a simple code for you.

      PHP Code:
      session_start();
      echo  
      substr(session_id(),1,8).rand(); 
      {{ DiscussionBoard.errors[8248760].message }}
      • Profile picture of the author Iluminada
        its working fine right now but when i refresh the page first few characters remains same it only get new characters when i close browswer window and reopen it
        {{ DiscussionBoard.errors[8248769].message }}
        • Profile picture of the author bugtrack
          You can use

          PHP Code:
          session_regenerate_id(); 
          use this code after session_start i hope it will help and your problem will be solved, all the best
          {{ DiscussionBoard.errors[8248771].message }}
          • Profile picture of the author Brandon Tanner
            Here's another way to do it...

            $randomString = md5(rand());
            Signature

            {{ DiscussionBoard.errors[8249866].message }}
  • Profile picture of the author Paul Moss
    For some reason I'm not able to put this in full.

    Check out this link: http://stackoverflow.com/a/853898

    Pretty cool answer.
    {{ DiscussionBoard.errors[8250439].message }}
    • Profile picture of the author Rennell Garrett
      Originally Posted by Paul Moss View Post

      For some reason I'm not able to put this in full.

      Check out this link: How to create a random string using PHP? - Stack Overflow

      Pretty cool answer.
      This is a nice find. Truely a unique way of generating random stuffs. Thanks for the share.
      {{ DiscussionBoard.errors[8256802].message }}
      • Profile picture of the author freetutorial
        <?php

        function RandomTxt() {
        $chars = "thequickbrownfoxjumpsoverthelazydog";
        srand((double)microtime()*1000000);
        $i = 0;
        $pass = '' ;
        while ($i <= 10) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
        }

        return $pass;
        }

        echo RandomTxt();
        ?>

        above code useful to create random text
        {{ DiscussionBoard.errors[8260472].message }}
  • Profile picture of the author Iluminada
    You can geenrate very good random string by session and md5 combination

    echo md5(substr(session_id(),0,10));
    You can use both the function together and get good string
    {{ DiscussionBoard.errors[8261832].message }}
  • Profile picture of the author sktthemes
    Hello

    You can use uniqid(); to randomly ganerate numbers/string
    {{ DiscussionBoard.errors[8318221].message }}
  • Profile picture of the author weaveronline
    Signature

    Thanks & Regards,
    Reach us at dukeduke600@gmail.com.
    Web Design| Logo Design | Banner Design | Web Development | Mobile Applications [iPhone/iPad/Android/Windows Phone]

    {{ DiscussionBoard.errors[8334710].message }}
  • Profile picture of the author JimRiley
    This might help you to generate PHP password. You can download the script if required.
    PHP Password Generator
    {{ DiscussionBoard.errors[9440603].message }}

Trending Topics