Jump to content

php coding problems


oracle259

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]
Link to comment
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]
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.