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;
}

This is probably your answer. :) Or you can echo it, by just replacing the word "print" with "echo"

print $_SESSION['sessioname'];

 

If you're doing the method cags is doing, just asign $pass to a session variable as stated in my tiny example :)

$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!

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....???

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.