oracle259 Posted October 20, 2006 Share Posted October 20, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/24580-php-coding-problems/ Share on other sites More sharing options...
wildteen88 Posted October 20, 2006 Share Posted October 20, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/24580-php-coding-problems/#findComment-112003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.