Web Servers Web Clients ftp Web Browser Database
웹 서버-클라이언트 연결 구조 Web Servers Web Clients 웹 서버 프로그램 웹 문서, 서버 프로그 램 파일 전송 (ftp 등) Web Browser Database PHP, Python JSP 웹 문서 HTML 5, CSS Java. Script, XML HTML 5, CSS, Java. Script HTTP TCP/IP 웹 문서 URL (주소), 폼 입력 데이터 5 TCP/IP
■ PHP 특징 l 윈도우, 유닉스, 리눅스 OS에 아파치 웹 서버를 설치하여 사용 l 다양한 데이터베이스에 대해 사용하기 간편한 인터페이스 제공 l LAMP(Linux Apache My. SQL PHP) l WAMP(Windows Apache My. SQL PHP) <!DOCTYPE HTML> <html> <body> <h 2> PHP 예제 </h 2> <? php print "<p> Hello PHP! </p>"; print "<p> Welcome to HTML 5 Web Programming 입문 </p>"; ? > <body> </body> <h 2> PHP 예제 </h 2> </html> <p> Hello PHP! </p> <p> Welcome to HTML 5 Web Programming 입문 </p> </body> 8
PHP 시작하기: 함수, 변수 ■ 함수 ■ 변수: $변수명 <html> $week_day = <body> “Wed” <p>PHP 요일 확인 예제</p> <? php $week_day = date("D"); print "<strong> Today is $week_day. . </strong><br/>"; if ($week_day == "Fri") print "[$week_day] Have a nice weekend!"; elseif ($week_day == "Sun") print "[$week_day] Have a nice Sunday!"; else print "[$week_day] Have a nice day!"; <body> ? > <p>PHP 요일 확인 예제</p> </body> <strong> Today is $week_day. . </strong><br/> </html> [Wed] Have a nice day! </body> 9
PHP로 폼 다루기(1) ■ <form> 에서 action, method 속성 l 예, <form action="formexample. php" method="GET"> l 변수명은 name 속성값 GET 메소드의 경우 : $_GET["변수명"] POST 메소드의 경우 : $_POST["변수명"] <body> http: //webclass. me/html 5_2 e/appendi <p> form으로 PHP 프로그램 실행</p> x/formexample. php? kind=search&key <form action="formexample. php" method="GET"> word=%EB%B 0%95%EC%A 7%80% 유형 선택 : EC. . . <input type="radio" name="kind" value="news">구글 뉴스 <input type="radio" name="kind" value="blog">네이버 블로그 <input type="radio" name="kind" value="search">네이버 검색<br/> 키워드 입력 : <input type="text" name="keyword" size="15">=> <input type="submit" value="PHP 실행"> </form> </body> 11
<? php $k = $_GET["kind"]; $w = $_GET["keyword"]; ? > <p> form으로 데이터 전송 결과</p> <p> 기사 유형(kind): <? php print $k; ? > </p> <p> 키워드(keyword)는 [ <? php print $w; ? > ] 입니다. </p> <? php if ($k=="news") $result_url = "'http: //www. google. co. kr/search? tbm=nws&q=$w'"; elseif ($k=="blog") $result_url = "'http: //section. blog. naver. com/sub/Search. Blog. nhn? option. keyword=$w'"; elseif ($k=="search") $result_url = "'http: //search. naver. com/search. naver? query=$w'"; ? > <script type="text/javascript"> window. open('http: //search. naver. com/search. naver? query=$ window. open(<? php print $result_url; ? >); w'"); </script> 12
PHP로 폼 다루기(2) <h 3>도서 대출 신청 양식</h 3> <form action="formexample 2. php" method="GET"> 대출 도서관 선택: <label> <input type="checkbox" name="seoul">서울도서관</label> <input type="checkbox" name="cheonan">천안도서관</label> <input type="checkbox" name="jeju">제주도서관</label> 대출 기간 선택: <select name="duration"> <option value="1 week"> 1주 </option> <option value="2 week"> 2주 </option> <option value="4 week"> 4주 </option> <option value="8 week"> 8주 </option> </select> 대출 시작 날짜: <input type="date" name="date"/> 대출 시작 시간: <input type="time" name="time"/> http: //webclass. me/. . . /appendix/formexample 2. ph 포장 색상 선택: p? <input type="color" name="color"/> seoul=on&cheonan=on&duration=4 week&date=2 <input type="submit" value="신청하기"> 017 -04 -19&time=14%3 A 00&color=%23 ff 8080 <input type="reset" value="리셋"> </form> 13
PHP에서 My. SQL 실행하기 1. My. SQL 데이터베이스 연결 l $db = new mysqli( 서버이름, 사용자계정이름, 비밀번호, DB이름 ); 2. 질의(query) 요청 l $result = $db->query( SQL질의 ); ■ 예, 선호도 조사 <p> 음식 선호도 조사</div> <form method="get" action="favorate_food. php"> <p> 이름을 입력해 주세요. </p> <input type="text" name="name"> <p> 좋아하는 음식을 선택해 주세요. </p> <input type="radio" name="food" value="korean">한식 <input type="radio" name="food" value="chinese">중식 <input type="radio" name="food" value="japanese">일식 <br/> <input type="reset" value="초기화"/> <input type="submit" value="투표하기"/> </form> 17
<? php $db = new mysqli("localhost", "webclass 1234", "favorate_food"); // 데이터베이스 연결 if (mysqli_connect_errno()) { print "Error: Could not connect to database server. "; exit; id name food 정수 문자열 } $name = $_GET["name"]; $food = $_GET["food"]; $q = "select * from vote where name = $name'"; $result = $db->query($q); if ($result->num_rows > 0) { // already voted, 입력한 이름으로 이미 투표했는지 확인 print "$name has already voted!"; } else { $q = "insert into vote (name, food) values ('$name', '$food')"; // 선호도 투표 결과 저장 $db->query($q); print "$name voted for $food"; print "<p> 음식 선호도 조사 결과 </p>"; $result = $db->query("select * from vote"); print "<br/> 총 투표자 수: ". $result->num_rows; $result = $db->query("select * from vote where food = 'korean'"); print "<br/> 한식: ". $result->num_rows; // 음식별 투표 결과 출력 $result = $db->query("select * from vote where food = 'chinese'"); print "<br/> 중식: ". $result->num_rows; … } ? > 18
- Slides: 18