Jump to content

Create random code and assign to a session variable


adrianle

Recommended Posts

Simple example of a slightly more complicated system...

 

$valid_chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
echo generate_password( 6, $valid_chars );

function generate_password( $length, $valid_chars ) {
  $count = strlen( $valid_chars ) - 1;
  $pass = '';
  for( $i = 0; $i < $length; $i++ ) {
    $pass .= $valid_chars[rand( 0, $count)];
  }

  return $pass;
}

Link to comment
Share on other sites

$valid_chars = 'abcdefghijklmnopqrstuvwxyz0123456789';

echo generate_password( 6, $valid_chars );

function generate_password( $length, $valid_chars ) { 

$count = strlen( $valid_chars ) - 1; 

$pass = '';  for( $i = 0; $i < $length; $i++ ) {   

$pass .= $valid_chars[rand( 0, $count)];  } 

return $pass;}

$_SESSION['PSSWD'] = $pass;

 

This DOES generate the value.. I can see it on this page.. but on the next page where it should pull form the session variable "PSSWD" it doesn't display anything!

Link to comment
Share on other sites

OK, very odd.. I've got it working.. mostly.. but the value of the session variable that gets generated and then inserted into the table record, does NOT match the 6-character session variable value displayed on the next page!!  It's almost like the second page is displaying a separately generated variable value .. but why?? All I'm asking it to do is echo the session variable....???

Link to comment
Share on other sites

You need some conditional logic ( and if(){} statement) so that the value is only generated and assigned to the session variable once (if it is not set.)

 

If you are unconditionally generating and assigning the value, it will be get changed on every page request.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.