6. Php

6.1. ³ëÇÁ·¹ÀÓ ¸¸µå´Â¹ý

¸Ó¸®´Ü°ú ¹Ù´Ú¿¡ µé¾î°¥ È­ÀÏÀ» ÀÛ¼ºÇÏ°í ³ª¼­ º» È­ÀÏ¿¡¼­ ºÒ·¯¼­ »ç¿ëÇÏ¸é µÈ´Ù.

main.php
 
<?php 

include("include/header.ph"); 

print_html("main_htm/top.htm"); 

echo("<br><br>º»¹® ³»¿ëÀÌ µé¾î°¡´Â °÷ÀÔ´Ï´Ù.<br>"); 
echo("³»¿ëÀº º°°Å ¾ø½À´Ï´Ù.<br>"); 
echo("ÇÁ·¹ÀÓÀ» ³ª´©Áö ¾Ê°í, º»¹® ³»¿ëÀ» »ç¿ëÇÏ´Â °ÍÀÔ´Ï´Ù.<br>"); 
echo("º»¹®³»¿ëÀ» ¼­ºê ÇÔ¼ö¸¦ ÀÌ¿ëÇؼ­ ¸¸µé¸é µË´Ï´Ù.<br>"); 
echo("´ÙÀ½ ÀÛ¾÷À» À§ÇØ ½¬¿î ³»¿ëÀ» ¾ÆÁÖ ¾î·Æ°Ô Çß½À´Ï´Ù.<br>"); 
echo("À̳»¿ëÀ» Àß ÀÌÇØÇÏ¸é ¿©·¯¸é¿¡¼­ µµ¿òÀÌ µÉ°ÍÀÔ´Ï´Ù.<br>"); 

print_html("main_htm/bottom.htm"); 

?> 
  

include/header.ph
 
<? 
/* ÇѶóÀÎ Ãâ·ÂÇØÁÖ´Â ÇÔ¼ö */ 
function print_html($filename)
{ 
	$fp = fopen( $filename, "r" ); 

	while(!feof($fp)) 
	{ 
		$contents .= fgets($fp, 100); 
	} 
           
  fclose($fp); 
  echo "$contents"; 
} 
?> 

	

main_htm/top.htm
<html> 
<head> 
<title>³ª¶óºñÀÇ ¹æ</title> 
<style TYPE="TEXT/CSS"> 
  A:link {color: blue; text-decoration:none} 
  A:visited {color: navy; text-decoration:none} 
  A:hover {color: red; text-decoration:underline;} 
</style> 
</head> 

<body> 
  <table border="0" width = "600" cellpadding="0" cellspacing="0"> 
    <tr><td width="589" colspan="2" bgcolor="#f9cd8a">³ª¶óºñÀÇ ¹æÀ» ã¾ÆÁּż­ ´ë´ÜÈ÷ °¨»çÇÕ´Ï´Ù.</td></tr> 
    <tr> 
      <tr> 
        <td width = "120" valign="top" colspan="0"> 
          <table width = "120" border="0"> 
            <tr><td width="120" bgcolor="#b5d490"><p> <b><br>ȨÀ¸·Î</b></font></td></tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">³ª¶óºñ ¼Ò°³</font></td></tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">³ª¸®ºñ ¼Ò½Ä</font></td></tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">Windows NT</font></td></tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">½Ã(±è¼Ò¿ù)</font></td> </tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">½Ã(±âŸ)</font></td> </tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">Áú¹®°ú´äº¯</font></td> </tr> 
            <tr><td width="120" bgcolor="#c3f298"><p> <font size="2">´Ù¸¥°÷</font></td> </tr> 
          </table> 
       <td valign="top" align="center" width = "480">  
	

main_htm/bottom.htm
       </td> 
     </tr> 
   <tr> 
     <td colspan="2" align="center" valign="top">Copyright ¨Ï 1999 nalabi.Mail to<a href="mailto:nalabi@hananet.net">webmaster</a></td> 
   </tr> 
  </table> 
</body> 
</html>

  

6.2. À̸§À» ÀÔ·Â¹Þ¾Æ mysql¿¡ ÀúÀåÇÏ°í Ãâ·ÂÇϱâ

  1. Å×À̺í ÀÛ¼º (mysql DBNAME < test.tbl)
    create table test ( 
      no int default '0' not null auto_increment, 
      name char(10), 
      regdate date, 
      PRIMARY KEY(no) 
    );
           

  2. name_input.php ÀÛ¼º
    <html> 
    <body> 
    
    <form name="form" method="get" action="write_db.php3"> 
    <inputtype="text" name="name" size="10"> 
    <input type="submit" name="ok" value="º¸³»±â">
    </form> 
    
    </body>
    </html>
           

  3. write_db.php ÀÛ¼º
    <? 
    // MySQL ¼­¹ö¿¡ ¿¬°áÇÑ´Ù 
    $connect=mysql_connect( "localhost", "root", "") or die( "SQL server¿¡ ¿¬°á ÇÒ¼ö ¾ø½À´Ï´Ù."); 
    
    $tbl_list = mysql_list_tables("kedu"); //µ¥ÀÌŸ º£À̽º¸¦ ¼³Á¤ÇÕ´Ï´Ù. 
       
    $result = mysql_query("insert into test (name) values ('$name')"); 
    $result=mysql_query($result, $connect ); 
    
    $tbl_name="select * from test" ; 
    $result=mysql_query($tbl_name, $connect ); 
    
    $print=mysql_fetch_array($result); //Å×À̺íÀÇ ³»¿ëÀ» °¡Á®¿È 
       
    $rows = mysql_num_rows($result); 
    
    echo("ÀÔ·Â µÇ¾ú½À´Ï´Ù..<br>"); 
    
    for($i=0; $i< $rows;$i++) 
    { 
      echo("$print[name]<br>"); 
      $print=mysql_fetch_array($result); 
    } 
    ?> 
           

6.3. À̸§À» ÀÔ·Â¹Þ¾Æ Ã¼Å©ÇÏ°í mysql¿¡ ÀúÀåÇÏ°í Ãâ·ÂÇϱâ

  1. Å×À̺í ÀÛ¼º ( mysql DBNAME < test.tbl )
    create table test ( 
      no int default '0' not null auto_increment, 
      id char(10), 
      name char(10), 
      pw char(8), 
      regdate date, 
      regtime time, 
      PRIMARY KEY(no) 
    ); 
           

  2. check_input.php ÀÛ¼º
    <html> 
    <body> 
    
    <table> 
    <form name="form" method="post" action=access_db.php3> 
        
       <tr> 
         <td>¾ÆÀ̵ð </td> 
         <td><INPUT name=id size=10> <br></td></tr> 
       <tr> 
         <td>ÀÌ ¸§ </td> 
         <td><INPUT name=name size=10> <br></td></tr> 
       <tr> 
         <td>ºñ¹Ð¹øÈ£</td> 
         <td><INPUT name=pw size=10 type=password> <br></td></tr> 
       <tr> 
         <td>ºñ¹Ð¹øȣȮÀÎ</td> 
         <td><INPUT name=pw2 size=10 type=password> </td></tr></table> 
    
       <input type="submit" value="º¸³»±â"> 
    
    </form> 
    </table> 
    
    </body> 
    </html> 
           

  3. access_db.php ÀÛ¼º
    <? 
    // MySQL ¼­¹ö¿¡ ¿¬°áÇÑ´Ù 
    $connect=mysql_connect( "localhost", "root", "") or die( "SQL server¿¡ ¿¬°á ÇÒ¼ö ¾ø½À´Ï´Ù."); 
    $tbl_list= mysql_list_tables("kedu"); //µ¥ÀÌŸ º£À̽º¸¦ ¼³Á¤ÇÕ´Ï´Ù. 
    
    if($id == "") { 
      echo "¾ÆÀ̵𸦠ÀÔ·Â ÇÏÁö ¾Ê¾Ò½À´Ï´Ù."; 
    } 
    elseif($name == ""){ 
      echo "À̸§À» ÀÔ·Â ÇÏÁö ¾Ê¾Ò½À´Ï´Ù."; 
    } 
    elseif($pw == ""){ 
      print "ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇÏÁö¾Ê¾Ò½À´Ï´Ù.<br>"; 
      print "ºñ¹Ð¹øÈ£¸¦ ÀÔ·Â ÇØÁÖ¼¼¿ä.<br>"; 
    } 
    elseif($pw2 == ""){ 
      print "ºñ¹Ð¹øȣȮÀÎÀ» ÀÔ·ÂÇÏÁö ¾Ê¾Ò½À´Ï´Ù.<br>"; 
      print "ºñ¹Ð¹øȣȮÀÎÀ» ÀÔ·Â ÇØÁÖ¼¼¿ä.<br>"; 
    } 
    elseif($pw != $pw2) { 
      print "ºñ¹Ð¹øÈ£°¡ °°Áö ¾Ê½À´Ï´Ù. <br>"; 
      print "ºñ¹Ð¹øÈ£¸¦ ´Ù½Ã ÀÔ·Â ÇØÁÖ¼¼¿ä. <br>"; 
    } 
    else { 
      echo "$id°¡ ÀÔ·Â µÇ¾ú½À´Ï´Ù..<br>"; 
    
      $date = date("y-m-d", time()); 
    
      $result = mysql_query("insert into test values ('','$id','$name','$pw','$date','$time')"); 
      $result=mysql_query($result, $connect); 
    
      $tbl_name="select *from test" ; 
      $result=mysql_query($tbl_name, $connect ); 
    
      $print=mysql_fetch_array($result); //Å×À̺íÀÇ ³»¿ëÀ» °¡Á®¿È 
      
      $rows = mysql_num_rows($result); 
    
      for($i=0; $i< $rows;$i++) 
      { 
        echo("$print[id] $print[name] $print[pw] $print[regdate]<br>"); 
        $print=mysql_fetch_array($result); 
      } 
    } 
    ?> 
           

6.4. Å×À̺í Ãâ·Â ÇÁ·Î±×·¥

Å×À̺í Ãâ·Â ÇÁ·Î±×·¥
<? 
// MySQL ¼­¹ö¿¡ ¿¬°áÇÑ´Ù 
$connect=mysql_connect( "localhost", "username", "") or die( "Unabletoconnect to SQL server"); 
//-------------------------------------------------------------------------- 
// »ç¿ë°¡´ÉÇÑ DB ¸ñ·ÏÀ» °Ë»ö 
//-------------------------------------------------------------------------- 
$dblist = mysql_listdbs(); //µ¥ÀÌŸ º£À̽º ¸ñ·ÏÀ» ÀνÄÇÑ´Ù. 
$dbnum=mysql_num_rows($dblist); //µ¥ÀÌŸ º£À̽º °¹¼ö¸¦ º¯¼ö¿¡ ÀúÀåÇÑ´Ù. 

for($i=0; $i < $dbnum; $i++) //µ¥ÀÌŸ º£À̽º °¹¼ö ¸¸Å­ 
{ 
  $dbname[$i] = mysql_dbname($dblist, $i) ; //µ¥ÀÌŸ º£À̽º ¸ñ·Ï À̸§À» º¯¼ö¿¡ ÀúÀåÇÑ´Ù. 
} 
echo "µ¥ÀÌŸ º£À̽º °Ë»ö ³»¿ë <br>"; 
for($i=0; $i < count($dbname); $i++)
{ //µ¥ÀÌŸ º£À̽º °¹¼ö ¸¸Å­ 
  echo "$dbname[$i] <br>"; // µ¥ÀÌŸ º£À̽º À̸§À» À¥¿¡ ³ªÅ¸³»ÁØ´Ù. 
} 

//-------------------------------------------------------- 
// »ç¿ë°¡´ÉÇÑ Å×ÀÌºí ¸ñ·ÏÀ» °Ë»ö 
//-------------------------------------------------------- 

$tbl_list = mysql_list_tables("board"); //µ¥ÀÌŸ º£À̽º¸¦ ¼³Á¤ÇÕ´Ï´Ù. 
$tbl_num=mysql_num_rows($tbl_list); // Å×ÀÌºí °¹¼ö¸¦ º¯¼ö¿¡ ÀúÀåÇÕ´Ï´Ù. 

echo "<br>µ¥ÀÌŸ Å×ÀÌºí °Ë»ö ³»¿ë--> Å×À̺í¸í,·¹ÄÚµå¼ö<br>"; 
for($i=0; $i < $tbl_num; $i++) //Å×ÀÌºí °¹¼ö ¸¸Å­ 
{ 
  $tbl_name[$i] = mysql_tablename($tbl_list,$i) ; // Å×À̺í À̸§À» º¯¼ö¿¡ ÀúÀå 
  $result[$i] = mysql_query( "SELECT * FROM $tbl_name[$i]"); //Å×À̺íÀÇ ³»¿ëÀ» º¯¼ö¿¡ ÀúÀå 
  $rows[$i] = mysql_num_rows($result[$i]); // Å×À̺íÀÇ ·¹ÄÚµå ¼ö¸¦º¯¼ö¿¡ ÀúÀå 
  echo "$tbl_name[$i], $rows[$i], $name <br>"; //Å×À̺íÀÇ À̸§°ú, ·¹ÄÚµå¼ö¸¦ À¥¿¡ Ãâ·Â ÇØÁÝ´Ï´Ù. 
} 

//-------------------------------------------------------------------- 
// »ç¿ë°¡´ÉÇÑ Å×ÀÌºí ³»¿ëÀ» Ãâ·Â
//-------------------------------------------------------------------- 

$tbl_name="select * from test" ; 

$result=mysql_query($tbl_name, $connect ); 
$print=mysql_fetch_array($result); //Å×À̺íÀÇ ³»¿ëÀ» °¡Á®¿È 

echo("<br>Å×ÀÌºí ³»¿ëÀ» È­¸é¿¡ Ãâ·Â ½ÃÄÑ ÁÝ´Ï´Ù.<br>"); 
echo("À̸§, ³ªÀÌ, ÀüÈ­¹øÈ£ºÎ, ÁÖ¼Ò<br>"); 
echo("$print[name], $print[age], $print[tel], $print[addr]<br>"); 
   

//------------------------------------------------------------------------------- 
// »ç¿ë°¡´ÉÇÑ Å×ÀÌºí ³»¿ëÀ» Àüü Ãâ·Â
//------------------------------------------------------------------------------- 
$que_bbs="select * from test" ; 

$result=mysql_query($que_bbs,$connect ); 
$print=mysql_fetch_array($result); 
$rows = mysql_num_rows($result); 
echo("<br>Å×ÀÌºí ³»¿ë Àüü¸¦ Ãâ·Â ½ÃÄÑ ÁÝ´Ï´Ù.<br>"); 
echo("À̸§, ³ªÀÌ, ÀüÈ­¹øÈ£ºÎ, ÁÖ¼Ò<br>"); 
for($i=0; $i < $rows; $i++) 
{ 
  echo("$print[name], $print[age], $print[tel], $print[addr]<br>"); 
  $print=mysql_fetch_array($result); 
} 
?>
  

6.5. mysql_fetch_array ¿¹Á¦

mysql_free_result ¸¦ Çؼ­ ¸Þ¸ð¸®¸¦ Ç®¾î ÁØ´Ù.
<?

mysql_connect($db_server, $db_user, $db_pass);

$result = mysql_db_query("$db_name","select * from $table_name");

while($row = mysql_fetch_array($result)) 
{
  echo $row["no"];
  echo $row["name"];
  echo " ";
}

mysql_free_result($result);
?>
    

6.6. À̹ÌÁö È­ÀÏ Ãâ·Â

<?
Header("Content-type: image/gif");

$file = "test.gif";

$size = 4096;
if (!($fp = fopen($file, "r"))) 
{ 
  die("could not open input"); 
  exit; 
} 

while ($data = fread($fp,$size)) 
{ 
  print($data); 
}	
?>
    

6.7. ´Þ·Â ¿¹Á¦

´Þ·Â ¿¹Á¦
<?

// º¯¼ö°ª ¼³Á¤
$date=01; 
$day=01; 
$off=0; 

//³âµµ ´Þ ¼³Á¤
$year = '2000'; 
$month = '01'; 


/*  Figure out how many days are in this month  */ 
while (checkdate($month,$date,$year)): 
    $date++; 
endwhile; 

// ¸Þ´º Ç¥½ÃÇϱâ
echo "<table width='420' ><tr><td align = center>$year ³â $month ¿ù</td></tr></table>";
echo "<table border='1'  width='420'><tr>"; 
echo "<td><b><font face='µ¸À½'>¿ù</font></b></td>"; 
echo "<td><b><font face='µ¸À½'>È­</font></b></td>"; 
echo "<td><b><font face='µ¸À½'>¼ö</font></b></td>"; 
echo "<td><b><font face='µ¸À½'>¸ñ</font></b></td>"; 
echo "<td><b><font face='µ¸À½'>±Ý</font></b></td>"; 
echo "<td><b><font face='µ¸À½'>Åä</font></b></td>"; 
echo "<td><b><font face='µ¸À½'>ÀÏ</font></b></td>"; 
echo "<tr>"; 

//·çÇÁ µ¹¸®±â
while ($day<$date): 

/*  Figure what day of the week the first falls on and set the number of preceding and trailing cells accordingly  */ 
if ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) == 'Sunday') 
{ 
    echo "<td>$day</td>"; 
    $off = '01'; 
}  
elseif ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) ==  'Monday') 
{ 
    echo "<td></td><td>$day</td>"; 
    $off= '02'; 
}  
elseif ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) ==  'Tuesday') 
{ 
    echo "<td></td><td></td><td>$day</td>";
    $off= '03'; 
}  
elseif ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) ==  'Wednesday') 
{ 
    echo "<td></td><td></td><td></td><td>$day</td>"; 
    $off= '04'; 
}  
elseif ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) ==  'Thursday') 
{ 
    echo "<td></td><td></td><td></td><td></td><td>$day</td>"; 
    $off= '05'; 
}  
elseif ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) ==  'Friday') 
{ 
    echo "<td></td><td></td><td></td><td></td><td></td><td>$day</td>"; 
    $off= '06'; 
}  
elseif ($day == '01' and date('l', mktime(0,0,0,$month,$day,$year)) ==  'Saturday') 
{ 
    echo "<td></td><td></td><td></td><td></td><td></td><td></td><td>$day</td>"; 
    $off= '07'; 
}  
else 
{ 
    echo "<td>$day</td>"; 
} 

//ÀÏ Áõ°¡ ½ÃÅ°±â
$day++; 
$off++; 

if ($off>7) 
{ 
    echo "</tr><tr>"; 
    $off='01'; 
} else 
{ 
    echo ""; 
} 

endwhile; 

echo "</table>"; 

?>
    

6.8. ¿øÇÏ´Â ¹®ÀÚ¿­ °Ë»ö(ereg)

¾î¶² ¹®ÀÚ¿­ Áß¿¡¼­ 000 À̶ó´Â ¹®ÀÚ¿­ÀÇ ·¹Äڵ带 ã¾Æ ³»±â

Ç¥Çö :

int ereg(string pattern, string string, array [regs]);

»ç¿ë¹ý :

ereg(ãÀ» ÆÐÅÏ, °Ë»çÇÒ ¹®ÀÚ¿­, °á°ú ¹è¿­);

¿¹Á¦ :

ereg("([0]{3})", $hash[code], $regs)

°¢°¢ÀÇ ÆÐÅÏÀº ()·Î Á¤ÀǸ¦ ÇÕ´Ï´Ù.

([0]{3})´Â 0ÀÇ°¹¼ö°¡ 3°³·Î ³ª¿Í¾ßÇÑ´Ù´Â ÀǹÌÀÌ´Ù.

([0]{2, 3}) 0ÀÇ °¹¼ö°¡ 2°³ ȤÀº 3°³¸¦ ÀǹÌ

([0-9]{2, 3}) 0¿¡¼­ 9±îÁöÀÇ ¼ýÀÚ°¡ 2ÀÚ¸® ȤÀº 3ÀÚ¸®¸¦ ÀǹÌ

    
<?

$sth = mysql_query("select * from kangsa_type order by code");

while($hash = mysql_fetch_array($sth)) 
{ 
  if(ereg("([0]{3})", $hash[code], $regs)) 
  {
    print "$hash[typename]";
  }
}

?>
    

6.9. ÀÎÁõ ¿¹Á¦

  1. auth.sql À» ¸ÕÀú ÀÛ¼ºÀ»ÇÑ´Ù.
    # MySQL dump 6.6
    #
    # Host: localhost    Database: myhome
    #--------------------------------------------------------
    # Server version    3.23.8-alpha
    
    #
    # Table structure for table 'auth'
    #
    CREATE TABLE auth (
      id char(10) DEFAULT '' NOT NULL,
      passwd char(10),
      name char(10),
      level int(11),
      PRIMARY KEY (id)
    );
    
    #
    # Dumping data for table 'auth'
    #
    
    INSERT INTO auth VALUES ('nalabi','xxxx','±è¿ëÀÏ',1);
            

  2. auth.ph ÀÎÁõÇÏ´Â ºÎºÐÀ» ÀÛ¼ºÇÑ´Ù.
    <?php 
    
    cfunction authenticate() 
    {
      Header( "HTTP/1.0 401 Unauthorized"); 
      $title= "Invalid Login";
    ?> 
      ¾ÆÀ̵ð¿Í ¾ÏÈ£°¡ ÇÊ¿äÇÕ´Ï´Ù! 
    <?php  exit;
    } 
    
    if(!isset($PHP_AUTH_USER)) 
    { 
      authenticate(); 
    } else 
    { 
      mysql_pconnect( 'localhost', 'root', '') or die( "Unable to connect to SQL server");  // MySQL ¼­¹ö Á¢¼Ó 
            
      mysql_select_db( "test") or die( "Unable to select database");              // DB ¼±Åà 
            
      $result = mysql_query( "select name , level from auth where id='$PHP_AUTH_USER'  and passwd='$PHP_AUTH_PW' ");
            
      if(!mysql_num_rows($result)) 
      {  
        authenticate(); 
      } 
      else  
      { 
        $userinfo = mysql_fetch_array($result); 
      } 
    } 
    ?> 
            

  3. test.php ÇÁ·Î±×·¥ Çϴµ¥¼­ ÀÎÁõÇÏ´Â ºÎºÐÀ» Àû¿ëÇÑ´Ù.
    <?
    
    include ("auth.ph");
    
    echo("¾î¼­ ¿À½Ê½Ã¿ä.");
    
    ?>
            

6.10. Ä¿³Î ¹öÀü ³ªÅ¸³»ÁÖ´Â ÇÔ¼ö

kernel.php
<?
function kernel() 
{
  $label = array("¾ÈÁ¤¹öÀü","°³¹ß¹öÀü","¾ËÆĹöÀü");
  $cmd = "/usr/bin/finger @ftp.kernel.org| cut -c59-66 | sed -e '/^$/d'";
  exec($cmd,$array,$rtn);

  for($i=0; $i < count($array); $i++) 
  {
    list($v1, $v2, $v3)  = split('[/.-]' , $array[$i]);
    $version = "v".$v1.".".$v2."/linux-".$array[$i].".tar.gz";
		
    $label[$i] = "<a href = ftp://ftp.kernel.org/pub/linux/kernel/".$version.">".$label[$i]." : ".$array[$i]."</a>";
  }

  return $label;
}

$label = kernel();

echo("$label[0] <br> $label[1] <br> $label[2]");
	
?>
    

6.11. µµ¸ÞÀΠǥ½Ã ÇÔ¼ö

whois ÇÁ·Î±×·¥ÀÌ ±ò·Á ÀÖ¾î¾ß ÇÕ´Ï´Ù.

rpm -qa | grep whois
 
    whois-1.0.10-1
    

ÇÁ·Î±×·¥
 
<?
function domain($string) 
{
  $cmd = "whois $string";
  exec($cmd,$array,$rtn);

  for ($i=0; $i < count($array); $i++) 
  { 
    $rstring .= "$array[$i]";
  }
  return $rstring;
}

print domain("formail.org");

?>
    

6.12. ¹®ÀÚ¿­ üũ ÇÔ¼ö ¿¹Á¦(ereg)

¹®ÀÚ¿­¿¡ ¿µ¾î ´ë¼Ò¹®ÀÚ, ¼ýÀÚ¸¸ ¿Àµµ·Ï ÇÏ°Ô ÇÏ´Â ÇÔ¼öÀÔ´Ï´Ù
<?
function check_id($string)
{
  $error_msg = "error";

  if (ereg("[^a-zA-Z0-9]", $string)) 
  {
    $error_msg = $error_msg;
    return $error_msg;
  }
  else 
  {
    return $string;
  }
}

$id1 = "abcd";
$id2 = "ZabCA";
$id3 = "9Zab01";
$id4 = "#ab.c.%";
$id5 = "ÇѱÛ";

$msg = check_id($id1);
print "id1 : $msg";

$msg = check_id($id2);
print "id2 : $msg";

$msg = check_id($id3);
print "id3 : $msg";

$msg = check_id($id4);
print "id4 : $msg";

$msg = check_id($id5);
print "id5 : $msg";
?>
    

6.13. IDüũ ÇØÁÖ´Â ÇÁ·Î±×·¥

  1. sql ¹®
    # MySQL dump 7.2
    #
    # Host: localhost    Database: test
    #--------------------------------------------------------
    # Server version	3.23.11-alpha
    
    #
    # Table structure for table 'login3'
    #
    CREATE TABLE login3 (
      nick char(10)
    );
    
    #
    # Dumping data for table 'login3'
    #
    
    INSERT INTO login3 VALUES ('³ª¶óºñ');
            

  2. test.php
    <?
    function add_error($why, $reason, $nick) 
    {
      print "$reason<br>";
    }
    
    $nick= "³ª¶óºñ";
    
    if (ereg ("[~!@#$%^&*()~|"/'+=-_:;,.<>]", $nick)) 
    { 
      $why="´Ð³×ÀÓ"; 
      $reason="´Ð³×ÀÓ¿¡ Ư¼ö¹®ÀÚ°¡ ÀÖ½À´Ï´Ù."; 
      add_error($why,$reason,$nick); 
      exit; 
    } 
                         
    if(ereg(" ", $nick)) 
    { 
      $why="´Ð³×ÀÓ"; 
      $reason="´Ð³×ÀÓ¿¡ °ø¹éÀÌ ÀÖ½À´Ï´Ù."; 
      add_error($why,$reason,$nick); 
      exit; 
    } 
                         
    if($nick=="") 
    { 
      $head = "´Ð³×ÀÓ"; 
      $mesg = "´Ð³×ÀÓÀÌ ¾ø½À´Ï´Ù."; 
      add_error($head,$mesg,$nick); 
    } 
    
    if(strlen($nick) < 2) 
    { 
      $head = "´Ð³×ÀÓ"; 
      $mesg = "´Ð³×ÀÓÀº Ư¼ö¹®ÀÚ¸¦ Á¦¿ÜÇÑ 2ÀÚÀÌ»óÀ¸·Î ³Ö¾îÁÖ¼¼¿ä."; 
      add_error($head,$mesg,$nick); 
    } 
    
    $server = "localhost";
    $name = "root";
    $passwd = "";
    $dbname = "test";
    
    mysql_connect($server, $name, $passwd);
    mysql_select_db($dbname);
    
    
    $que1 = "select nick from login3 where nick='$nick'"; 
    $result= mysql_query($que1); 
    $current_row = mysql_fetch_row($result); 
                         
    if($current_row[0] == $nick) 
    { 
      $head = "´Ð³×ÀÓ"; 
      $reason = $nick."´ÔÀÌ  ÀÌ¹Ì µî·ÏµÇ¾î ÀÖ½À´Ï´Ù."; 
      add_error($why,$reason,$nick); 
    } 
    else 
    {
    	print $nick;
    }
    ?>
            

6.14. °Ô½Ã¹° ÆäÀÌÁö º¯°æ ¿¹Á¦

  1. sql ¹®
    # MySQL dump 6.4
    #
    # Host: localhost    Database: test
    #--------------------------------------------------------
    # Server version	3.23.5-alpha
    
    #
    # Table structure for table 'login'
    #
    CREATE TABLE login (
      id char(10) DEFAULT '' NOT NULL,
      reg_date char(10),
      nick char(10),
      passwd char(10),
      name char(10),
      user_num int(11),
      address char(10),
      phone char(10),
      email char(10),
      job char(10),
      PRIMARY KEY (id)
    );
    
    #
    # Dumping data for table 'login'
    #
    
    INSERT INTO login VALUES ('nalab1','','aaaa','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab2','','bbbb','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab3','','cccc','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab4','','dddd','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab5','','eeee','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab6','','ffff','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab7','','gggg','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab8','','hhhh','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nalab9','','iiii','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nala10','','jjjj','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nala11','','kkkk','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nala12','','llll','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nala13','','mmmm','bbbb','1111',0,'1','cccc','dddd','eeee');
    INSERT INTO login VALUES ('nala14','','nnnn','bbbb','1111',0,'1','cccc','dddd','eeee');
            

  2. test.php
    <?
    
    $server = "localhost";
    $name = "root";
    $passwd = "";
    $dbname = "test";
    
    mysql_connect($server, $name, $passwd);
    mysql_select_db($dbname);
    
    
    $page_scale=3; //ÇÑÆäÁã´ç º¸¿©ÁÙ ¼ýÀÚ ¹øÈ£[1][2][3] 
    $scale=2; // ÇÑÆäÀÌÁã ´ç º¸¿©ÁÙ ¶óÀÎ °³¼ö(·¹ÄÚµå)  
        
    if(!$star){ $star= 0 ;}  // ½ÃÀÛÇÒ ·¹ÄÚµå ¹øÈ£ - 1
        
    $page=floor($star/($scale*$page_scale));  // ÆäÀÌÁö ¼ö ¾ò±â
    
    $que1="select DATE_FORMAT(reg_date,'Y.m.d'), id,nick, passwd ,name , user_num, address, phone,  email, job from login order by reg_date DESC " ; 
    
    $result = mysql_query($que1);
        
    $total = mysql_affected_rows(); //°Ô½Ã¹° ÃÑ ¼ö
        
    $last=floor($total/$scale); //¸¶Áö¸· ¹øÈ£
        
    echo "<font size=-1>Àüü µî·ÏÀÚ : $total ¸í ÀÔ´Ï´Ù."; 
        
    echo "<html><title>ȸ¿ø°ü¸®</title><body bgcolor=ffffff><center><font size=4>ȸ¿ø°ü¸® ÆäÀÌÁö</font>
            <table border=1><tr bgcolor=336699>
              <td align=center><font color=ffffff size=-1>µî·ÏÀÏ</font><td align=center><font color=ffffff size=-1>ID</font></td>
              <td align=center><font color=ffffff size=-1>´Ð³×ÀÓ</font></td>
              <td align=center><font color=ffffff size=-1>ºñ¹Ð¹øÈ£</font></td>
              <td align=center><font color=ffffff size=-1>À̸§</font></td>
              <td align=center><font color=ffffff size=-1>Áֹεî·Ï¹øÈ£</font></td>
              <td align=center><font color=ffffff size=-1>ÁÖ¼Ò</font></td>
              <td align=center><font color=ffffff size=-1>¿¬¶ôó</font></td>
              <td align=center><font color=ffffff size=-1>E-Mail</font></td>
              <td align=center><font color=ffffff size=-1>Á÷¾÷</font></td>
              <td align=center><font color=ffffff size=-1>°ü¸®</font></td></tr>";
    
    if($star) 
    {  
      $n=$total-$star;  
    }
    else  
    {      
      $n=$total;  
    }
    
    for($i=$star ; $i< $star+$scale ; $i++)  //  star ¿¡¼­ scale ±îÁö ¸¸
    {
      if($i< $total ) // Àüü ÀÚ·á °³¼ö±îÁö¸¸ Ãâ·Â 
      { 
        mysql_data_seek($result,$i);
        $row=mysql_fetch_array($result);
                 
        $imsi=$row[5];
                 
        echo"<tr><td align=center><font size=-1>$row[0]</font></td>
               <td align=center><font size=-1>$row[1]</font></td>
               <td align=center><font size=-1>$row[2]</font></td>
               <td align=center><font size=-1>$row[3]</font></td>
               <td align=center><font size=-1>$row[4]</font></td>
               <td align=center><font size=-1>$row[5]</font></td>
               <td align=center><font size=-1>$row[6]</font></td>
               <td align=center><font size=-1>$row[7]</font></td>
               <td align=center><font size=-1><a href=mailto:$row[8]>$row[8]</font></a></td>
               <td align=center><font size=-1>$row[9]</font></td>
               <td align=center><font size=-1><a href=$admin_url?action=erase&num=$row[5]&start=$start>»èÁ¦</a></font></td></tr> ";
      }
            
      $n--;
    }
        
    echo "</table><br><br>";
         
    if($total>$scale)  
    {
      if($star+1>$scale*$page_scale)   
      {
        $pre_star=$star-$scale;
        echo"<a href='$PHP_SELF?star=$pre_star'> ÀÌÀü</a>";
      }
              
      for($vj=0; $vj < $page_scale ; $vj++) 
      {
        $ln=($page*$page_scale+$vj)*$scale;
                  
        $vk=$page*$page_scale+$vj+1;
                  
        if($ln < $total) 
        {
          if($ln!=$star)   
          {
            echo"<a href='$PHP_SELF?star=$ln'><font size=2>[$vk]</a></font>";
          }
          else    
          {
            echo"<font size-2><b>$vk</b></font>";
          }
        }
      }
              
      if($total>(($page+1)*$scale*$page_scale)) 
      {
        $n_star=($page+1)*$scale*$page_scale;
                  
        echo"<a href='$PHP_SELF?star=$n_star'>´ÙÀ½</a>";
      }
    }
                    
    ?>
    
            

6.15. Á¤±ÔÇ¥Çö½Ä

  1. Á¤±Ô½Ä Ç¥Çö
    a(b*) : a, ab, abb, a¿Í ÀÓÀÇÀÇ °¹¼öµé·Î µÈ ¹®ÀÚ¿­
    a(b+) : ab, abb, abbb, ab¿Í ÀÓÀÇÀÇ °¹¼öµé·Î µÈ ¹®ÀÚ¿­
    a(b?) : a or ab, a ´ÙÀ½¿¡ b°¡ À־ µÇ°í, ¾ø¾îµµ µÈ´Ù.
    a(b{3}) : abbb 
    a(b{2,}) : abb, abbb, abbbb, a ¿Í µÎ°³ ÀÌ»óÀÇ bµé·Î µÈ ¹®ÀÚ¿­
    a(b{2.4}) : abb, abbb, abbbb, a ¿Í µÎ°³ ÀÌ»ó, ³× °³ ÀÌÇÏÀÇ bµé·Î µÈ ¹®ÀÚ¿­
    
    [a-z] : a-z ±îÁö ¹®ÀÚ¿­
    [a-zA-Z0-9] : ÀÓÀÇ ¿µ¼ýÀÚ
            

  2. °ø¹é ¹®ÀÚ
    [^0-9] : ÀÓÀÇ ¼ýÀÚ°¡ ¾Æ´Ñ¹®ÀÚ
    [~a-zA-Z0-9]: ÀÓÀÌÀÇ ¿µ¼ýÀÚ°¡ ¾Æ´Ñ ¹®ÀÚ
            

  3. ÀÓÀÇÀÇ °ø¹é¹®ÀÚ°¡ ¾Æ´Ñ ¹®ÀÚ
    a.c : aac, abc, acc, a ¿Í c¿¡ ÀÓÀÇÀÇ ¹®ÀÚ Çϳª°¡ ³¢¾î ÀÖ´Â ¹®ÀÚ¿­
    ^a.* : a·Î ½ÃÀÛÇÏ´Â ÀÓÀÇÀÇ ¹®ÀÚ¿­
    [a-c]*x$ : x, ax, bx, abax, abcx, a¿¡¼­ c»çÀÌÀÇ ÀÓÀÇÀÇ ¹®ÀÚµé·ÎÀÌ·ç¾îÁö¸ç, ¸¶Áö¸· ¹®ÀÚ°¡ xÀÎ ¹®ÀÚ¿­
    b[ao]y : bay ¶Ç´Â boy¿¡ÀÎ ¹®ÀÚ¿­
    [^Zz]{5} : Z or z °¡ Æ÷ÇÔµÇÁö ¾ÊÀº ±æÀÌ°¡ 5ÀÎ ÀÓÀÇÀÇ ¹®ÀÚ¿­
    [[:digit:]] : ÀÓÀÇÀÇ ¼ýÀÚ , [0-9]
    [[:<:]a.* : a·Î ½ÃÀÛÇÏ´Â ÀÓÀÇÀÇ ¹®ÀÚ¿­
    
    ¿¹) ^([0-9]{2,4})(-[0-9]{3,4})(-[0-9]{3,4}) : ÀüÈ­¹øÈ£(xxxx-xxxx-xxxx)
            
    ÀÓÀÇÀÇ °ø¹é¹®ÀÚ°¡ ¾Æ´Ñ ¹®ÀÚ

6.16. ¿µ¾î ´Ü¾î °Ë»ö

  1. engdic ÇÁ·Î±×·¥ÀÌÀÖ³ª È®ÀÎÇÏ°í ¾øÀ¸¸é ¼³Ä¡ÇÑ´Ù.

    which engdic

  2. test.php, exec ¸¦ »ç¿ëÇؼ­ engdic ÇÁ·Î±×·¥À» ½ÇÇàÇÑ´Ù.
    <?
    
    // ¿µÇÑ/ »çÀü ÀÔ´Ï´Ù.
    function engdic($string) 
    {
      $cmd = "engdic $string";
      exec($cmd,$array,$rtn);
    
      for ($i=0; $i < count($array); $i++) 
      {
        $rstring .= "$array[$i]";
      }
      return $rstring;
    }
    
    print engdic("hello");
    
    ?>
            

6.17. IP ¼Ò¼Ó±â°ü,ȸ»ç ¾Ë¾Æº¸±â

  1. fwhois ÇÁ·Î±×·¥ÀÌÀÖ³ª È®ÀÎÇÏ°í ¾øÀ¸¸é ¼³Ä¡ÇÑ´Ù.

    which fwhois

  2. test.php, exec ¸¦ »ç¿ëÇؼ­ engdic ÇÁ·Î±×·¥À» ½ÇÇàÇÑ´Ù.
    <?
    
    //¾ÆÀÌÇÇ ¼Ò¼Ó ȸ»ç, ±â°ü ¾Ë¾Æ³»´Â ÇÔ¼ö
    function ip($string) 
    {
     $cmd = "fwhois $string"."@whois.nic.or.kr";
     exec($cmd,$array,$rtn);
    
     for ($i=0; $i < count($array); $i++) 
     {
       $rstring .= "$array[$i]";
      }
      return $rstring;
    }
    
    print ip("210.180.116.238");
    
    ?>
            

6.18. ÇöÀçÀÇ ³â/¿ù/ÀÏ ±¸Çϱâ

<?
function date_selector($name, $date) 
{
  $monthname = array(1=>"1¿ù", "2¿ù", "3¿ù", "4¿ù","5¿ù","6¿ù","7¿ù","8¿ù","9¿ù","10¿ù", "11¿ù","12¿ù");

  if($date == "") 
  {
    $date = time();
  }

  //³â
  print "<select name = ".$year." year > ";

  $start_year = date("Y", $date);

  for($current_year = $start_year - 5; $current_year <= $start_year + 5; $current_year++) 
  {
    print "<option value = $current_year";

    if(date("Y", $date) == $current_year) 
    {
      print " selected ";
    }

    print " > $current_year ";
  }
	
  print "</select>";
	
  //¿ù
  print "<select name = ".$month." month >";
  for($current_month = 1; $current_month <= 12; $current_month++) 
  {
    print "<option value = $current_month ";
		
    if(date("m", $date) == $current_month) 
    {
      print " selected ";
      print ">";
      print $monthname[$current_month];
			
    }
    else 
    {
      print ">";
      print $monthname[$current_month];
    }
		
    print " ";
  }

  print "</select>";

  //ÀÏ
  print "<select name = ".$day." day >";
	
  for($current_day = 1; $current_day <= 31; $current_day++) 
  {
    print "<option value = $current_day ";
    if(date("d", $date) == $current_day ) 
    {
      echo " selected ";
    }
    echo " > $current_day ÀÏ";
  }
	
  print "</select>";
}

echo "<html>
<head>
<title>¿¬½À</title>
<body>";

if(isset($month)) 
{
  //$use_date = mktime(0, 0, 0, $sample_month, $sample_day, $sample_year);
  $use_date = mktime(0, 0, 0, $month, $day, $year);
}
else 
{
	$use_date = time();
}

print "<form action = cal.php method = post>";
print date_selector("cal", $use_date);

echo "<input type = submit value = ÀÔ·Â>
</form>
</body>
</html>";

?>