Jump to content

Archived

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

oracle259

php coding problems

Recommended Posts

How can i simplify the switch section of this code

[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;
?>

[/code]

Share this post


Link to post
Share on other sites
Use a for loop instead. Use the following code in place of your switch statement.
[code]    if($seq != 0)
    {
        for($j = 0; $j < $seq; $j++)
        {
            $sessid .= $token{$i};
        }

        $sessid .= $hash{$i};
    }
    else
    {
        echo "i equals 0";
    }[/code]

Share this post


Link to post
Share on other sites

×

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.