Jump to content


Photo

php coding problems


  • Please log in to reply
1 reply to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 20 October 2006 - 06:28 PM

How can i simplify the switch section of this code

<?php

/* Generate Security Token */
$token='';
for ($i=1;$i<=100;$i++) {
$seedID = substr('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789~!@#$%^&*+?.', rand(1,73), 1);
$trackingID .= trim($seedID);
}
$token = sha1(uniqid(trim($trackingID)));

echo "Token<br>".$token."<br>";

/* Generate Hash */
$salt = (uniqid(rand(),225));
$hash = sha1(trim($salt));

echo "Hash<br>".$hash."<br>";

/* Generate Combination Sequence */
$sequence='';
$seq = trim(rand(1, 9));

echo "Sequence<br>".$seq."<br>";

/* Generate SessID */
$sessid = '';
for($i=0;$i<strlen($token);$i++) {

switch ($seq) {
case 0:
    echo "i equals 0";
    break;
case 1:
    $sessid .= $token{$i}.$hash{$i};
    break;
case 2:
     $sessid .= $token{$i}.$token{$i}.$hash{$i};
    break;
case 3:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;
case 4:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;
case 5:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;
case 6:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;
case 7:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;
case 8:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;
case 9:
     $sessid .= $token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$token{$i}.$hash{$i};
    break;

}

  $encrypt_sessid = sha1(trim($sessid));
  }
echo "SessID<br>".$sessid;
echo "<br>Encrypt SessID<br>".$encrypt_sessid;
?>



#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 October 2006 - 06:43 PM

Use a for loop instead. Use the following code in place of your switch statement.
    if($seq != 0)
    {
        for($j = 0; $j < $seq; $j++)
        {
            $sessid .= $token{$i};
        }

        $sessid .= $hash{$i};
    }
    else
    {
        echo "i equals 0";
    }





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users