590 likes | 726 Views
-- PHP, Web Board – Lecturer : Sujung Bae sjbae@paradise.kaist.ac.kr. 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.
E N D
-- PHP, Web Board – Lecturer : Sujung Bae sjbae@paradise.kaist.ac.kr 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
MySQL Administrator: User > Add Host localhost
Installing gnuboard • Create DB • Add user • Grant the user privilege to access database • Run and restart mysql
Installing gnuboard • http://sir.co.kr/main/gnuboard4/ • download gnuboard4.tgz (1.2M) from http://sir.co.kr/bbs/board.php?bo_table=g4_pds&wr_id=4276 • Extract gnuboard4.tgz into /htdocs/ • C:\Program Files\Apache Software Foundation\Apache2.2\htdocs • Access the file via web browser • http://localhost/gnuboard4