420 likes | 574 Views
-- PHP, Web Board – Lecturer : 김한규. CC510 Web Programming Ⅱ. <result source >. What is PHP?. Connect to site. PHP program execute. Web Server. PHP application program. Web browser in PC. Transfer result by html. Transfer html to browser. html. * PHP code “ php test.php ”
E N D
-- PHP, Web Board – Lecturer : 김한규 CC510Web Programming Ⅱ
<result source > What is PHP? Connect to site PHP program execute Web Server PHP application program Web browser in PC Transfer result by html Transfer html to browser html * PHP code “php test.php” <?echo"첫번째 프로그램";echo"아싸~";echo"날짜 : ".date("Y-m-d");?>
Server side script language • Execute in server and include html • When you change .html file to .php, you can also use it • Process automatically and represent the result • Need not change by user • E.g.) in previous page, without php, user need to change date everyday • Developed by Rasmus Lerdorf in 1994
PHP in HTML example • In ex1.php • -line1~5, 7~8 : recognize as html • Line 6 : execute as php mode
What can we do with PHP? • Can make web application • Support variable Databases and make web page using database • Adabas D InterBase Solid dBase mSQL Sybase • Empress MySQL Velocis FilePro Oracle Unix dbm • InformixPostgreSQL
PHP grammar • php in html file • <? Echo (“very simple \n”); ?> • <?php echo(“ ….. \n”); ?> • php must have ‘;’ at the end of the line ex) <? echo “this is test.”; ?> (O) <? echo “if there is no semicolon” echo “error!”; ?> (X) – in one line, there must be 1 command <? echo “if there is no semicolon”; echo “error!”; ?> (O)
PHP grammar • Comments • Basically same as in C and perl • //, # , /* */ e.g.) <? echo “today <BR>"; // 2005-03-28 ?> <? echo “it’s warm<BR>"; # 한줄 주석 ^^ ?> <? echo “happy day~~! ^^<BR>";/* echo "아 여기부터는 주석처리되기 때문에 ";echo "결과에는 반영이 안되겠군요.. ㅜㅜ<BR>"; */ ?> • Comments in comments are not allowed e.g.) /* ~~~ /* ~~~ */ ~~~ */ (X)
PHP grammar • Error message • Set in “C:\Program Files\PHP\php.ini” Display_errors = Off Display_errors = ON Save this file and Restart apache server!! Edit like this!!
PHP variables ex4.php) <?php $str = "문자열"; $str = $str . " 좀더 붙여서.."; $str .= " 나도 끼워줘~\n"; echo $str . "<BR>"; $num = 9; $str = "Number: $num"; echo $str . "<BR>"; $num = 9; $str = 'Number: $num'; echo $str . "<BR>"; ?> • Variable starts with $ • Php variables consist of alphabet, number (case sensitive) • After $, there must be alphabet e.g.) $babo, $php4, $in_out_result (O) $babo?, $4abc, $in-out (X) • Integer : $a = 1234; $a = -123; • Doubles : $a = 1.234; $a = 1.2e3; • String : “string” • “\n” : new line, “\\” : back slash (\) • “\t” : tab, “\”” : “
PHP operator • <PHP operator> • 산술 : +, -, *, /, % • 대입 : = • 비교 : <, >, <=, >=, ==, != • 증감 : ++, -- • 논리 : and, or, xor, !, &&, || • 문자열 : . • 배열 : + • 비트 : &, |, ^, ~, <<, >> <?echo"<h4>a++</h4>";$a=5;echo" \$a++: ".$a++."<br>\n";echo" \$a : ".$a."<br>\n";echo"<h4>++a</h4>";$a=5;echo" ++\$a: ".++$a."<br>\n";echo" \$a : ".$a."<br>\n";echo"<h4>=</h4>";$a=5; $b=$a; echo" \$a : ".$a."<br>\n";echo" \$b : ".$b."<br>\n";echo" \$a+$b : ".$a+$b."<br>\n"; echo"<h4>.</h4>";$a= ”hi”; $b = “!~~”; $c = $a.$b; $d= $c. “how are you”; echo" \$c: ".$c."<br>\n";echo" \$c+ : ".$d."<br>\n";?> Example
PHP array • $ArrayName[Index] = value • $ArrayName = array(value1, value2, …, valueN) • Index • Scalar array : starts with 0 • Association array : string Example
PHP – control statements • If <? $a = 1; $b = 0; if ( $a > $b ) { //if $a>$b , execute next echo “a is bigger than b.”; }elseif ($a == $b) { // if $a>$b is false and $a==$b, execute next echo “a is equal to b.”; } else { // if $a<$b , execute next echo “a is smaller than b.”; } ?>
PHP – control statements switch (variable) { case value1: statement1 break; case value2: statement2 break; default : statement3 } If variable is value1, then run statement until when it meet ‘break’
PHP – control statements • while (a) { b } • while a is true, execute b • do { a } while (b) • similar as while • run a and while b is true, execute a <?$i=1;while($i<=10){echo$i++;//$i }?>
PHP – control statements • for(expr1; expr2; expr3) statements • Execute expr1 • If expr2 is true, run statements • After run all statements, execute expr3 e.g.) for ($i=1; $i <=10; $i++) { echo $i; }
PHP – control statements • break • Escape from for or while loop <? for ($i = 1;$i<20;$i++) { if ($i >10 ) { break; // escape from loop } echo $i; } ?> • continue • Go back to the loop start <? for($num=1;$num<10;$num++) { if ($num==5) { continue; } echo $num; } ?>
PHP – include • include(‘somefile.html’); • Somefile.html substitutes for the include statements
PHP - require • require(‘somefile.html’); • the function is similar to the function of include • The difference between include and require • When it is in control statements • require execute all the time • Include execute when the statements is true
PHP - form • <form name=“form name” action=“destination’s address” method=“transfer protocol> HTML … </form> • Method • Get : transfer by URL • /index.php?name=brown&homepage=http://… • ?variable_name=value • Post : transfer by HTTP header, more secure than get • <input> ex) <FORM ACTION=FILENAME METHOD=POST> <INPUT TYPE=TEXT> <INPUT TYPE=SUBMIT> </FORM>
PHP - form • METHOD = POST or GET • POST : don’t represent data in URL • Good for security • GET : represent data in URL • Only limited string can transfer • ACTION = “file” • When input data fill, and click, then execute the contents in the file
<FORM> tag Click here!!
<FORM> tag In “C:\Program Files\PHP\php.ini” register_globals = Off
In “C:\Program Files\PHP\php.ini” register_globals = On
PHP function • 반복된 작업이나 복잡한 로직을 하나의 묶음으로 만들어 사용하는 프로그래밍 문법 <?php function functionName($argument1,$argument2,$argument3 ... ) { statements; return returnValue; } ?>
PHP function - date • date() • Return date according to the requested form reference) http://kr.php.net/date
PHP function - time • Time() • Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) • http://kr.php.net/manual/kr/function.time.php
PHP function - math • abs() : return absolute value • ceil() : return minimal integer which is bigger than the value • floor() : return maximum integer which is smaller than the value • round() : return the closest integer <?php $value = abs(-15); echo “$value<br>”; $value = ceil(31.2); echo “$value”<br>; $value = floor(31.2); echo “$value<br>”; $value = round(31.4); echo “$value<br>”; ?>
PHP function - math • pow(a,b) : return ab • sqrt(a) : square root of a • log(a) : logea • log10(a) : log10a • pi() : 3.141592… • hexdec() : change hexadecimal value to decimal value • dechex() : change decimal value to hexadecimal value
PHP function - math • rand(a,b) : make random number between a and b • srand() : initialize random seed • Srand(time()) • http://kr.php.net/manual/kr/ref.math.php
PHP function - file • fopen(file name, mode) : file open • fclose() : close file e.g.) $fp=fopen(“/home/etc/file.txt”, “r”); $fp=fopen(“http://www.php.net/”, “r”); $fp=fopen(“ftp://user:password@example.com/”, “w”); • file_exists() : if file exists, return 1, else return 0 • file() : Reads entire file into an array • $fcontents = file(‘http://www.php.net’);
PHP function - file • fread(), fgets() • Read data from file • fwrite(), fputs() • Write data into file • feof() • Check if it is end of the file • http://kr.php.net/manual/kr/ref.filesystem.php e.g.) fread <?php $filename = “/usr/local/something.txt”; $fd = fopen ($filename, “r”); $contents = fread ($fd, filesize ($filename)); fclose($fd); ?>
Configuration file - PHP • php.ini • C:\Program Files\PHP\php.ini or C:\windows\php.ini • We need to change some configurations • short_open_tag = on ; <? also works instead of <?php • List of php.ini directives
Configuration file - Apache • httpd.conf • C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf • modify line • DirectoryIndex index.html index.php
Configuration file • my.ini • Mysql configuration file • C:\Program Files\MySQL\MySQL Server 5.0\my.ini • To use gnuboard • sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION“ -> • #sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION“ • delete or insert comment mark • MySQL version lower than 5.0 doesn’t need this configuration.
Board • Needs server which supports PHP and MySQL • Guest and user’s can post articles, diaries, photos, etc. • There are some free web boards on the web sites e.g.) zeroboard, gnuboard
Board name Use gnuboard • Using in homepage • Make HTML file using link the gnuboard • Link url http://localhost/gnuboard4/bbs/board.php?bo_table=main
LAB #2 • Implement “Simple Calculator” web application on the Apache + PHP server. • “Simple Calculator” support 2 operands and 6 operations ( +, -, *, /, power, log) • The calculation result is show like following examples • Result page contains the link for file download the calculation result. • The calculation must be conducted on server using PHP • Hint : use <form> tag and POST method.
Homework #1 1) Install apach, mysql, and php 2) Make one homepage to introduce yourself - which includes at least two web boards (e.g. Notice, Guest board…) - which has more than 3 pages - which has one download link for a file - each page must be linked together - use at least one frame - use at least one table • Using Namo Web editor or MS front page are not allowed • Hand in : Mail to TA Hangyu Kim(hgkim@bulsai.kaist.ac.kr) • Contents • the homepage address and your student ID number and your name • Mail title must be • [CC510] homework #1_student number • Contents, design will not effect score • Due date is midnight(24:00) 15th October.
Homework #1(in Korean) 1) apach, mysql, php를 설치하라 2) 자신을 주제로 한 개인 홈페이지를 만들어라. <조건> - 적어도 두개 이상의 게시판을 포함해야 한다. (예. 공지, 방명록,…) - Page는 3개 이상이어야 한다. - 적어도 하나의 download link가 있어야 하며, 파일을 download 받을 수 있어야 한다. - 각 페이지는 서로 link되어 있어야 한다. - 하나 이상의 frame을 사용하여야 한다. - 하나 이상의 table을 사용하여야 한다. • 나모 웹에디터나 프론트페이지 같은 홈페이지 작성 툴은 사용해서는 안됨 • 페이지의 내용과 디자인은 채점에 포함되지 않음. • 제출 : 조교에게 메일을 이용하여 제출. • 담당TA: 김한규(hgkim@bulsai.kaist.ac.kr) • 메일내용 : 홈페이지 주소와 학번, 본인의 이름을 포함해야 한다. • 메일 제목은 다음과 같이 하여 보낸다. • [CC510] homework #1_학번 • 기한은 10월 15일 자정(24:00)까지