IP 주소와 서브넷마스크로 네트워크 주소를 구하는 간단한 PHP 프로그램을 작성해 보았다.
IP 주소 네 옥텟과 서브넷마스크 네 옥텟을 입력한 후 확인 버튼을 누르면 네트워크 주소가 출력된다.
HTML 코드는 다음과 같다.
<html>
<head>
<title>네트워크 주소 구하기</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<form method="post" action="netmask.php">
<!--IP 주소 입력-->
IP : <input type="text" maxlength="3" name="ip1">.<input type="text" maxlength="3" name="ip2">.<input type="text"
maxlength="3" name="ip3">.<input type="text" maxlength="3" name="ip4"><br>
<!--서브넷마스크 입력-->
서브넷마스크 : <input type="text" maxlength="3" name="nm1">.<input type="text" maxlength="3" name="nm2">.<input
type="text" maxlength="3" name="nm3">.<input type="text" maxlength="3" name="nm4"><br>
<!--버튼들-->
<input type="submit" name="확인" value="확인">
<input type="reset" name="취소" value="취소"><br>
</form>
</html>
Form을 이용해 IP 주소와 서브넷마스크 인풋을 받는다.
PHP 코드는 다음과 같다.
<html>
<head>
<title>네트워크 주소 구하기</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<?
// IP 옥텟 4개
$ip1 = $_POST["ip1"];
$ip2 = $_POST["ip2"];
$ip3 = $_POST["ip3"];
$ip4 = $_POST["ip4"];
// 서브넷마스크 옥텟 4개
$nm1 = $_POST["nm1"];
$nm2 = $_POST["nm2"];
$nm3 = $_POST["nm3"];
$nm4 = $_POST["nm4"];
// 네트워크 주소 옥텟 4개
$ad1 = (int) $ip1 & (int) $nm1; // (int): 문자열을 정수로 변환
$ad2 = (int) $ip2 & (int) $nm2;
$ad3 = (int) $ip3 & (int) $nm3;
$ad4 = (int) $ip4 & (int) $nm4;
echo ("네트워크 주소: $ad1 . $ad2 . $ad3 . $ad4");
?>
</html>
IP 주소와 서브넷마스크를 & 연산 시켜 네트워크 주소를 구한다.
'프로그래밍' 카테고리의 다른 글
Java 상속에서 생성자 (1) | 2025.04.18 |
---|---|
Java의 static 변수 (0) | 2025.04.11 |
PHP로 Vigenère 치환을 하는 웹 애플리케이션 만들기 (0) | 2025.03.17 |
대각선 모양으로 별 출력하는 PHP 코드 작성하기 (0) | 2025.03.01 |
Python으로 네트워크 ID, 브로드캐스트 주소, 호스트 수 구하기 (Subnet masking) (20250109 7일차) (0) | 2025.01.09 |