PHP ³¼ö¸¦ ÀÌ¿ëÇÑ ÄíÆù¹øÈ£ »ý¼ºÇϱâ |
|
|
 |
13³â Àü |
PHP ³¼ö¸¦ ÀÌ¿ëÇÑ ÄíÆù¹øÈ£ »ý¼ºÇϱâ
php¿¡¼ ³¼ö¸¦ ±¸Çϱâ À§Çؼ´Â srand() ÇÔ¼ö¿Í rand() ÇÔ¼ö°¡ »ç¿ëµË´Ï´Ù. srand()ÇÔ¼ö´Â Á÷Á¢ ³¼ö¸¦ »ý¼ºÇÏ´Â ÇÔ¼ö°¡ ¾Æ´Ï¶ó rand()ÇÔ¼ö°¡ Á¦´ë·Î µÈ ³¼ö¸¦ »ý¼ºÇϵµ·Ï ³¼ö°ªÀ» ÃʱâÈÇÏ´Â ÇÔ¼ö·Î srand() ÇÔ¼ö¸¦ »ý·«ÇÏ¿´À» °æ¿ì¿¡´Â °è¼ÓÇؼ ÀÏÁ¤ÇÑ ¸ð¾çÀÇ, ³¼ö´äÁö ¾ÊÀº ³¼ö°¡ »ý¼ºµÇ´Â °ÍÀ» º»ÀûÀÌ ÀÖÀ¸½Ç °Ì´Ï´Ù.
rand() ÇÔ¼ö´Â ÀÎÀÚ·Î ¹ÞÀº min°ú max»çÀÌÀÇ °ªÁß ÀÓÀÇÀÇ Á¤¼ö¸¦ ¹ÝȯÇÕ´Ï´Ù.
int rand([int min], [int max])
¡Ø php4.2 ¹öÀüºÎÅÍ´Â srand()ÇÔ¼ö·Î ³¼ö°ªÀ» ÃʱâÈ ÇÒ ÇÊ¿ä°¡ ¾øÀÌ ÀÚµ¿À¸·Î ÃʱâÈ µË´Ï´Ù.
¾Æ·¡´Â "ASP¿¡¼ ³¼ö¸¦ ÀÌ¿ëÇÑ ÄíÆù¹øÈ£ »ý¼º ¿¹Á¦"¸¦ php ¹®¹ýÀ¸·Î ¹Ù²Û ¿¹Á¦·Î ¿¹Á¦ÀÇ ¼³¸íÀº asp¿Í Å©°Ô ´Ù¸£Áö
¾Ê±â ¶§¹®¿¡ ¾Æ·¡ ¸µÅ©Å¬¸¯ ÈÄ È®ÀÎÇϽô °ÍÀ¸·Î ´ëüÇÏ°Ú½À´Ï´Ù.
ASP¿¡¼ ³¼ö¸¦ ÀÌ¿ëÇÑ ÄíÆù¹øÈ£ »ý¼º º¸·¯°¡±â
¹è¿À» ÀÌ¿ëÇÑ ¹æ¹ý(½Ã°£ÀÌ ³Î³ÎÇÑ ºÐµé¿¡°Ô ±ÇÀå)
<?php
$couponArray = array(
0=>"A",1=>"B",2=>"C",3=>"D",4=>"E",
5=>"F",6=>"G",7=>"H",8=>"I",9=>"J",
10=>"K",11=>"L",12=>"M",13=>"N",14=>"O",
15=>"P",16=>"Q",17=>"R",18=>"S",19=>"T",
20=>"U",21=>"V",22=>"W",23=>"X",24=>"Y",
25=>"Z",26=>"0",27=>"1",28=>"2",29=>"3",
30=>"4",31=>"5",32=>"6",33=>"7",34=>"8",
35=>"9"
);
srand((double)microtime()*1000000); //³¼ö°ª ÃʱâÈ
for($i=0;$i<16;$i++){
$randNo = rand(0,35); //0°ú 35»çÀÌÀÇ ³¼ö¸¦ ±¸ÇÑ´Ù
$resultStr .= $couponArray[$randNo];
}
echo$resultStr;
?>
ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ ¹æ¹ý
<?php
$couponNo = getCoupon(16);
echo$couponNo;
function getCoupon($couponLength, $couponString=""){
$defaultString = "ABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
srand((double)microtime()*1000000);
if ( $couponString == "" ){ //$couponStringÀÇ °ªÀÌ Á¤ÇØÁöÁö ¾Ê¾Ò´Ù¸é $defaultString °ªÀ¸·Î »ç¿ë
$couponString = $defaultString;
}
$length = strlen($couponString);
for($i=0;$i<$couponLength;$i++)
{
$couponStr = rand(0,$length-1); //0¿¡¼ $defaultString¶Ç´Â $couponStringÀÇ ±æÀÌ»çÀÌÀÇ ³¼ö¸¦ ±¸ÇÑ´Ù
$resultStr .= substr( $couponString, $couponStr, 1 );
}
return $resultStr;
}
?>
|
|
̵̧ : 540 |
̵̧
¸ñ·Ï
|
|