![]() | การเขียนโปรแกรมภาษา PHP สำหรับ Dynamic website | ![]() |
ปรับปรุง : 2552-12-25 (ค้น ip หาประเทศ) | ||
หน่วยที่ 1 : ความรู้เบื้องต้น
| |
Download : | + ตัวแปลภาษา 4.4.7 8 MB (last of 4) + PHP Manual 3 MB(html) + PHP Manual 14 MB(chm) รหัสต้นฉบับ (Source Code) + icon.php ? + รายงานการประเมินตนเอง # 10/48 + Webboard (Sample) + SuperModel (Sample) + เพื่อเปิดบริการพื้นที่เว็บฟรี (Sample) + รับสมาชิก และตรวจ (admin:p) + เอกสารหน่วยที่ 5 (.doc) + แปลงสระปัญหาของ PDF + โค้ดอื่น ๆ ที่รวมไว้ ... ![]()
|
หน่วยที่ 3 : บทเรียน
|
![]() CMS + LMS |
| หน่วยที่ 4 : รหัสต้นฉบับ จาก PERLPHPASP.COM | ||
|
|
|
| หน่วยที่ 5 : รหัสต้นฉบับที่ขยายจากหน่วยที่ 2 |
ตัวอย่าง : http://www.yonok.ac.th/burin/mailhtml.php
<?
# version 2 in html format : http://www.yonok.ac.th/burin/mailhtml.php
# version 1 in text : http://www.isinthai.com/isinthai/sndform.php
if (!isset($_POST{"mfrom"})) $_POST{"mfrom"} = "";
if (!isset($_POST{"mto"})) $_POST{"mto"} = "";
if (!isset($_POST{"msubj"})) $_POST{"msubj"} = "";
if (!isset($_POST{"message"})) $_POST{"message"} = "";
if (!isset($_POST{"redirect"})) $_POST{"redirect"} = "";
if (!isset($_GET{"mfrom"})) $_GET{"mfrom"} = "";
if (!isset($_GET{"mto"})) $_GET{"mto"} = "";
if (!isset($_GET{"msubj"})) $_GET{"msubj"} = "";
if (!isset($_GET{"message"})) $_GET{"message"} = "";
if (!isset($_GET{"redirect"})) $_GET{"redirect"} = "";
$mfrom = $_POST{"mfrom"} . $_GET{"mfrom"};
$mto = $_POST{"mto"} . $_GET{"mto"};
$msubj = $_POST{"msubj"} . $_GET{"msubj"};
$message = $_POST{"message"} . $_GET{"message"};
$redirect = $_POST{"redirect"} . $_GET{"redirect"};
if (empty($redirect)) { $redirect = "http://www.yonok.ac.th"; }
if (empty($message)) { $message = "<html><body bgcolor=red>
<a href=http://www.thaiall.com>thaiall.com</a><hr color=white>hello
</body></html>"; }
if (empty($mto) || empty($msubj) || empty($mfrom)) {
echo "<font face="courier new">";
echo "<form action=mailhtml.php method=post>";
echo ".. From : <input name=mfrom value='$mfrom' size=30><br>";
echo ".... To : <input name=mto value='$mto' size=30><br>";
echo "Subject : <input name=msubj value='$msubj' size=50><br>";
echo "Messages:<br><textarea name=message rows=10 cols=80>$message";
echo "</textarea> <input type=submit value='Send mail'>";
echo "<br><input name=redirect type=hidden value=$redirect>";
echo "</form>";
} else {
$msubj = "Subject _ ".$msubj;
mail("$mto,burin@yonok.ac.th","$msubj",$message,"From: $mfrom\r\n"
."Reply-To: $mfrom\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: text/html;charset=windows-874\r\n"
."Content-Transfer-Encoding: 7bit\r\n"
."X-Priority: 1\r\n"
."X-Mailer: PHP/" . phpversion());
// mail("$mto","$msubj",$message,"From: $mfrom\nX-Mailer: PHP/" . phpversion());
echo "<html><head><meta http-equiv=Refresh content='50; URL=$redirect'>";
echo "</head><body><font face="courier new"><pre>";
echo "From : $mfrom \n";
echo "To : $mto \n";
echo "Subject : $msubj \n";
echo "Message :<ul><font color=gray>" . htmlentities($message);
echo "</ul></font><hr color=red>Your message sending : <b>Completely</b>\n";
echo "Script developer : webmaster@thaiall.com\n";
}
?>
|
ตัวอย่าง : http://www.thaiall.com/php/testjpg.php |
ตัวอย่าง : http://www.lampang.go.th/nwt
<table width=100%>
<?
if (!isset($_GET["p"])) $_GET["p"] = 1;
$i = 0;
$dir_handle = opendir(".");
while ($list = readdir($dir_handle)) {
if (strtolower(substr($list,-5)) == "t.jpg") {
$ar[$i] = "<td><a href=../nw/".substr($list,0,6).".jpg>";
$ar[$i] .= "<img src=$list border=0></a></td>";
$last_modified = filemtime($list); // Ex. Monday, 27th October, 2003 @ 02:59pm
$ar[$i] .= " Last modified " . date("l, dS F, Y @ h:ia", $last_modified);
$i = $i + 1;
}
}
closedir($dir_handle);
srand(time());
$pp = 10;
$end = $_GET["p"] * $pp;
print "<table width=100%>";
for ($k=($end - $pp);$k<$end;$k++) {
// $j = rand() % $i;
// print $ar[$j];
print $ar[$k];
}
// echo (sprintf("%03d", 5) . strlen(sprintf("%03d", 5))); // 0053
print "</table>";
for ($c=1;$c<=($i / $pp);$c++) {
print "<a href=?p=$c>$c</a> | ";
}
?>
|
ตัวอย่าง : http://yn1.yonok.ac.th/catalog/subjdesc.php
<html><head><title>Course description database</title></head>
<body bgcolor=#ffffdd><font face="microsoft sans serif" size=0>
<form action=subjdesc.php>
แสดงรายวิชาทั้งหมด [<a href=subjdesc.php>แบบรายชื่อ</a>,
<a href=subjdesc.php?codes=csv>แบบ CSV</a>]
หรือค้นหาตามรหัสวิชา หรือคำอธิบายรายวิชา
<br><input name=codes><input type=submit><br>
</form>
<?
$d{'ACCT 100'}="การบัญชีทั่วไป~3(3-0)~~ศึกษา...";
$d{'ACCT 201'}="หลักการบัญชี 1~3(3-0)~~ศึกษาเกี่ยว...";
$d{'ACCT 202'}="หลักการบัญชี 2~3(3-0)~หลักการบัญชี 1 (ACCT 201)~ศึกษา...";
if (isset($_GET['codes'])) { $c = $_GET['codes']; }
if (isset($_POST['codes'])) { $c = $_POST['codes']; }
if (strlen($c) == 0) { $c = "list"; }
if (strlen($d{$c}) > 1) {
$ds = explode("~",$d{$c});
echo "<b>$c</b> : $ds[0] $ds[1]";
echo "<ul><b>วิชาบังคับก่อน</b> : $ds[2]";
echo "<dd>$ds[3]</ul>";
} else {
if ($c == "csv") {
echo "<pre>";
while( list ($k,$v) = each($d)) {
$ds = explode("~",$d{$k});
echo "$k,$ds[0],$ds[1],$ds[2],$ds[3]<br>";
}
echo "</pre>";
} else {
echo "<ul><ol>";
while( list ($k,$v) = each($d)) {
if ((strpos(strtolower($d{$k}),strtolower($c)) > 0) || _
(strlen($c) == 0) || ($c == "list")) {
$ds = explode("~",$d{$k});
echo "<li><a href='?codes=$k'>$k</a> : $ds[0] $ds[1]";
}
}
echo "</ol></ul>";
}
}
// Code เพิ่มเติมเกี่ยวกับอาร์เรย์
// $arr = array("thai", "siam", "lampang");
// sort($arr);
// reset($arr);
// while (list($key, $value) = each($arr)) {
// echo "Key: $key; Value: $value<br>";
// }
// foreach ($arr as $key => $value) {
// echo "Key: $key; Value: $value<br>";
// }
?>
</body></html>
|
ตัวอย่าง : http://www.thaiall.com/php/secure.php
<body>
<?
$ft = $_POST['keept'] % 5;
if (($_POST['u'] == "a" && $_POST['p'] == "b") && (
($ft == "0" && $_POST['w'] == "abc") ||
($ft == "1" && $_POST['w'] == "def") ||
($ft == "2" && $_POST['w'] == "oho") ||
($ft == "3" && $_POST['w'] == "love") ||
($ft == "4" && $_POST['w'] == "wow") ))
{ echo "ok : pass"; } else {
$t = microtime() * 1000000;
$img = $t % 5;
echo "<img src=secure". $img .".jpg>";
?>
<form action=secure.php method=post>
Word in picture : <input name=w><br>
User : <input name=u value="a"><br>
Password : <input name=p value="b"><br>
<input type=hidden name=keept value=<?=$t;?>><br>
<input type=submit value="Check password">
</form>
<? } ?>
</body>
|
Source Code : (empl.mdb 68 KB = empl :emplid as double,emplname as text,emplsalary as double) |
<base href=http://www.youtube.com/>
<?
$ar = file("http://www.youtube.com");
foreach ($ar as $value) {
echo $value;
}
?>
|
Commnet : manager.co.th มี hit rate เยอะเกินไป Response Time ต่ำ ไม่เหมาะที่จะดูด
<?
$x = '<table cellspacing=0 cellpadding=2 width="100%" border=0>';
$a = file("http://www.manager.co.th/Home/default.html");
$xl = strlen($x);
$found = 0;
$l = 0;
echo "คำที่ค้นหามีขนาด $xl ตัวอักษร\n";
for($i=0;$i<count($a);$i++) {
if (stristr($a[$i],$x)) {
$found = 1;
}
if ($l <= 26 && $found == 1) {
echo $a[$i]."\n";
$l = $l + 1;
}
}
?>
|
9.1 ดูด Directory ของ google (ปรับปรุง 2552-08-24) ตัวอย่าง : http://www.thaiall.com/php/google.php <? $web = file("http://www.google.co.th/dirhp?hl=th&tab=gd&q="); $chk1 = '<table align=center border=0 cellpadding=0 cellspacing=7 width=1%>'; $chk2 = "</table>"; $found = 0; $first = 1; $l = 0; if (!isset($_GET['url'])) { echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8">'; echo "<body bgcolor=#ffffdd><b>My Directory</b>\n"; for($i=0;$i<count($web);$i++) { if ($found == 0) { $r = split($chk1,$web[$i]); if (count($r) > 1) { $found = 1; echo $chk1; # พิมพ์คำที่ค้นหาเป็นคำแรก } } else { $r = split($chk2,$web[$i]); if (count($r) > 1) $found = 2; $u = $web[$i]; echo str_replace("/Top/World/Thai/","?url=",$u); } if ($found == 2) break; } # end for } else { echo "<frameset><frame src=http://www.google.co.th/Top/World/Thai/" . urlencode($_GET['url']) . "></frameset>"; } # end if ?> 9.2 ดูดจาก lovelampang.com มาไว้ใน thaiall.com <? $a = file("http://www.lovelampang.com/iblog.php"); for($i=0;$i<count($a);$i++) { echo str_replace("<body","<base href=http://www.lovelampang.com/><body",$a[$i]); echo "\n"; } ?> 9.3 ดูด url ของ mp3 จากเว็บไซต์ของ encarta ตัวอย่าง : http://www.thaiall.com/listen/_cut.php <font size=6><b>โปรแกรมช่วยจับชื่อแฟ้มเสียงจากผลการสืบค้น <br>http://encarta.msn.com</b></font> <form action=_cut.php method=post> <input name=url size=90 value='http://encarta.msn.com/dictionary_1861582869/a.html'> <input type=submit> </form><br> <? if (isset($_POST['url'])) { $chk1 = "http://images.encarta.msn.com"; $chk2 = ".mp3"; $r = file($_POST['url']); $found = 0; for($i=0;$i<count($r);$i++) { $r1 = split($chk1,$r[$i]); if (count($r1) > 1) { $found = 1; $r2 = split($chk2,$r1[1]); $r3 = $r2[0]; } } echo "<a href=".$chk1.$r3.$chk2.">mp3</a>"; } ?> 9.4 ดูดเว็บเพจของ ไอทีในชีวิตประจำวัน ตัวอย่าง : http://www.thaiall.com/itinlife/article.php ปรับปรุงมาจากโปรแกรมดูดเว็บที่ใช้กับ google.com จึงทำให้ดูเข้าใจง่ายขึ้น <table width=760 align=center><td bgcolor=#ffffdd> <? $web = file("http://www.thaiall.com/opinion/readonly.php?view=".$id); $chk1 = "<td bgcolor=white>"; $chk2 = "</td></tr><tr><td align=right bgcolor=black>"; $found = 0; $first = 1; $stop = 0; $l = 0; for($i=0;$i<count($web);$i++) { $r1 = split($chk1,$web[$i]); $r2 = split($chk2,$web[$i]); if (count($r1) > 1) $found = 1; if ($found == 1 && $stop == 0) { if ($first == 1) { echo "<dd>$r1[1]"; // พิมพ์ครั้งแรก เมื่อพบคำแรก $first = 0; } else { if (count($r2) > 1) $stop = 1; echo "<dd>$r2[0]"; // พิมพ์ที่เหลือ ก่อน $stop = 1 } } } ?> </td></table> |
<! up.php โปรแกรม upload แฟ้มข้อมูลเข้าไปในเครื่องบริการ>
<form action=up.php enctype="multipart/form-data" method=post>
<input type=file name=uploadfile>
<input type=submit>
</form>
<?
if (isset($_FILES["uploadfile"]["name"])) {
if(copy($_FILES["uploadfile"]["tmp_name"],"./".$_FILES["uploadfile"]["name"]))
echo "upload : process";
else
echo "upload : error";
}
?>
|
<! x.php โปรแกรมที่มีฟอร์มรับค่าเข้าตัวแปร uname แล้วส่งให้ y.php แบบ get> <body> <form action=y.php method=get> <input name=uname size=40><br> <input type=submit> </form> </body> |
ตัวอย่าง : http://www.yonok.ac.th/burin/countersession.php <body> <? # http://th2.php.net/manual/en/ref.session.php # http://th2.php.net/manual/en/function.session-cache-expire.php # session_start() ต้องมีในทุกเว็บเพจที่ต้องการใช้ตัวแปรแบบ session # session_save_path("/var/www/cgi-bin/tmp"); สร้างห้อง tmp ไว้เก็บ session สำหรับ Linux # session_save_path("f:/203.130.156/sahapat/tmp"); ตัวอย่างสำหรับ windows session_start(); if (!isset($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } # for delete the session variable # session_unregister('count'); ?> <br> Hello visitor, you have seen this page <?=$_SESSION['count']; ?> times. </body> :: เพราะผมเคยใช้ session ใน hypermart.net ไม่ได้ และต.ค.48 อ.ถนอมก็บ่นว่า server เขาไม่รับ :: พอสืบค้นก็พบข้อมูลจาก http://www.hypermart.net/knowledgebase/read_article.bml?kbid=600 :: ทำให้รู้ว่าบาง server ต้องกำหนดห้องเก็บ session เอง เพื่อ Security :: ถ้าใช้ session ไม่ได้ ก็ให้กำหนด session_save_path ตาม $_ENV["DOCUMENT_ROOT"] :: เช่น session_save_path("/home/users/web/[bucket]/[home dir]/cgi-bin/tmp"); ก่อน session_start(); |
Source Code : http://www.thaiall.com/php/timetabling.php<? if (!isset($_POST['regist'])) { echo "ระบบจัดตารางสอบอย่างง่าย ของนักเรียน 3 คนที่ลงทะเบียนคนละ 3 วิชา"; echo "<form action=timetabling.php method=post>"; echo "<textarea name=regist rows=10 cols=60> 1,a,b,d, 2,a,d,e, 3,b,c,d, </textarea><br><input type=submit value='Timetabling'>"; echo "</form>"; exit; } // แสดงข้อมูลที่รับมาจาก form echo "<pre>แสดงรายวิชาที่นักเรียนแต่ละคนลงทะเบียนเรียน \n"; $student = split("\n",$_POST['regist']); foreach($student as $one) { $subj = split(",",$one); for($i=0;$i<count($subj);$i++) echo $subj[$i]; } // นำข้อมูลแต่ละคน มาแยกเก็บแยกวิชา เช่น a,1,2 ลงตัวแปร $s เพื่อใช้ในการจัดตารางสอบ foreach($student as $one) { $subj = split(",",$one); for($i=1;$i<count($subj)-1;$i++) { if (!isset($s[$subj[$i]])) $s[$subj[$i]] = $subj[$i] . ","; $s[$subj[$i]] = $s[$subj[$i]] . $subj[0] . ","; } } // อ่าน $s มาจัดลงตัวแปร $sub เพื่อใช้อ้างวิชาที่ได้ เช่น $sub[0] หมายถึงวิชา "a" echo "<hr>แสดงชื่อนักเรียนที่ลงทะเบียนในแต่ละวิชา"; $i = 0; foreach($s as $sepsubj) { $sepagain = split(",",$sepsubj); $sub[$i] = $sepagain[0]; $i = $i + 1; echo $sepsubj . "\n"; // $sepsubj เก็บรหัสนักศึกษาในแต่ละวิชา } // จัดกลุ่มตามวัน โดยใช้ while ซ้อน for และตรวจสอบความซ้ำซ้อน $end = 0; $i = 0; while ($end == 0) { $outday = 0; for($j=0;$j<count($sub);$j++) { if (strlen($s[$sub[$j]]) > 2 && $outday == 0) { $day[$i] = $sub[$j].","; $base = $s[$sub[$j]]; $s[$sub[$j]] = ""; $outday = 1; } } for($j=1;$j<count($sub);$j++) { $sbase = $s[$sub[$j]]; $ssubj = split(",",$sbase); $found = 0; for($k=1;$k<count($ssubj)-1;$k++) { if (stristr($base,$ssubj[$k])) $found = 1; } if ($found == 0 && count($ssubj) > 1) { $s[$sub[$j]] = ""; $day[$i] = $day[$i] . $sub[$j] . ","; } } $i++; $end = 1; foreach ($s as $checksubj) { if (strlen($checksubj) > 2 ) $end = 0; } } echo "<hr>"; // แสดงผลการจัดตารางแล้ว for($i=0;$i<count($day);$i++) { echo "ชั่วโมงที่ "; echo $i + 1; echo " สอบวิชา " . $day[$i] . "\n"; } ?> |
Source Code : http://www.thaiall.com/php/barchart.php?b1=50&b2=100&b3=120&b4=30 14.1 เขียน barchart.php (ปรับปรุง : 5 กรกฎาคม 2548 เพราะเดิมลืมเครื่องหมาย $) |
<?
// $msconnect = mssql_connect("172.16.20.81:1433","sa","");
$msconnect = mssql_connect("MyServer70","sa","");
$msdb=mssql_select_db("Northwind",$msconnect);
$msquery = "select firstname,lastname from employees";
$msresults= mssql_query($msquery);
echo "<ol>";
while ($row = mssql_fetch_array($msresults)) {
echo "<li>" . $row['firstname'] . " " . $row['lastname'] . "\n";
}
echo "</ol>";
?>
|
ขณะนี้ online : <?
$gap = 60; // 60 seconds
$t = $_SERVER["REMOTE_ADDR"].",".time()."\n";
$f = "countuser.txt";
$c = 0;
$new = 1; // ตั้งไว้ก่อนว่าเป็นคนใหม่
if (file_exists($f)) {
$fr=file($f);
$fw=fopen ($f,"w");
foreach ($fr as $value) {
$ft = split(",",$value);
if ($ft[0] == $_SERVER["REMOTE_ADDR"]) {
fputs ($fw,$_SERVER["REMOTE_ADDR"].",".time()."\n");
$c = $c + 1;
$new = 0;
} else {
if ((time() - $gap) < $ft[1]) {
fputs ($fw,$value);
$c = $c + 1;
}
}
}
if ($new == 1) {
fputs ($fw,$t);
$c = $c + 1;
}
echo $c;
fclose ($fw);
} else {
$fw=fopen ($f,"w");
fputs ($fw,$t);
fclose ($fw);
echo "1";
}
?> คน
|
<?
srand(time());
$num = 10;
$a = array();
while (count($a) < $num) {
$n = rand() % $num;
if (!in_array($n, $a)) array_push($a,$n);
}
for ($i=0;$i<$num;$i++) echo $a[$i]."<br>";
?>
|
<body> <form action=bad.php method=post> <textarea name=word rows=5 cols=50> แดงบอย น้อย ขาวดำ ก็มี ดาว ดำ ดีดี สุภาพดำนะไม่ดี </textarea><input type=submit></form> <? $bi=-1; $bi++; $bad[$bi]="ทอง"; $bi++; $bad[$bi]="ขาว"; $bi++; $bad[$bi]="ดำ"; for($b=0;$b<=$bi;$b++) $_POST["word"] = str_replace($bad[$b],"..",$_POST["word"]); echo $_POST["word"]; ?> </body> |
<?
if (isset($_GET['codes']))
header("Location: http://yn1.yonok.ac.th/catalog/subjdesc.php?codes=" . $_GET['codes']);
else
header("Location: http://yn1.yonok.ac.th/catalog/subjdesc.php");
?> |
<pre>
<?
if (!isset($_POST["ch"])) $a = "<a title=test>ทดสอบ/aaa</a> // \\ // \\";
else $a = $_POST["ch"];
// Normal Output
echo $a."\n";
# Output: ทดสอบ/aaa // \ // \
// Un-quote string quoted with addslashes()
echo stripslashes($a)."\n";
# Output: ทดสอบ/aaa // //
// Quote string with slashes
echo addslashes($a)."\n";
# Output: ทดสอบ/aaa // \\ // \\
// URL-encodes string
echo urlencode($a)."\n";
# Output: %3Ca+title%3Dtest%3E%B7%B4%CA%CD%BA%2Faaa%3C%2Fa%3E+%2F%2F+%5C+%2F%2F+%5C
// Decodes URL-encoded string
echo urldecode(urlencode($a))."\n";
# Output: ทดสอบ/aaa // \ // \
// Convert special characters to HTML entities
echo htmlspecialchars($a)."\n";
# Output: <a title=test>ทดสอบ/aaa</a> // \ // \
// Convert all applicable characters to HTML entities
echo htmlentities($a)."\n";
# Output: <a title=test>?????/aaa</a> // \ // \
// Encodes data with MIME base64
echo base64_encode($a)."\n";
# Output: PGEgdGl0bGU9dGVzdD63tMrNui9hYWE8L2E+IC8vIFwgLy8gXA==
// Decodes data encoded with MIME base64
echo base64_decode(base64_encode($a))."\n";
# Output: ทดสอบ/aaa // \ // \
// One-way string encryption (hashing) (ผลไม่ซ้ำกัน)
echo crypt($a)."\n";
# Output: $1$RL..GJ..$HzbDnURKr6iBOL1txhwk2/
# EX. if (crypt($user_input, $passwd) == $passwd) echo "OK";
# EX. if (crypt($_GET["p"],"$1$01..Hv/") == "$1$01..Hv/") { }
?>
<form action="" method=post><input name=ch><input type=submit></form>
</pre> |
<?
# ใช้ session_save_path ในกรณีที่ server บังคับให้ใช้ เช่น hypermart.net
# session_save_path("/var/www/cgi-bin/tmp"); สร้างห้อง tmp ไว้เก็บ session เป็นต้น
session_start();
$_SESSION["a"] = 5;
foreach ($_SESSION as $k=>$v) {
echo $_SESSION["$k"];
}
foreach ($_SERVER as $k=>$v) {
echo $k . $_SERVER["$k"];
}
foreach ($_ENV as $k=>$v) echo $k." = ".$v."<br>";
phpinfo();
?>
Testing of session.php
<?
session_start();
if (isset($_SESSION["aaa"])) {
echo $_SESSION["aaa"] . "<br>";
if (isset($_GET["delete"])) {
session_unset();
session_destroy();
header("location: session.php");
}
}
if (isset($_GET["create"])) {
$_SESSION["aaa"] = "aaa";
header("location: session.php");
}
?>
<a href=?create=yes>?create=yes</a>
<a href=?delete=yes>?delete=yes</a>
|
<?
$host = "localhost";
$db = "test";
$tb = "testtb";
$user = "root";
$password = "";
###########################
if (isset($_POST["action"])) {
if ($_POST["action"] == "insert") {
$query = "insert into $tb (id,name) values (". $_POST["id"] .",'". $_POST["name"] ."')";
}
if ($_POST["action"] == "delete") {
$query = "delete from $tb where id =". $_POST["id"];
}
if ($_POST["action"] == "update") {
$query = "update $tb set name='".$_POST["name"] ."' where id =". $_POST["id"];
}
$connect = mysql_connect("$host","$user","$password");
mysql_query("set names tis620"); // การเพิ่มบรรทัดนี้ ทำให้ข้อมูลใน mysql เก็บถูกต้อง
$result = mysql_db_query($db,$query);
if ($result) echo "<br>". $_POST["action"] ." OK<br>"; else exit;
mysql_close($connect);
}
###########################
$query = "select * from $tb";
$connect = mysql_connect("$host","$user","$password");
mysql_query("set names tis620"); // การเพิ่มบรรทัดนี้ ทำให้ดึงข้อมูลมาแสดงอย่างถูกต้อง
$result = mysql_db_query($db,$query);
if ($result) echo "<br>OK<br>"; else exit;
while ($object = mysql_fetch_object($result)) {
foreach ($object as $o) echo $o." ";
# echo $object->cust . " " . $object->custname;
echo "<br>";
}
echo "Total records : ".mysql_num_rows($result);
mysql_close($connect);
###########################
?>
<form action=? method=post>
<b>Insert</b><br>id : <input name=id><br>name : <input name=name><br>
<input type=submit value=insert name=action>
</form>
<form action=? method=post>
<b>Update</b><br>id : <input name=id><br>name : <input name=name><br>
<input type=submit value=update name=action>
</form>
<form action=? method=post>
<b>Delete</b><br>id : <input name=id><br>
<input type=submit value=delete name=action>
</form> |
<?
// http://www.phpconcept.com/content/view/21/28/
$host = "localhost";
$db = "db";
$tb = "phpbb_users";
$user = "admin";
$password = "p";
$query = "select * from $tb";
###########################
$connect = mysql_connect("$host","$user","$password");
// $charset = "set character_set_results=tis620";
$charset = "set names tis620";
mysql_query($charset) or die('Invalid query: ' . mysql_error());
$result = MySQL_db_query($db,$query);
while ($object = MySQL_fetch_object($result)) {
echo $object->username . "<br>";
}
mysql_close($connect);
?>
+ แก้ปัญหาภาษาไทย เมื่อพิมพ์ภาษาไทยใน phpmyadmin แล้วออกเป็น ???
|
<?
$url = "http://lampang.thcity.com/face/indexo.html";
if (isset($_POST["url"])) {
$url = strtolower($_POST["url"]);
$web = file($url);
echo "<ol>";
for($i=0;$i<count($web);$i++) {
$web[$i] = strtolower($web[$i]);
$found = 0;
$r1 = split("href=",$web[$i]);
if (count($r1) > 1) $found = 1;
if ($found == 1) {
$r2 = split('[ >]',$r1[1]);
$tag = $r2[0];
echo "<li>";
if (substr($r2[0],0,4) != "http")
echo "<font color=red>$tag</font>\n"; else echo $tag."\n";
}
}
echo "</ol>Total Line : ". count($web);
}
echo "<form action='' method=post><input name=url value='$url' size=60>
<input type=submit></form>";
?> |
<?
// global $x; declare here, can not used in function
$start = 0;
xtime("start");
for($i=1;$i<=10;$i++) echo "$i<br>";
echo xtime("stop");
// =======================
function xtime ($action) {
global $start;
list($u,$s) = split(" ",microtime());
$n = $s + $u;
if ($action == "start")
$start = $n;
else
return ($n - $start);
}
?> |
my01.htm <form action=my01.php method=post> <input name=a><input name=b><br> <input type=radio value=i name=c>insert<br> <input type=radio value=d name=c>delete<br> <input type=radio value=u name=c>update<br> <input type=radio value=s name=c>select<br> <input type=submit> </form> |
<?
// unzip( $zipfile, $destination_folder )
// สร้างห้อง c:\xxx ให้ใหม่ เมื่อไม่ถูกสร้างไว้
unzip('c:\\test.zip','c:\\xxx\\');
echo ": ok";
function unzip($file, $path) {
$zip = zip_open($file);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
if (zip_entry_filesize($zip_entry) > 0) {
// str_replace must be used under windows to convert "/" into "\"
$complete_path = $path.str_replace('/','\\',dirname(zip_entry_name($zip_entry)));
$complete_name = $path.str_replace ('/','\\',zip_entry_name($zip_entry));
if(!file_exists($complete_path)) {
$tmp = '';
foreach(explode('\\',$complete_path) AS $k) {
$tmp .= $k.'\\';
if(!file_exists($tmp)) mkdir($tmp, 0777);
}
}
if (zip_entry_open($zip, $zip_entry, "r")) {
$fd = fopen($complete_name, 'w');
fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
fclose($fd);
zip_entry_close($zip_entry);
}
} // end of if
} // end of while
zip_close($zip);
} // end of if
} // end of function
?>
|
<?
echo "<br>".date("d/m/Y H:i:s"); // 13/07/2006 18:29:00
echo "<br>".time(); // 1152185502
echo "<br>".microtime(); // 0.23212200 1152185502
echo "<br>". ( microtime() + microtime() ); // 0.464397
echo "<pre><font face="courier new">";
$cmd =`type c:\\autoexec.bat`;
echo "<hr>$cmd";
$cmd =`echo dir %WINDIR% > a.bat`;
echo "<hr>$cmd";
$cmd =`type a.bat`;
echo "<hr>$cmd";
$cmd =`a.bat`;
echo "<hr>$cmd";
$cmd =`dir`;
echo "<hr>$cmd";
$cmd = shell_exec("echo dir %WINDIR% > b.bat");
$cmd = shell_exec("dir b.bat");
echo "<hr>$cmd"; // 17 Bytes
$cmd = shell_exec('del b.bat');
$cmd = shell_exec('dir b.bat');
echo "<hr>$cmd";
?> |
$host = "127.0.0.1" ;
$output = shell_exec("ping -c1 $host");
echo "<pre>$output</pre>";
$user = "hello";
$pass = "wow";
echo shell_exec("sudo -u root -S /usr/sbin/useradd $user");
echo shell_exec("echo $pass |sudo -u root -S /usr/bin/passwd $user --stdin");
echo shell_exec("id $user");
วิธีทำให้ใช้ sudo และ useradd ด้วยการแก้ไขแฟ้ม /etc/sudoers
- เปลี่ยนสิทธิ์จาก 440 เป็น 777
- เปิดแฟ้ม /etc/sudoers ด้วย vi แล้วแก้ให้เลิกถามรหัสผ่านเมื่อใช้ sudo
- เพิ่ม apache ALL= NOPASSWD: /usr/sbin/useradd, /usr/bin/passwd
- ต้องเปลี่ยนกลับเป็น 440 จึงจะสั่ง script ให้ทำงานได้
วิธีทำให้ใช้ shell_exec ไม่ได้
<? // chmod 644 maillog $cmd =`tail -100 /var/log/maillog`; $a = split("\n",$cmd); $user =""; $time =""; echo "<font size=0>"; for ($i=0;$i<count($a);$i++){ $r = split("imap-login",$a[$i]); if(count($r) > 1){ $f = split(" ",$r[1]); $user = $f[2]; $time = substr($r[0],0,15); if($user != "login") $u[$user] = "<br /><b>Last login</b> : ". $user ." ".$time; } } foreach($u as $v) echo $v; ?> |
<?
// callAmazonRESTWS.php จาก http://gotoknow.org/blog/krunapon/74176
// โดย ผศ.ดร.กานดา รุณนะพงศา
// - ถ้าไม่ส่ง parameters จะเรียก http://webservices.amazon.com/onca/xml ไม่ได้
// - http://www.onlamp.com/pub/a/php/2003/10/30/amazon_rest.html
header("Content-type: text/xml");
$base = 'http://webservices.amazon.com/onca/xml';
$query_string = '';
$params = array(
'Service' =>'AWSECommerceService',
'SubscriptionId' => '16XT8ETKKB7NWHAGCQ02' ,
'Operation' => 'ItemSearch',
'SearchIndex' => 'Books',
'Keywords' => 'Web Services');
foreach ($params as $key => $value) {
if ($key != 'Keywords')
$query_string .="$key=".urlencode($value) . "&";
else
$query_string .="$key=".urlencode($value);
}
$url = "$base?$query_string";
$xml = file_get_contents($url);
echo $xml;
?>
|
<?
if (isset($_GET["i"])) $i = $_GET["i"]; else $i = 1;
if (isset($_GET["f"])) $f = $_GET["f"]; else $f = 1;
if ($i < 1 || $f < 1) exit;
$filename = 'http://www.thaiall.com/vbnet/testtoolbox_0' . $f .'.gif';
$percent = 1; // 0.2 สำหรับลดขนาดลง 20%
header('Content-type: image/gif');
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$gap = ($height - 17) / 20;
$nh = $newheight - (20 * $gap);
// $thumb = imagecreatetruecolor($newwidth, $newheight);
$thumb = imagecreatetruecolor($newwidth, $nh);
$source = imagecreatefromgif($filename);
// imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresized($thumb, $source, 0, 0, 0, ($i * 20 - 2), $newwidth, $nh, $width, $nh);
imagejpeg($thumb);
?>
|
<?php
header("Content-type: text/vnd.wap.wml");
print "<?xml version=\"1.0\"?>\n";
?>
<wml><card id="HTML" title="<?php print rand();?>">
<p>Hello on thaiall.com<br/>
<?php print date("d/m/Y H:i:s"); ?></p>
</card></wml>
|
<?
$r = fopen("mas1.dat","r"); // input file
$o = fopen("mas1.txt","w"); // output file
$v = fread($r, 16); // skip head
for ($i=1;$i<=200;$i++) {
$v = fread($r, 36);
fwrite($o, $v.chr(10));
}
fclose($o);
fclose($r);
?>
|
|
|
<? $u = 'http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=11.2.3.9'; $tags = get_meta_tags($u); foreach($tags as $key => $value) { echo $key . $value. '<br>'; } // print $tags['city']; ?> |
|
| หน่วยที่ 6 : แนะนำเว็บ (Web Guides) | ||||
Writing XLS File with PHP [ Download : php2xls.zip ]
Demo Files หรือ example-stocks.php (ได้นำไปทดสอบใน thaiabc.com ก็ไม่พบปัญหาใด)
การนำไปใช้ ทำโดยเปิดแฟ้ม example-stocks.php ด้วย notepad เพื่อแก้ไข หรือเพิ่มข้อมูลลงใน cell
การนำไปใช้ แสดงรายชื่อนักศึกษา gradesubjn.php
PHP e-Commerce
http://www.tutorialized.com/tutorials/PHP/E-Commerce/1 (รวม link)
http://www.oscommerce.com (สมบูรณ์ด้วย PHP + MYSQL)
http://www.zen-cart.com (หน้าตาก็เหมือน zen-cart.com นั่นเลย)
PHP calendar with MYSQL
http://www.jtr.de/scripting/php/calendar/index_eng.html
http://keithdevens.com/software/php_calendar
Library of .dll on PHP
http://www.multimedia.uni-greifswald.de/~php4/
http://bugs.php.net/bug.php?id=4284 (problem about php_mssql70.dll on win98)
http://www.kromann.info/php.php (problem about php_mssql70.dll on win98)
Image in Script
http://cannot.info/base64image (convert image in php)
Fusion Charts
http://www.fusioncharts.com
Google Charts (หาวิธีแสดงภาษาไทยยังไม่ได้ ใช้ %A1 แล้วหยุดเลยครับ)
http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=%41%42|%61%62%25
|
|
แนะนำหนังสือ
- คู่มือเรียน PHP และ MYSQL สำหรับผู้เริ่มต้น, พร้อมเลิศ หล่อวิจิตร, provision, 239 บาท |