2005/Nov/23
ตรวจสอบ Browser
<?
function getBrowserName() {
global $HTTP_USER_AGENT;
$browser=strtoupper($HTTP_USER_AGENT);
if (strstr($browser,"MSIE."))
return "MS Internet Explorer";
else if (strstr($browser,"MOZILLA"))
return "Netscape";
else
return "";
}
$name = getBrowserName();
if ($name != "") {
echo "Your browser is ".$name.".<BR>";
}
?>
<?
function getBrowserName() {
global $HTTP_USER_AGENT;
$browser=strtoupper($HTTP_USER_AGENT);
if (strstr($browser,"MSIE."))
return "MS Internet Explorer";
else if (strstr($browser,"MOZILLA"))
return "Netscape";
else
return "";
}
$name = getBrowserName();
if ($name != "") {
echo "Your browser is ".$name.".<BR>";
}
?>
การใช้ตัวแปรแบบ global ภายในฟังก์ชัน
บางครั้งเราไม่ต้องการที่จะผ่านตัวแปรเป็นอาร์กิวเมนต์ของฟังก์ชัน เพื่อนำไปใช้ภายในฟังก์ชันเหล่านั้น ก็จะทำได้โดยการแจ้งใช้ตัวแปรที่มีชื่อเหมือนตัวแปรภายนอกที่เราต้องการใช้ ให้เป็น global หรือใช้ผ่านตัวแปรที่เป็นอาร์เรย์ของ PHP ที่มีชื่อว่า $GLOBALS ดังตัวอย่างต่อไปนี้
ตัวอย่าง
<?
$a = 10;
$b = 20;
function getMin ( ) {
global $a, $b;
if ($a < $b)
return $a;
else
return $b;
}
function getMin2 () {
if ($GLOBALS["a"] < $GLOBAL["b"])
return $GLOBALS["a"];
else
return $GLOBALS["b"];
}
echo getMin()."<BR>\n";
echo getMin2()."<BR>\n";
?>
บางครั้งเราไม่ต้องการที่จะผ่านตัวแปรเป็นอาร์กิวเมนต์ของฟังก์ชัน เพื่อนำไปใช้ภายในฟังก์ชันเหล่านั้น ก็จะทำได้โดยการแจ้งใช้ตัวแปรที่มีชื่อเหมือนตัวแปรภายนอกที่เราต้องการใช้ ให้เป็น global หรือใช้ผ่านตัวแปรที่เป็นอาร์เรย์ของ PHP ที่มีชื่อว่า $GLOBALS ดังตัวอย่างต่อไปนี้
ตัวอย่าง
<?
$a = 10;
$b = 20;
function getMin ( ) {
global $a, $b;
if ($a < $b)
return $a;
else
return $b;
}
function getMin2 () {
if ($GLOBALS["a"] < $GLOBAL["b"])
return $GLOBALS["a"];
else
return $GLOBALS["b"];
}
echo getMin()."<BR>\n";
echo getMin2()."<BR>\n";
?>
การสร้างสตริงค์แบบสุ่มอีกแบบหนึ่งซึ่งอาจจะนำไปใช้ในการสร้าง one-time password ( OTP)
ตัวอย่าง
<?
function randomToken($len) {
srand( date("s") );
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$chars.= "1234567890!@#$%^&*()";
$ret_str = "";
$num = strlen($chars);
for($i=0; $i < $len; $i++) {
$ret_str.= $chars[rand()%$num];
}
return $ret_str;
}
echo randomToken(13)," ";
?>
ตัวอย่าง
<?
function randomToken($len) {
srand( date("s") );
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$chars.= "1234567890!@#$%^&*()";
$ret_str = "";
$num = strlen($chars);
for($i=0; $i < $len; $i++) {
$ret_str.= $chars[rand()%$num];
}
return $ret_str;
}
echo randomToken(13)," ";
?>
รวมหัวข้อ PHP Code
การใช้อาร์เรย์
อาร์เรย์ในภาษา PHP นั้นจะแตกต่างจากอาร์เรย์ในภาษาซีหรือจาวาตรงที่ว่า อาร์เรย์ในภาษา PHP มีขนาดที่เปลี่ยนแปลงได้ หรือจะเรียกว่า dynamic array หรือ vector (สำหรับอาร์เรย์มิติเดียว) เริ่มต้นอาจจะแจ้งใช้ตัวแปรแบบอาร์เรย์ พร้อมเจาะจงขนาดเริ่มแรก เช่น มีขนาดเป็นศูนย์ก็ได้
ตัวอย่าง
$myarray[]=3;
$myarray[]=1.1;
$myarray[]="abc";
แต่เมื่อใช้อาร์เรย์ไป ขนาดของมันจะปรับเปลี่ยนได้ คือขยายจำนวนข้อมูลที่เก็บอยู่ภายในอาร์เรย์ ตามจำนวนข้อมูลที่เราใส่เพิ่มเข้าไป จากตัวอย่างข้างบน ในกรณีที่เรามิได้กำหนดเลขดัชนี (index) ก็หมายความว่า จะมีการขยายขนาดของอาร์เรย์เพิ่มขึ้นอีกหนึ่งโดยอัตโนมัติ ทุกครั้งที่เราใส่ข้อมูลที่อยู่ทางขวา และค่าที่เรากำหนดจากทางขวามือ และจะเก็บไว้ในที่ใหม่ของอาร์เรย์ เราไม่ต้องคำนึงถึงเรื่องการจอง หรือ ปลดปล่อยหน่วยความจำของอาร์เรย์ เหมือนอย่างในกรณีของอาร์เรย์ แบบไดนามิกในภาษาซี
นอกจากนั้นข้อมูลแต่ละตัวในอาร์เรย์ไม่จำเป็น ต้องเป็นข้อมูลชนิดเดียวกัน เช่น อาจจะมีทั้งจำนวนเต็ม เลขทศนิยม และข้อความ ปะปนกันไป ตัวอย่างเช่น
ตัวอย่าง
<?
$myarray[0] = 1;
echo "number of elements =".count($myarray)."<BR>\n";
$myarray[1] = "abc";
echo "number of elements =".count($myarray)."<BR>\n";
$myarray[2] = 1.3;
echo "number of elements =".count($myarray)."<BR>\n";
$myarray[]= 13+10; // the same as $myarray[3]= 13+10;
echo "number of elements =".count($myarray)."<BR>\n";
for ($i=0; $i < 4; $i++) {
echo $myarray[$i]," \n";
}
?>
ถ้าเราต้องการจะทราบจำนวนของข้อมูลที่มีอยู่ในอาร์เรย์เราจะใช้คำสั่ง count()
เทคนิคหนึ่งที่ใช้ในการสร้างอาร์เรย์ที่เก็บหลายๆข้อความหรือสตริงค์ คือ แทนที่เราจะกำหนดค่าของสมาชิก ในอาร์เรย์ทีละตัว เราจะสร้างได้โดยอัตโนมัติ โดยเก็บสตริงค์เหล่านั้นไว้ในสตริงค์เพียงอันเดียวโดยมีสัญลักษณ์ | เป็นตัวแยก และก็แล้วใช้ฟังก์ชันเป็นตัวแบ่งเพื่อสร้างอาร์เรย์อีกที ตามตัวอย่าง
ตัวอย่าง
<?
// create empty array
$a=array();
// define string containing color names separated by | (pipe)
$color_names="red|green|blue";
// create array from string
$a=explode("|",$color_names);
while ( $color=each($a) ) {
echo "$color[1]<BR>\n"; // note: $color[0] contains the index (0,1,2,...)
}
?>
ลองดูอีกตัวอย่างหนึ่งที่ใช้ฟังก์ชัน explode() สร้างอาร์เรย์โดยอัตโนมัตสำหรับใส่ไว้ใน FORM ในส่วนของ SELECT เป็นเมนูให้เลือก
ตัวอย่าง
<?
// create selection list from a given string
function str2select($str, $delim) {
$options = explode($delim,$str);
$num = count($options);
for( $i=0; $i < $num;$i++) {
echo "<option> $options[$i]</option>\n";
}
}
$select_str="10 บาท|20 บาท|30 บาท|40 บาท|50 บาท|100 บาท|200 บาท|500 บาท|1000 บาท";
?>
<FORM>
<SELECT NAME="testform">
<? str2select($select_str,"|"); ?>
</SELECT>
</FORM>
Top
การใช้อาร์เรย์สองมิติ
ถ้าเราต้องการจะใช้อาร์เรย์แบบสองมิติ (หรือมากกว่า) ก็ทำได้เช่นกัน คือชื่อตัวแปรแล้วตามด้วย [..][..] ตัวอย่างเช่น
ตัวอย่าง
<?
$dim = 3;
for ($row=0; $row <= $dim; $row++) {
for ($column=0; $column <= $dim; $column++) {
$myarray2[$row][$column] = 4*$row + $column;
echo $myarray2[$row][$column]," ";
}
echo "<BR>\n";
}
?>
สังเกตว่า สำหรับการใช้งานตัวแปรที่เป็นอาร์เรย์ เราไม่จำเป็นต้องแจ้งใช้ตัวแปรที่เป็นอาร์เรย์ พร้อมกำหนดขนาดก่อนการใช้งาน
Top
อาร์เรย์แบบเชื่อมโยงหรือ associative array
การเก็บข้อมูลในอาร์เรย์แบบนี้จะใช้กับข้อมูลที่จัดเก็บเป็นคู่ๆไป ซึ่งแตกต่างจากอาร์เรย์แบบแรกที่เราได้ทำความรู้จัก ตัวอย่างเช่น ใช้ทำ lookup table เช่น สมมุติว่า "red" ให้แทนค่า 0xff0000 "green" ให้แทนค่า 0x00ff00 และ "blue" 0x0000ff โดยเก็บไว้ในอาร์เรย์ชื่อ $color_table ตามตัวอย่างต่อไปนี้
ตัวอย่าง
$color_table["red"] = 0xff0000;
$color_table["green"] = 0x00ff00;
$color_table["blue"] = 0x0000ff;
$color_name= "red";
echo "value = ".$color_table[ $color_name]."<BR>\n";
หรืออีกรูปแบบหนึ่งที่เขียนสร้างอาร์เรย์ดังกล่าวได้ โดยใช้คำสั่ง array()
ตัวอย่าง
$color_table = array(
"red" => 0xff0000,
"green" => 0x00ff00,
"blue" => 0x0000ff
);
เราอาจจะสร้างอาร์เรย์เป็นสองมิติก็ได้ เช่น
ตัวอย่าง
<?
$countries = array (
"thailand" => array ( "zone" => "Asia", "D_NAME" => ".th"),
"malasia" => array ( "zone" => "Asia", "D_NAME" => ".my"),
"india" => array ( "zone" => "Asia", "D_NAME" => ".in"),
"holland" => array ( "zone" => "Europe", "D_NAME" => ".nl"),
"france" => array ( "zone" => "Europe", "D_NAME" => ".fr")
);
echo "domain name=".$countries[ "thailand"]["D_NAME"]."<BR>\n";
?>
การใช้คำสั่ง each และ list สำหรับ associative array
ถ้าเราต้องการจะเข้าถึงข้อมูลแต่ละคู่ที่ถูกเก็บอยู่ใน associative array เราอาจจะใช้วิธีเรียกผ่านฟังก์ชัน each() และ list() ตามตัวอย่างต่อไปนี้
ตัวอย่าง
<?
unset($a);
$a = array( "a" => 10, "b" => 20, "c" => 30 );
while (list($key,$value) = each($a)) {
echo "$key=$value <BR>\n";
}
?>
ฟังก์ชัน each() จะอ่านข้อมูลทีละคู่จากอาร์เรย์แบบเชื่อมโยงมาแล้วส่งไปยังฟังก์ชัน list() ซึ่งจะทำหน้าที่แยกเก็บ ซึ่งในกรณีก็คือ เก็บไว้ในตัวแปร $key และ $value หลังจากนั้น เราก็สามารถนำค่าของตัวแปร ไปใช้งานตามที่ต้องการได้
Top
การนิยามและสร้างฟังก์ชันโดยผู้ใช้ (User-defined functions)
ถ้าเราต้องการสร้างฟังก์ชันขึ้นมาใช้งานเองก็ทำได้ โดยเฉพาะอย่างยิ่งในกรณีที่เราต้องการจะ ใช้ชุดคำสั่งเหล่านั้นบ่อยครั้ง เราก็จัดเก็บเป็นฟังก์ชัน เพื่อให้เรียกใช้ได้สะดวก และยังช่วยให้การเขียนโปรแกรมง่ายขึ้นด้วย
การสร้างฟังก์ชันขึ้นใช้เองทำได้โดย ใช้โครงสร้าง
รูปแบบ
function function_name ($arg1, $arg2, .., $argN) {
....
}
และฟังก์ชันจะให้ค่ากลับคืนหรือไม่ก็ได้ ถ้าต้องการให้ค่ากลับคืนจากการทำงานของฟังก์ชัน ก็จะใช้คำสั่ง return นอกจากนั้น PHP ยังสนับสนุน default parameter ด้วย
ตัวอย่างเช่น การหาค่าสัมบรูณ์ของตัวเลข
ตัวอย่าง
<?
function myabs ($x) {
if ($x < 0)
return -$x;
}
echo myabs(-6),"<BR>\n";
echo myabs(-4+2.034),"<BR>\n";
?>
การหาค่าดังกล่าวของตัวเลขใดๆ เราสามารถใช้ฟังก์ชัน abs() หรือเราเขียนขึ้นเองก็ได้ตามตัวอย่างข้างบน
การหาค่ามากกว่าและน้อยกว่าจากตัวเลขสองตัวและสลับที่กัน สมมุติว่า เรามีตัวแปรอยู่สองตัว และเราต้องการจะตรวจดูว่า ตัวแปรตัวแรกมีค่าน้อยกว่าตัวแปรอีกตัวหรือไม่ ถ้าไม่ ก็ให้สลับที่กัน ปัญหานี้เราสามารถแก้ไขได้โดยเขียนฟังก์ชันดังนี้
ตัวอย่าง
<?
function minmax (&$a,&$b) {
if ($a > $b) {
$t=$a; $a=$b; $b=$t;
}
}
$x=10;
$y=3;
echo "x=",$x,",y=",$y,"<BR>\n";
minmax($x,$y);
echo "x=",$x,",y=",$y,"<BR>\n";
?>
ฟังก์ชัน minmax() เป็นตัวอย่างของฟังก์ชันที่ใช้หลักการของ call-by-reference โปรดสังเกตที่เครื่องหมาย & ที่วางอยู่หน้าตัวแปรที่เป็นอาร์กิวเมนต์ของฟังก์ชัน การเรียกใช้ฟังก์ชันแบบ call-by-reference ช่วยให้เราสามารถผ่านตัวแปรไปยังฟังก์ชัน และให้ฟังก์ชันสามารถเปลี่ยนแปลงแก้ไขค่าของตัวแปรนั้นได้
Top
การสลับค่าของตัวแปรสองตัว swap()
ถ้าเราต้องการสลับค่าระหว่างสองตัวแปร เราก็เขียนฟังก์ชัน swap() ขึ้นมา
ตัวอย่าง
<?
function swap(&$a, &$b) {
$t = $a;
$a = $b;
$b = $t;
}
$x=10;
$y=3;
echo "x=",$x,",y=",$y,"\n";
swap($x,$y);
echo "x=",$x,",y=",$y,"\n";
?>
ตัวอย่างข้างบน ก็แสดงให้เห็นวิธีการใช้ call-by-reference อีกเช่นกัน
มีข้อสังเกตอยู่ว่า การใช้ call-by-reference ไม่จำเป็นต้องทำตอนนิยามฟังก์ชันเท่านั้น แต่อาจจะทำตอนผ่านตัวแปรเมื่อเรียกใช้งานจริง ตัวอย่างเช่น
ตัวอย่าง
<?
function swap($a, $b) {
$t = $a;
$a = $b;
$b = $t;
}
$x=10;
$y=3;
echo "x=",$x,",y=",$y,"\n";
swap(&$x, &$y);
echo "x=",$x,",y=",$y,"\n";
?>
จากตัวอย่างนี้ เราแก้ไขฟังก์ชัน swap() ทำให้ไม่สนับสนุน call-by-reference ดังนั้นเพื่อจะใช้งานได้อย่างถูกต้อง เราก็จะต้องใช้ reference ของตัวแปรเป็นอาร์กิวเมนต์ของฟังก์ชัน ในเวลาที่เรียกใช้ ซึ่งก็คือ swap(&$x,&$y) ถ้าเราไม่ทำอย่างนี้ เช่น เขียนว่า swap($x,$y) ก็จะไม่มีการสลับค่าของตัวแปรทั้งสอง เนื่องจากว่า เมื่ออยู่ภายในฟังก์ชัน swap() แล้ว เราไม่สามารถเปลี่ยนแปลงค่าของตัวแปรเหล่านั้นได้ คืออ่านได้ แต่ไม่สามารถกำหนดค่าใหม่ได้
Top
การใช้ฟังก์ชันเพื่อสร้างตัวเลขแบบสุ่ม
การใช้ฟังก์ชันเพื่อสร้างตัวเลขแบบสุ่ม หรือ random number generator จะคล้ายกับของภาษาซี คือ เริ่มต้นด้วย srand () โดยจะต้องผ่านค่าที่เรียกว่า seed ซึ่งเป็นเลขจำนวนเต็มใดๆก็ได้ก่อน โดยทั่วไปจะใช้ค่าของเวลาในหน่วยวินาที หรือ Time Stamp ซึ่งสามารถอ่านได้จากฟังก์ชัน date("s") (s หมายถึง second หรือหน่วยวินาที) โดยผ่านค่านี้เป็นค่าของ seed จากนั้นจึงค่อยเรียกใช้ rand()
ตัวอย่าง
<?
srand( date("s") );
for ($i=0; $i < 10; $i++) {
$x = rand() % 10;
echo $x," ";
}
?>
คำสั่งนี้จะสร้างตัวเลขโดยการสุ่มเลือกเป็นจำนวน 10 ตัวเลข และพิมพ์ออกทางเอาพุต
ตัวอย่าง
<?
function randInt($low,$high) {
srand ( date("s") );
$range = $high - $low;
$num = (rand() % $range) + $low;
return $num;
}
?>
ตัวอย่างนี้จะสร้างตัวเลขโดยสุ่มที่อยู่ระหว่างเลขจำนวนเต็มสองค่า และเงื่อนไขของการใช้ฟังก์ชันนี้คือ $low จะต้องมีค่าน้อยกว่า $high และทั้งสองต้องเป็นเลขจำนวนเต็ม
ตัวอย่าง
<?
function randStr($len) {
srand ( date("s") );
for ($i=0; $i < $len; $i++) {
$ret_str .= chr( (rand() % 26)+97 );
}
return $ret_str;
}
echo randStr(40);
?>
ตัวอย่างนี้จะสร้างสตริงค์แบบสุ่มที่มีความยาวตามที่กำหนดและสร้างขึ้นจากตัวอักขระภาษาอังกฤษ
Top
การสร้างฟังก์ชันแบบเรียกตัวเอง (recursive function)
การหาค่าแฟลทอเรียล n!
ตัวอย่าง
<?
function factorial ($n) {
if ( ($n == 0) || ($n == 1) )
return 1;
else
return $n*factorial($n-1);
}
echo factorial(4);
?>
เงื่อนไขก็ใช้ฟังก์ชัน factorial() จากตัวอย่างข้างบน คือ $n จะต้องเป็นตัวแปรที่เก็บค่าที่เป็นเลขจำนวนเต็ม และไม่เป็นลบ ถ้าเราต้องการจะเขียนฟังก์ชันให้มีความปลอดภัยในการใช้งาน เราก็อาจจะเพิ่มเงื่อนไข เพื่อตรวจเช็คดูก่อนว่า ผู้ใช้ผ่านค่าของตัวแปรที่ตรงตามต้องการหรือไม่ เช่น ไม่ผ่านค่าที่เป็นสตริงค์ หรือเป็นเลขทศนิยม หรือค่าที่เป็นลบ เป็นต้น
การค้นหาข้อมูลแบบ Binary Search ในอาร์เรย์ที่มีการเรียงข้อมูลจากน้อยไปมาก
ตัวอย่าง
<?
function binSearch(&$key,&$array, $left, $right) {
$mid = ceil( ($left + $right) / 2 );
if ($left > $right)
return -1;
if ($array[$mid] == $key)
return $mid;
else if ($key < $array[$mid])
return binSearch($key, $array, $left, $mid-1); // recursive call
else
return binSearch($key, $array, $mid+1, $right); // recursive call
}
$num=100;
$key = randInt(0, $num);
for($i=0; $i < $num; $i++) {
$sorted_array[$i] = $i+1;
}
echo binSearch(13, $sorted_array, 0, $num);
?>
การสร้างสตริงค์แบบสุ่มอีกแบบหนึ่งซึ่งอาจจะนำไปใช้ในการสร้าง one-time password ( OTP)
ตัวอย่าง
<?
function randomToken($len) {
srand( date("s") );
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$chars.= "1234567890!@#$%^&*()";
$ret_str = "";
$num = strlen($chars);
for($i=0; $i < $len; $i++) {
$ret_str.= $chars[rand()%$num];
}
return $ret_str;
}
echo randomToken(13)," ";
?>
หมายเหตุ: การกำหนดค่า seed สำหรับฟังก์ชัน srand() นอกจะใช้ date("s") เป็นตัวกำหนดค่าแล้ว เราอาจจะใช้ฟังก์ชันอื่นก็ได้ เช่น srand((double)microtime()*1000000);
Top
การใช้ตัวแปรแบบ global ภายในฟังก์ชัน
บางครั้งเราไม่ต้องการที่จะผ่านตัวแปรเป็นอาร์กิวเมนต์ของฟังก์ชัน เพื่อนำไปใช้ภายในฟังก์ชันเหล่านั้น ก็จะทำได้โดยการแจ้งใช้ตัวแปรที่มีชื่อเหมือนตัวแปรภายนอกที่เราต้องการใช้ ให้เป็น global หรือใช้ผ่านตัวแปรที่เป็นอาร์เรย์ของ PHP ที่มีชื่อว่า $GLOBALS ดังตัวอย่างต่อไปนี้
ตัวอย่าง
<?
$a = 10;
$b = 20;
function getMin ( ) {
global $a, $b;
if ($a < $b)
return $a;
else
return $b;
}
function getMin2 () {
if ($GLOBALS["a"] < $GLOBAL["b"])
return $GLOBALS["a"];
else
return $GLOBALS["b"];
}
echo getMin()."<BR>\n";
echo getMin2()."<BR>\n";
?>
ในกรณีนี้เราต้องการจะใช้ตัวแปร $a และ $b ซึ่งอยู่นอกฟังก์ชัน getMin() เพื่อเช็คดูว่า ค่าของตัวแปรใดมีค่าน้อยกว่ากัน ถ้าเราไม่แจ้งใช้ global $a, $b; ตามตัวอย่างแล้ว $a และ $b จะกลายเป็นตัวแปรภายในแม้ว่าจะชื่อเหมือนกันตัวแปรภายนอกที่มีอยู่แล้วก็ตาม ทำให้ได้ผลการทำงานไม่ถูกต้องตามที่ต้องการ
ฟังก์ชัน getMin() อีกรูปแบบหนึ่ง โดยไม่ใช้ตัวแปรแบบ global ภายในฟังก์ชัน และใช้วิธีผ่านค่าแทน
ตัวอย่าง
<?
$a = 10;
$b = 20;
function getMin ($a, $b) {
if ($a < $b)
return $a;
else
return $b;
}
echo getMin($a, $b)."<BR>\n";
?>
Top
การตัวแปรแบบ static ภายในฟังก์ชัน
สมมุติว่า เราต้องการจะใช้ตัวแปรภายในฟังก์ชัน และสามารถเก็บค่าไว้ได้ตลอดเวลา โดยไม่สูญหายไปทุกครั้งที่มีการเรียกใช้ฟังก์ชัน ในกรณีนี้เราจะแจ้งใช้ตัวแปรให้เป็นแบบ static ตามตัวอย่างต่อไปนี้
ตัวอย่าง
function MyFunc() {
static $num_func_calls = 0;
echo "my function\n";
return ++$num_func_calls;
}
ทุกครั้งที่มีการเรียกใช้ฟังก์ชันดังกล่าว ตัวแปรชื่อ $num_func_calls ซึ่งมีค่าเริ่มต้นเป็นศูนย์ในตอนแรก จะเพิ่มค่าที่เก็บขึ้นทีละหนึ่ง
Top
การผ่านค่ากลับคืนมากกว่าหนึ่งจากฟังก์ชัน
โดยปรกติแล้วเราไม่สามารถผ่านค่ากลับคืนจากฟังก์ชันได้มากกว่าหนึ่ง แต่อย่างไรก็ตาม ยังมีวิธีการหนึ่งที่ช่วยแก้ปัญหาดังกล่าวได้ วิธีนี้คือ เก็บค่าต่างๆที่ต้องการจะใช้เป็นค่ากลับคืนไว้ใน array แล้วใช้ array นั้นเป็นค่ากลับคืน และผู้เรียกใช้ฟังก์ชันสามารถใช้ฟังก์ชัน list() อ่านค่าเหล่านั้นได้ ตัวอย่างเช่น
ตัวอย่าง
<?
function foobar() {
return array ("foo", "bar", 0xff);
}
list ($foo, $bar, $num) = foobar();
echo "$foo $bar $num <BR>\n";
?>
จากตัวอย่าง ฟังก์ชัน foobar() จะให้ค่ากลับคืนเป็น array ประกอบด้วยสามสมาชิก ค่าที่ได้จากฟังก์ชันนี้ก็จะส่งไปยังฟังก์ชัน list() เพื่อให้เก็บแยกลงในตัวแปรตามชื่อที่กำหนดคือ $foo, $bar และ $num ตามลำดับ
การใช้อาร์เรย์
อาร์เรย์ในภาษา PHP นั้นจะแตกต่างจากอาร์เรย์ในภาษาซีหรือจาวาตรงที่ว่า อาร์เรย์ในภาษา PHP มีขนาดที่เปลี่ยนแปลงได้ หรือจะเรียกว่า dynamic array หรือ vector (สำหรับอาร์เรย์มิติเดียว) เริ่มต้นอาจจะแจ้งใช้ตัวแปรแบบอาร์เรย์ พร้อมเจาะจงขนาดเริ่มแรก เช่น มีขนาดเป็นศูนย์ก็ได้
ตัวอย่าง
$myarray[]=3;
$myarray[]=1.1;
$myarray[]="abc";
แต่เมื่อใช้อาร์เรย์ไป ขนาดของมันจะปรับเปลี่ยนได้ คือขยายจำนวนข้อมูลที่เก็บอยู่ภายในอาร์เรย์ ตามจำนวนข้อมูลที่เราใส่เพิ่มเข้าไป จากตัวอย่างข้างบน ในกรณีที่เรามิได้กำหนดเลขดัชนี (index) ก็หมายความว่า จะมีการขยายขนาดของอาร์เรย์เพิ่มขึ้นอีกหนึ่งโดยอัตโนมัติ ทุกครั้งที่เราใส่ข้อมูลที่อยู่ทางขวา และค่าที่เรากำหนดจากทางขวามือ และจะเก็บไว้ในที่ใหม่ของอาร์เรย์ เราไม่ต้องคำนึงถึงเรื่องการจอง หรือ ปลดปล่อยหน่วยความจำของอาร์เรย์ เหมือนอย่างในกรณีของอาร์เรย์ แบบไดนามิกในภาษาซี
นอกจากนั้นข้อมูลแต่ละตัวในอาร์เรย์ไม่จำเป็น ต้องเป็นข้อมูลชนิดเดียวกัน เช่น อาจจะมีทั้งจำนวนเต็ม เลขทศนิยม และข้อความ ปะปนกันไป ตัวอย่างเช่น
ตัวอย่าง
<?
$myarray[0] = 1;
echo "number of elements =".count($myarray)."<BR>\n";
$myarray[1] = "abc";
echo "number of elements =".count($myarray)."<BR>\n";
$myarray[2] = 1.3;
echo "number of elements =".count($myarray)."<BR>\n";
$myarray[]= 13+10; // the same as $myarray[3]= 13+10;
echo "number of elements =".count($myarray)."<BR>\n";
for ($i=0; $i < 4; $i++) {
echo $myarray[$i]," \n";
}
?>
ถ้าเราต้องการจะทราบจำนวนของข้อมูลที่มีอยู่ในอาร์เรย์เราจะใช้คำสั่ง count()
เทคนิคหนึ่งที่ใช้ในการสร้างอาร์เรย์ที่เก็บหลายๆข้อความหรือสตริงค์ คือ แทนที่เราจะกำหนดค่าของสมาชิก ในอาร์เรย์ทีละตัว เราจะสร้างได้โดยอัตโนมัติ โดยเก็บสตริงค์เหล่านั้นไว้ในสตริงค์เพียงอันเดียวโดยมีสัญลักษณ์ | เป็นตัวแยก และก็แล้วใช้ฟังก์ชันเป็นตัวแบ่งเพื่อสร้างอาร์เรย์อีกที ตามตัวอย่าง
ตัวอย่าง
<?
// create empty array
$a=array();
// define string containing color names separated by | (pipe)
$color_names="red|green|blue";
// create array from string
$a=explode("|",$color_names);
while ( $color=each($a) ) {
echo "$color[1]<BR>\n"; // note: $color[0] contains the index (0,1,2,...)
}
?>
ลองดูอีกตัวอย่างหนึ่งที่ใช้ฟังก์ชัน explode() สร้างอาร์เรย์โดยอัตโนมัตสำหรับใส่ไว้ใน FORM ในส่วนของ SELECT เป็นเมนูให้เลือก
ตัวอย่าง
<?
// create selection list from a given string
function str2select($str, $delim) {
$options = explode($delim,$str);
$num = count($options);
for( $i=0; $i < $num;$i++) {
echo "<option> $options[$i]</option>\n";
}
}
$select_str="10 บาท|20 บาท|30 บาท|40 บาท|50 บาท|100 บาท|200 บาท|500 บาท|1000 บาท";
?>
<FORM>
<SELECT NAME="testform">
<? str2select($select_str,"|"); ?>
</SELECT>
</FORM>
Top
การใช้อาร์เรย์สองมิติ
ถ้าเราต้องการจะใช้อาร์เรย์แบบสองมิติ (หรือมากกว่า) ก็ทำได้เช่นกัน คือชื่อตัวแปรแล้วตามด้วย [..][..] ตัวอย่างเช่น
ตัวอย่าง
<?
$dim = 3;
for ($row=0; $row <= $dim; $row++) {
for ($column=0; $column <= $dim; $column++) {
$myarray2[$row][$column] = 4*$row + $column;
echo $myarray2[$row][$column]," ";
}
echo "<BR>\n";
}
?>
สังเกตว่า สำหรับการใช้งานตัวแปรที่เป็นอาร์เรย์ เราไม่จำเป็นต้องแจ้งใช้ตัวแปรที่เป็นอาร์เรย์ พร้อมกำหนดขนาดก่อนการใช้งาน
Top
อาร์เรย์แบบเชื่อมโยงหรือ associative array
การเก็บข้อมูลในอาร์เรย์แบบนี้จะใช้กับข้อมูลที่จัดเก็บเป็นคู่ๆไป ซึ่งแตกต่างจากอาร์เรย์แบบแรกที่เราได้ทำความรู้จัก ตัวอย่างเช่น ใช้ทำ lookup table เช่น สมมุติว่า "red" ให้แทนค่า 0xff0000 "green" ให้แทนค่า 0x00ff00 และ "blue" 0x0000ff โดยเก็บไว้ในอาร์เรย์ชื่อ $color_table ตามตัวอย่างต่อไปนี้
ตัวอย่าง
$color_table["red"] = 0xff0000;
$color_table["green"] = 0x00ff00;
$color_table["blue"] = 0x0000ff;
$color_name= "red";
echo "value = ".$color_table[ $color_name]."<BR>\n";
หรืออีกรูปแบบหนึ่งที่เขียนสร้างอาร์เรย์ดังกล่าวได้ โดยใช้คำสั่ง array()
ตัวอย่าง
$color_table = array(
"red" => 0xff0000,
"green" => 0x00ff00,
"blue" => 0x0000ff
);
เราอาจจะสร้างอาร์เรย์เป็นสองมิติก็ได้ เช่น
ตัวอย่าง
<?
$countries = array (
"thailand" => array ( "zone" => "Asia", "D_NAME" => ".th"),
"malasia" => array ( "zone" => "Asia", "D_NAME" => ".my"),
"india" => array ( "zone" => "Asia", "D_NAME" => ".in"),
"holland" => array ( "zone" => "Europe", "D_NAME" => ".nl"),
"france" => array ( "zone" => "Europe", "D_NAME" => ".fr")
);
echo "domain name=".$countries[ "thailand"]["D_NAME"]."<BR>\n";
?>
การใช้คำสั่ง each และ list สำหรับ associative array
ถ้าเราต้องการจะเข้าถึงข้อมูลแต่ละคู่ที่ถูกเก็บอยู่ใน associative array เราอาจจะใช้วิธีเรียกผ่านฟังก์ชัน each() และ list() ตามตัวอย่างต่อไปนี้
ตัวอย่าง
<?
unset($a);
$a = array( "a" => 10, "b" => 20, "c" => 30 );
while (list($key,$value) = each($a)) {
echo "$key=$value <BR>\n";
}
?>
ฟังก์ชัน each() จะอ่านข้อมูลทีละคู่จากอาร์เรย์แบบเชื่อมโยงมาแล้วส่งไปยังฟังก์ชัน list() ซึ่งจะทำหน้าที่แยกเก็บ ซึ่งในกรณีก็คือ เก็บไว้ในตัวแปร $key และ $value หลังจากนั้น เราก็สามารถนำค่าของตัวแปร ไปใช้งานตามที่ต้องการได้
Top
การนิยามและสร้างฟังก์ชันโดยผู้ใช้ (User-defined functions)
ถ้าเราต้องการสร้างฟังก์ชันขึ้นมาใช้งานเองก็ทำได้ โดยเฉพาะอย่างยิ่งในกรณีที่เราต้องการจะ ใช้ชุดคำสั่งเหล่านั้นบ่อยครั้ง เราก็จัดเก็บเป็นฟังก์ชัน เพื่อให้เรียกใช้ได้สะดวก และยังช่วยให้การเขียนโปรแกรมง่ายขึ้นด้วย
การสร้างฟังก์ชันขึ้นใช้เองทำได้โดย ใช้โครงสร้าง
รูปแบบ
function function_name ($arg1, $arg2, .., $argN) {
....
}
และฟังก์ชันจะให้ค่ากลับคืนหรือไม่ก็ได้ ถ้าต้องการให้ค่ากลับคืนจากการทำงานของฟังก์ชัน ก็จะใช้คำสั่ง return นอกจากนั้น PHP ยังสนับสนุน default parameter ด้วย
ตัวอย่างเช่น การหาค่าสัมบรูณ์ของตัวเลข
ตัวอย่าง
<?
function myabs ($x) {
if ($x < 0)
return -$x;
}
echo myabs(-6),"<BR>\n";
echo myabs(-4+2.034),"<BR>\n";
?>
การหาค่าดังกล่าวของตัวเลขใดๆ เราสามารถใช้ฟังก์ชัน abs() หรือเราเขียนขึ้นเองก็ได้ตามตัวอย่างข้างบน
การหาค่ามากกว่าและน้อยกว่าจากตัวเลขสองตัวและสลับที่กัน สมมุติว่า เรามีตัวแปรอยู่สองตัว และเราต้องการจะตรวจดูว่า ตัวแปรตัวแรกมีค่าน้อยกว่าตัวแปรอีกตัวหรือไม่ ถ้าไม่ ก็ให้สลับที่กัน ปัญหานี้เราสามารถแก้ไขได้โดยเขียนฟังก์ชันดังนี้
ตัวอย่าง
<?
function minmax (&$a,&$b) {
if ($a > $b) {
$t=$a; $a=$b; $b=$t;
}
}
$x=10;
$y=3;
echo "x=",$x,",y=",$y,"<BR>\n";
minmax($x,$y);
echo "x=",$x,",y=",$y,"<BR>\n";
?>
ฟังก์ชัน minmax() เป็นตัวอย่างของฟังก์ชันที่ใช้หลักการของ call-by-reference โปรดสังเกตที่เครื่องหมาย & ที่วางอยู่หน้าตัวแปรที่เป็นอาร์กิวเมนต์ของฟังก์ชัน การเรียกใช้ฟังก์ชันแบบ call-by-reference ช่วยให้เราสามารถผ่านตัวแปรไปยังฟังก์ชัน และให้ฟังก์ชันสามารถเปลี่ยนแปลงแก้ไขค่าของตัวแปรนั้นได้
Top
การสลับค่าของตัวแปรสองตัว swap()
ถ้าเราต้องการสลับค่าระหว่างสองตัวแปร เราก็เขียนฟังก์ชัน swap() ขึ้นมา
ตัวอย่าง
<?
function swap(&$a, &$b) {
$t = $a;
$a = $b;
$b = $t;
}
$x=10;
$y=3;
echo "x=",$x,",y=",$y,"\n";
swap($x,$y);
echo "x=",$x,",y=",$y,"\n";
?>
ตัวอย่างข้างบน ก็แสดงให้เห็นวิธีการใช้ call-by-reference อีกเช่นกัน
มีข้อสังเกตอยู่ว่า การใช้ call-by-reference ไม่จำเป็นต้องทำตอนนิยามฟังก์ชันเท่านั้น แต่อาจจะทำตอนผ่านตัวแปรเมื่อเรียกใช้งานจริง ตัวอย่างเช่น
ตัวอย่าง
<?
function swap($a, $b) {
$t = $a;
$a = $b;
$b = $t;
}
$x=10;
$y=3;
echo "x=",$x,",y=",$y,"\n";
swap(&$x, &$y);
echo "x=",$x,",y=",$y,"\n";
?>
จากตัวอย่างนี้ เราแก้ไขฟังก์ชัน swap() ทำให้ไม่สนับสนุน call-by-reference ดังนั้นเพื่อจะใช้งานได้อย่างถูกต้อง เราก็จะต้องใช้ reference ของตัวแปรเป็นอาร์กิวเมนต์ของฟังก์ชัน ในเวลาที่เรียกใช้ ซึ่งก็คือ swap(&$x,&$y) ถ้าเราไม่ทำอย่างนี้ เช่น เขียนว่า swap($x,$y) ก็จะไม่มีการสลับค่าของตัวแปรทั้งสอง เนื่องจากว่า เมื่ออยู่ภายในฟังก์ชัน swap() แล้ว เราไม่สามารถเปลี่ยนแปลงค่าของตัวแปรเหล่านั้นได้ คืออ่านได้ แต่ไม่สามารถกำหนดค่าใหม่ได้
Top
การใช้ฟังก์ชันเพื่อสร้างตัวเลขแบบสุ่ม
การใช้ฟังก์ชันเพื่อสร้างตัวเลขแบบสุ่ม หรือ random number generator จะคล้ายกับของภาษาซี คือ เริ่มต้นด้วย srand () โดยจะต้องผ่านค่าที่เรียกว่า seed ซึ่งเป็นเลขจำนวนเต็มใดๆก็ได้ก่อน โดยทั่วไปจะใช้ค่าของเวลาในหน่วยวินาที หรือ Time Stamp ซึ่งสามารถอ่านได้จากฟังก์ชัน date("s") (s หมายถึง second หรือหน่วยวินาที) โดยผ่านค่านี้เป็นค่าของ seed จากนั้นจึงค่อยเรียกใช้ rand()
ตัวอย่าง
<?
srand( date("s") );
for ($i=0; $i < 10; $i++) {
$x = rand() % 10;
echo $x," ";
}
?>
คำสั่งนี้จะสร้างตัวเลขโดยการสุ่มเลือกเป็นจำนวน 10 ตัวเลข และพิมพ์ออกทางเอาพุต
ตัวอย่าง
<?
function randInt($low,$high) {
srand ( date("s") );
$range = $high - $low;
$num = (rand() % $range) + $low;
return $num;
}
?>
ตัวอย่างนี้จะสร้างตัวเลขโดยสุ่มที่อยู่ระหว่างเลขจำนวนเต็มสองค่า และเงื่อนไขของการใช้ฟังก์ชันนี้คือ $low จะต้องมีค่าน้อยกว่า $high และทั้งสองต้องเป็นเลขจำนวนเต็ม
ตัวอย่าง
<?
function randStr($len) {
srand ( date("s") );
for ($i=0; $i < $len; $i++) {
$ret_str .= chr( (rand() % 26)+97 );
}
return $ret_str;
}
echo randStr(40);
?>
ตัวอย่างนี้จะสร้างสตริงค์แบบสุ่มที่มีความยาวตามที่กำหนดและสร้างขึ้นจากตัวอักขระภาษาอังกฤษ
Top
การสร้างฟังก์ชันแบบเรียกตัวเอง (recursive function)
การหาค่าแฟลทอเรียล n!
ตัวอย่าง
<?
function factorial ($n) {
if ( ($n == 0) || ($n == 1) )
return 1;
else
return $n*factorial($n-1);
}
echo factorial(4);
?>
เงื่อนไขก็ใช้ฟังก์ชัน factorial() จากตัวอย่างข้างบน คือ $n จะต้องเป็นตัวแปรที่เก็บค่าที่เป็นเลขจำนวนเต็ม และไม่เป็นลบ ถ้าเราต้องการจะเขียนฟังก์ชันให้มีความปลอดภัยในการใช้งาน เราก็อาจจะเพิ่มเงื่อนไข เพื่อตรวจเช็คดูก่อนว่า ผู้ใช้ผ่านค่าของตัวแปรที่ตรงตามต้องการหรือไม่ เช่น ไม่ผ่านค่าที่เป็นสตริงค์ หรือเป็นเลขทศนิยม หรือค่าที่เป็นลบ เป็นต้น
การค้นหาข้อมูลแบบ Binary Search ในอาร์เรย์ที่มีการเรียงข้อมูลจากน้อยไปมาก
ตัวอย่าง
<?
function binSearch(&$key,&$array, $left, $right) {
$mid = ceil( ($left + $right) / 2 );
if ($left > $right)
return -1;
if ($array[$mid] == $key)
return $mid;
else if ($key < $array[$mid])
return binSearch($key, $array, $left, $mid-1); // recursive call
else
return binSearch($key, $array, $mid+1, $right); // recursive call
}
$num=100;
$key = randInt(0, $num);
for($i=0; $i < $num; $i++) {
$sorted_array[$i] = $i+1;
}
echo binSearch(13, $sorted_array, 0, $num);
?>
การสร้างสตริงค์แบบสุ่มอีกแบบหนึ่งซึ่งอาจจะนำไปใช้ในการสร้าง one-time password ( OTP)
ตัวอย่าง
<?
function randomToken($len) {
srand( date("s") );
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$chars.= "1234567890!@#$%^&*()";
$ret_str = "";
$num = strlen($chars);
for($i=0; $i < $len; $i++) {
$ret_str.= $chars[rand()%$num];
}
return $ret_str;
}
echo randomToken(13)," ";
?>
หมายเหตุ: การกำหนดค่า seed สำหรับฟังก์ชัน srand() นอกจะใช้ date("s") เป็นตัวกำหนดค่าแล้ว เราอาจจะใช้ฟังก์ชันอื่นก็ได้ เช่น srand((double)microtime()*1000000);
Top
การใช้ตัวแปรแบบ global ภายในฟังก์ชัน
บางครั้งเราไม่ต้องการที่จะผ่านตัวแปรเป็นอาร์กิวเมนต์ของฟังก์ชัน เพื่อนำไปใช้ภายในฟังก์ชันเหล่านั้น ก็จะทำได้โดยการแจ้งใช้ตัวแปรที่มีชื่อเหมือนตัวแปรภายนอกที่เราต้องการใช้ ให้เป็น global หรือใช้ผ่านตัวแปรที่เป็นอาร์เรย์ของ PHP ที่มีชื่อว่า $GLOBALS ดังตัวอย่างต่อไปนี้
ตัวอย่าง
<?
$a = 10;
$b = 20;
function getMin ( ) {
global $a, $b;
if ($a < $b)
return $a;
else
return $b;
}
function getMin2 () {
if ($GLOBALS["a"] < $GLOBAL["b"])
return $GLOBALS["a"];
else
return $GLOBALS["b"];
}
echo getMin()."<BR>\n";
echo getMin2()."<BR>\n";
?>
ในกรณีนี้เราต้องการจะใช้ตัวแปร $a และ $b ซึ่งอยู่นอกฟังก์ชัน getMin() เพื่อเช็คดูว่า ค่าของตัวแปรใดมีค่าน้อยกว่ากัน ถ้าเราไม่แจ้งใช้ global $a, $b; ตามตัวอย่างแล้ว $a และ $b จะกลายเป็นตัวแปรภายในแม้ว่าจะชื่อเหมือนกันตัวแปรภายนอกที่มีอยู่แล้วก็ตาม ทำให้ได้ผลการทำงานไม่ถูกต้องตามที่ต้องการ
ฟังก์ชัน getMin() อีกรูปแบบหนึ่ง โดยไม่ใช้ตัวแปรแบบ global ภายในฟังก์ชัน และใช้วิธีผ่านค่าแทน
ตัวอย่าง
<?
$a = 10;
$b = 20;
function getMin ($a, $b) {
if ($a < $b)
return $a;
else
return $b;
}
echo getMin($a, $b)."<BR>\n";
?>
Top
การตัวแปรแบบ static ภายในฟังก์ชัน
สมมุติว่า เราต้องการจะใช้ตัวแปรภายในฟังก์ชัน และสามารถเก็บค่าไว้ได้ตลอดเวลา โดยไม่สูญหายไปทุกครั้งที่มีการเรียกใช้ฟังก์ชัน ในกรณีนี้เราจะแจ้งใช้ตัวแปรให้เป็นแบบ static ตามตัวอย่างต่อไปนี้
ตัวอย่าง
function MyFunc() {
static $num_func_calls = 0;
echo "my function\n";
return ++$num_func_calls;
}
ทุกครั้งที่มีการเรียกใช้ฟังก์ชันดังกล่าว ตัวแปรชื่อ $num_func_calls ซึ่งมีค่าเริ่มต้นเป็นศูนย์ในตอนแรก จะเพิ่มค่าที่เก็บขึ้นทีละหนึ่ง
Top
การผ่านค่ากลับคืนมากกว่าหนึ่งจากฟังก์ชัน
โดยปรกติแล้วเราไม่สามารถผ่านค่ากลับคืนจากฟังก์ชันได้มากกว่าหนึ่ง แต่อย่างไรก็ตาม ยังมีวิธีการหนึ่งที่ช่วยแก้ปัญหาดังกล่าวได้ วิธีนี้คือ เก็บค่าต่างๆที่ต้องการจะใช้เป็นค่ากลับคืนไว้ใน array แล้วใช้ array นั้นเป็นค่ากลับคืน และผู้เรียกใช้ฟังก์ชันสามารถใช้ฟังก์ชัน list() อ่านค่าเหล่านั้นได้ ตัวอย่างเช่น
ตัวอย่าง
<?
function foobar() {
return array ("foo", "bar", 0xff);
}
list ($foo, $bar, $num) = foobar();
echo "$foo $bar $num <BR>\n";
?>
จากตัวอย่าง ฟังก์ชัน foobar() จะให้ค่ากลับคืนเป็น array ประกอบด้วยสามสมาชิก ค่าที่ได้จากฟังก์ชันนี้ก็จะส่งไปยังฟังก์ชัน list() เพื่อให้เก็บแยกลงในตัวแปรตามชื่อที่กำหนดคือ $foo, $bar และ $num ตามลำดับ
ตรวจสอบ IP Address และ Hostname อย่างผู้รู้จริง
ในบางครั้งเราต้องการตรวจสอบว่าผู้ที่เข้ามาเยี่ยมชมเว็บไซต์ของเราหรือผู้ที่มาโพสข้อความในเวบบอร์ดของเรานั้นมาจากที่ใด
ซึ่งวิธีการที่ง่ายและเป็นที่นิยมใช้กันก็คือการตรวจสอบ IP Address ของเครื่องผู้ชมที่เปิดดูเว็บไซต์ของเรานั่นเอง
โดยหลักการในการตรวจสอบ IP Address นี้ทำได้โดยการตรวจจาก Predefined Variable
ของเว็บเซิร์ฟเวอร์ที่เราใช้งานอยู่ ซึ่ง Predefined Variable นี้จะเป็นตัวแปรที่สร้างขึ้นมาโดยอัติโนมัติทุกครั้งที่มีการรันสคริปของ
PHP
Predefine Variable ที่เก็บค่า IP Address ของผู้ใช้ไว้ก็คือ REMOTE_ADDR ดังนั้นเมื่อเราต้องการตรวจสอบค่า IP Address ของผู้ใข้ เราจะใช้ฟังก์ชัน getenv ในการดึงเอาค่าของ Predefined Variable นี้มาเก็บไว้ในตัวแปร $ip ดังนี้
<?
$ip=getenv(REMOTE_ADDR);
print "Your IP Address is $ip";
?>
ผลลัพธ์ที่ได้คือ
Your IP Address is 127.0.0.1
จะเห็นได้ว่าโปรแกรมนี้สามารถตรวจสอบ IP Address ของผู้ใช้ได้ในระดับหนึ่ง แต่จะมีปัญหาในกรณีที่เครื่องผู้ใช้ได้ทำการปรับแต่งเครื่องให้ใช้ Proxy Server ซึ่งแทนที่เราจะได้ IP Address ของเครื่องผู้ใช้ เราก็จะได้รับ IP Address ของ Proxy Server แทน หรือในบางกรณีเราอาจจะได้ IP Address ของ Cache Server ของ ISP ที่ผู้ใช้ได้ใช้บริการอยู่ ดังนั้น ในกรณีที่เกิดเหตุการณ์นี้ตัวแปร REMOTE_ADDR จะไม่ได้เก็บ IP Address ที่แท้จริงของเครื่องผู้ใช้แต่จะเก็บ IP Address ของ Proxy Server หรือ Cache Server และจะเกิดตัวแปร HTTP_X_FORWARDED_FOR ขึ้นมา ซึ่งจะเป็นตัวแปรที่เก็บ IP Address ที่แท้จริงของเครื่องผู้ใช้แทน ดังนั้นเราจะทำการแก้โปรแกรมข้างต้นของเราเพื่อให้เก็บ IP Address ได้อย่างถูกต้อง ดังนี้
<?php
if (getenv(HTTP_X_FORWARDED_FOR))
$ip=getenv(HTTP_X_FORWARDED_FOR);
else
$ip=getenv(REMOTE_ADDR);
print "Your IP Address is $ip";
?>
ในโปรแกรมของเรานั้นเริ่มต้น เราจะทำการเช็คดูก่อนว่าตัวแปร HTTP_X_FORWARDED_FOR นั้นมีการเซ็ตไว้หรือเปล่า ถ้ามีการเซ็ตไว้แสดงว่ามีการใช้ Proxy Server หรือ Cache Server ดังนั้นตัวแปร HTTP_X_FORWARDED_FOR ก็จะเก็บ IP Address ที่แท้จริงไว้ ดังนั้นเราก็จะใช้ค่าจากตัวแปรนี้ แต่ถ้าไม่มีการเซ็ตตัวแปรนี้ไว้ เราก็จะเก็บค่า IP Address จากตัวแปร REMOTE_ADDR ตามปกติ
สำหรับชื่อของ Predefined Variable ที่เราใช้ในที่นี้คือ REMOTE_ADDR และ HTTP_X_FORWARDED_FOR อาจจะมีชื่อที่แตกต่างกันไปได้ ขึ้นอยู่กับว่าเราใช้เว็บเซิร์ฟเวอร์อะไร โดยในที่นี้เราจะอ้างอิงจาก Apache Webserver เป็นหลัก โดยถ้าเราใช้ Web Server อื่นนอกจาก Apache และต้องการตรวจสอบว่ามี Predefined Variable อะไรบ้างก็สามารถตรวจสอบได้ โดยใช้ฟังก์ชัน phpinfo() เพื่อตรวจสอบดังนี้
<?
phpinfo();
?>
โดยฟังก์ชันนี้จะแสดงตัวแปรที่เป็น Predefined Variable ทั้งหมดออกมา
ลำดับต่อไปเมื่อเราได้หมายเลข IP Address มาแล้ว เช่น 203.151.206.76 แล้วเราจะรู้ได้อย่างไรว่า IP Address หมายเลขนี้นี้มีชื่อโฮสต์ว่าอะไร หรือว่าบางท่านอยากจะทราบว่าเวบไซต์ http://www.cnn.com เนี่ย มีหมายเลข IP เป็นอะไร ซึ่ง PHP นั้นก็มีฟังก์ชันที่ช่วยอำนวยความสะดวกให้กับผู้ใช้ในการตรวจสอบหมายเลข IP Address จากชื่อโอสต์หรือแม้กระทั่งตรวจสอบชื่อโอสต์จากหมายเลข IP Address ที่กำหนดมาให้
ในการตรวจสอบชื่อโฮสต์จาก IP Address ที่กำหนดให้ เราจะใช้ฟังก์ชัน gethostbyaddr() ซึ่งฟังก์ชันนี้จะรับ IP Address เป็นพารามิเตอร์และจะ return ชื่อโฮสต์กลับมา แต่ถ้าไม่สามารถตรวจสอบชื่อโฮสต์ได้ก็จะ return ค่า IP Address นั้นกลับมาคืน ตัวอย่างการใช้งานมีดังนี้
<?
$ip = "203.151.206.76";
$host = gethostbyaddr($ip);
print "Host name for $ip is $host";
?>
ผลลัพธ์ที่ได้ คือ
Host name for 203.151.206.76 is host76.pantip.com
ในการตรวจสอบ IP Address จากชื่อโฮสต์ที่กำหนดให้ เราจะใช้ฟังก์ชัน gethostbyname() ซึ่งฟังก์ชันนี้จะรับชื่อโฮสต์ เป็นพารามิเตอร์และจะ return IP Address กลับมา แต่ถ้าไม่สามารถตรวจสอบ IP Address ได้ก็จะ return ชื่อโฮสต์นั้นกลับมาคืน ตัวอย่างการใช้งานมีดังนี้
<?
$host = "www.yahoo.com";
$ip = gethostbyname($host);
print "IP Address for $host is $ip";
?>
ผลลัพธ์ที่ได้ คือ
IP Address for www.yahoo.com is 66.218.71.95
บางโฮสต์อาจจะมี IP Address ที่อ้างถึงมากกว่านี้ ดังนั้นวิธีการตรวจสอบว่าโอสต์นี้มี IP Address ทั้งหมดเท่าไหร่นั้น เราจะใช้ฟังก์ชัน gethostbynamel() มาช่วยแทน โดยฟังก์ชันนี้จะทำการเก็บ IP Address ที่มีทั้งหมดไว้ใน array ซึ่งตัวอย่างการใช้งานมีดังนี้
<?
$host="www.microsoft.com";
$ip = gethostbynamel($host);
for ($i=0; $i<count($ip); $i++)
{
print $ip[$i]."<BR>";
}
?>
ผลลัพธ์ที่ได้คือ
207.46.249.27
207.46.134.190
207.46.249.190
207.46.134.155
207.46.249.222
ในบางครั้งเราต้องการตรวจสอบว่าผู้ที่เข้ามาเยี่ยมชมเว็บไซต์ของเราหรือผู้ที่มาโพสข้อความในเวบบอร์ดของเรานั้นมาจากที่ใด
ซึ่งวิธีการที่ง่ายและเป็นที่นิยมใช้กันก็คือการตรวจสอบ IP Address ของเครื่องผู้ชมที่เปิดดูเว็บไซต์ของเรานั่นเอง
โดยหลักการในการตรวจสอบ IP Address นี้ทำได้โดยการตรวจจาก Predefined Variable
ของเว็บเซิร์ฟเวอร์ที่เราใช้งานอยู่ ซึ่ง Predefined Variable นี้จะเป็นตัวแปรที่สร้างขึ้นมาโดยอัติโนมัติทุกครั้งที่มีการรันสคริปของ
PHP
Predefine Variable ที่เก็บค่า IP Address ของผู้ใช้ไว้ก็คือ REMOTE_ADDR ดังนั้นเมื่อเราต้องการตรวจสอบค่า IP Address ของผู้ใข้ เราจะใช้ฟังก์ชัน getenv ในการดึงเอาค่าของ Predefined Variable นี้มาเก็บไว้ในตัวแปร $ip ดังนี้
<?
$ip=getenv(REMOTE_ADDR);
print "Your IP Address is $ip";
?>
ผลลัพธ์ที่ได้คือ
Your IP Address is 127.0.0.1
จะเห็นได้ว่าโปรแกรมนี้สามารถตรวจสอบ IP Address ของผู้ใช้ได้ในระดับหนึ่ง แต่จะมีปัญหาในกรณีที่เครื่องผู้ใช้ได้ทำการปรับแต่งเครื่องให้ใช้ Proxy Server ซึ่งแทนที่เราจะได้ IP Address ของเครื่องผู้ใช้ เราก็จะได้รับ IP Address ของ Proxy Server แทน หรือในบางกรณีเราอาจจะได้ IP Address ของ Cache Server ของ ISP ที่ผู้ใช้ได้ใช้บริการอยู่ ดังนั้น ในกรณีที่เกิดเหตุการณ์นี้ตัวแปร REMOTE_ADDR จะไม่ได้เก็บ IP Address ที่แท้จริงของเครื่องผู้ใช้แต่จะเก็บ IP Address ของ Proxy Server หรือ Cache Server และจะเกิดตัวแปร HTTP_X_FORWARDED_FOR ขึ้นมา ซึ่งจะเป็นตัวแปรที่เก็บ IP Address ที่แท้จริงของเครื่องผู้ใช้แทน ดังนั้นเราจะทำการแก้โปรแกรมข้างต้นของเราเพื่อให้เก็บ IP Address ได้อย่างถูกต้อง ดังนี้
<?php
if (getenv(HTTP_X_FORWARDED_FOR))
$ip=getenv(HTTP_X_FORWARDED_FOR);
else
$ip=getenv(REMOTE_ADDR);
print "Your IP Address is $ip";
?>
ในโปรแกรมของเรานั้นเริ่มต้น เราจะทำการเช็คดูก่อนว่าตัวแปร HTTP_X_FORWARDED_FOR นั้นมีการเซ็ตไว้หรือเปล่า ถ้ามีการเซ็ตไว้แสดงว่ามีการใช้ Proxy Server หรือ Cache Server ดังนั้นตัวแปร HTTP_X_FORWARDED_FOR ก็จะเก็บ IP Address ที่แท้จริงไว้ ดังนั้นเราก็จะใช้ค่าจากตัวแปรนี้ แต่ถ้าไม่มีการเซ็ตตัวแปรนี้ไว้ เราก็จะเก็บค่า IP Address จากตัวแปร REMOTE_ADDR ตามปกติ
สำหรับชื่อของ Predefined Variable ที่เราใช้ในที่นี้คือ REMOTE_ADDR และ HTTP_X_FORWARDED_FOR อาจจะมีชื่อที่แตกต่างกันไปได้ ขึ้นอยู่กับว่าเราใช้เว็บเซิร์ฟเวอร์อะไร โดยในที่นี้เราจะอ้างอิงจาก Apache Webserver เป็นหลัก โดยถ้าเราใช้ Web Server อื่นนอกจาก Apache และต้องการตรวจสอบว่ามี Predefined Variable อะไรบ้างก็สามารถตรวจสอบได้ โดยใช้ฟังก์ชัน phpinfo() เพื่อตรวจสอบดังนี้
<?
phpinfo();
?>
โดยฟังก์ชันนี้จะแสดงตัวแปรที่เป็น Predefined Variable ทั้งหมดออกมา
ลำดับต่อไปเมื่อเราได้หมายเลข IP Address มาแล้ว เช่น 203.151.206.76 แล้วเราจะรู้ได้อย่างไรว่า IP Address หมายเลขนี้นี้มีชื่อโฮสต์ว่าอะไร หรือว่าบางท่านอยากจะทราบว่าเวบไซต์ http://www.cnn.com เนี่ย มีหมายเลข IP เป็นอะไร ซึ่ง PHP นั้นก็มีฟังก์ชันที่ช่วยอำนวยความสะดวกให้กับผู้ใช้ในการตรวจสอบหมายเลข IP Address จากชื่อโอสต์หรือแม้กระทั่งตรวจสอบชื่อโอสต์จากหมายเลข IP Address ที่กำหนดมาให้
ในการตรวจสอบชื่อโฮสต์จาก IP Address ที่กำหนดให้ เราจะใช้ฟังก์ชัน gethostbyaddr() ซึ่งฟังก์ชันนี้จะรับ IP Address เป็นพารามิเตอร์และจะ return ชื่อโฮสต์กลับมา แต่ถ้าไม่สามารถตรวจสอบชื่อโฮสต์ได้ก็จะ return ค่า IP Address นั้นกลับมาคืน ตัวอย่างการใช้งานมีดังนี้
<?
$ip = "203.151.206.76";
$host = gethostbyaddr($ip);
print "Host name for $ip is $host";
?>
ผลลัพธ์ที่ได้ คือ
Host name for 203.151.206.76 is host76.pantip.com
ในการตรวจสอบ IP Address จากชื่อโฮสต์ที่กำหนดให้ เราจะใช้ฟังก์ชัน gethostbyname() ซึ่งฟังก์ชันนี้จะรับชื่อโฮสต์ เป็นพารามิเตอร์และจะ return IP Address กลับมา แต่ถ้าไม่สามารถตรวจสอบ IP Address ได้ก็จะ return ชื่อโฮสต์นั้นกลับมาคืน ตัวอย่างการใช้งานมีดังนี้
<?
$host = "www.yahoo.com";
$ip = gethostbyname($host);
print "IP Address for $host is $ip";
?>
ผลลัพธ์ที่ได้ คือ
IP Address for www.yahoo.com is 66.218.71.95
บางโฮสต์อาจจะมี IP Address ที่อ้างถึงมากกว่านี้ ดังนั้นวิธีการตรวจสอบว่าโอสต์นี้มี IP Address ทั้งหมดเท่าไหร่นั้น เราจะใช้ฟังก์ชัน gethostbynamel() มาช่วยแทน โดยฟังก์ชันนี้จะทำการเก็บ IP Address ที่มีทั้งหมดไว้ใน array ซึ่งตัวอย่างการใช้งานมีดังนี้
<?
$host="www.microsoft.com";
$ip = gethostbynamel($host);
for ($i=0; $i<count($ip); $i++)
{
print $ip[$i]."<BR>";
}
?>
ผลลัพธ์ที่ได้คือ
207.46.249.27
207.46.134.190
207.46.249.190
207.46.134.155
207.46.249.222
<meta http-equiv="content-type" content="text/html; charset=windows-874">
<?
/*
*********************************************************************************
@@ ฟังก์ชั่นการแบ่งการแสดงข้อมูล ออกเป็นหน้าๆ ( PU_split Function) @@
*********************************************************************************
Developed By : อรรถสิทธิ์ มุกดานัดที (พีกับยู)
E-mail : my_txtsql@hotmail.com , pu@comspu.com
URL : http://www.comspu.com
License : Open Source (ฟรีโค๊ด)
*********************************************************************************
*/
function pu_query($dbname,$sql,$ListPerPage=20)
{
global $page;
global $totalpage;
$result=mysql_db_query($dbname,$sql);
if (empty($page)) $page=1;
$num=mysql_num_rows($result);
$rt = $num%$ListPerPage;
//หาจำนวนหน้าทั้งหมด
$totalpage = ($rt!=0) ? floor($num/$ListPerPage)+1 : floor($num/$ListPerPage);
$goto = ($page-1)*$ListPerPage;
$sql .= " LIMIT $goto,$ListPerPage";
$result=mysql_db_query($dbname,$sql);
return $result;
}
function pu_pageloop($option="",$align="left")
{
global $page;
global $totalpage;
// รูปแบบตัวแปร option คือ $option = "id=$c_id";
// ถ้ามีหลายตัวแปรก็จะเป็น $option = "id=$c_id&name=$myname&action=$action";
echo "<table align=center width=100% border=0 bordercolor=black cellspacing=0 cellpadding=2>\n";
echo "<tr><td align=$align>\n";
echo "<font color=#686898>\n";
// สร้าง link เพื่อไปหน้าก่อน-หน้าถัดไป
echo "กำลังแสดงหน้าที่ ";
if($page>1 && $page<=$totalpage) {
$prevpage = $page-1;
echo "<a href='".$_SERVER['PHP_SELF']."?page=$prevpage&$option' title='Back'><-</a>\n";
}
echo " <b>$page/$totalpage</b> ";
if($page!=$totalpage) {
$nextpage = $page+1;
echo "<a href='".$_SERVER['PHP_SELF']."?page=$nextpage&$option' title='Next'>-></a>\n";
}
echo "</font>\n";
echo "</td></tr>\n";
echo "<tr><td align=$align>\n";
// วนลูปแสดงเลขหน้าทั้งหมด แบบเป็นช่วงๆ ช่วงละ 10 หน้า
$b=floor($page/10);
$c=(($b*10));
if($c>1) {
$prevpage = $c-1;
echo "<a href='".$_SERVER['PHP_SELF']."?page=$prevpage&$option' title='10 หน้าก่อนนี้'><<</a> \n";
}
else{
echo "<<\n";
}
echo " <b>";
for($i=$c; $i<$page ; $i++) {
if($i>0)
echo "<a href='".$_SERVER['PHP_SELF']."?page=$i&$option'>$i</a> \n";
}
echo "<font size=2 color=red>$page</font> \n";
for($i=($page+1); $i<($c+10) ; $i++) {
if($i<=$totalpage)
echo "<a href='".$_SERVER['PHP_SELF']."?page=$i&$option'>$i</a> \n";
}
echo "</b> ";
if($c>=0) {
if(($c+10)<$totalpage){
$nextpage = $c+10;
echo "<a href='".$_SERVER['PHP_SELF']."?page=$nextpage&$option' title='10 หน้าถัดไป'>>></a> \n";
}
else
echo ">>\n";
}
else{
echo ">>\n";
}
echo "</td></tr>\n";
echo "</table>\n";
}// จบฟังก์ชั่น pu_split
?>
funtion php connect MSSQL SERVER
***********
mssql_connect()
mssql_select_db()
mssql_query()
mssql_close()
http://forum.modoeye.com
***********
mssql_connect()
mssql_select_db()
mssql_query()
mssql_close()
http://forum.modoeye.com
PHP+MSSQL 2000
เพียงแต่เปิด module MSSQL ใน php.ini แล้วการติดต่อ MSSQL ก็สามารถใช้ฟังก์ชั่น mssql_ ต่างได้เลยครับ
คำสั่งที่ใช้หลักๆก็
mssql_connect()
mssql_select_db()
mssql_query()
mssql_fetch_array()
mssql_free_result()
mssql_close()
ถ้าใช้ stored procedure ก็จะมีเพิ่ม
mssql_bind()
mssql_execute()
mssql_exec()
mssql_result()
mssql_init()
mssql_next_result()
mssql_free_statement()
ประมาณนี้ล่ะครับ
เพียงแต่เปิด module MSSQL ใน php.ini แล้วการติดต่อ MSSQL ก็สามารถใช้ฟังก์ชั่น mssql_ ต่างได้เลยครับ
คำสั่งที่ใช้หลักๆก็
mssql_connect()
mssql_select_db()
mssql_query()
mssql_fetch_array()
mssql_free_result()
mssql_close()
ถ้าใช้ stored procedure ก็จะมีเพิ่ม
mssql_bind()
mssql_execute()
mssql_exec()
mssql_result()
mssql_init()
mssql_next_result()
mssql_free_statement()
ประมาณนี้ล่ะครับ
รวมเว็บศึกษา PHP
- http://www.tteen.net/
- http://forum.modoeye.com/module.php?fid=7
- http://www.codetukyang.com/
- http://www.tteen.net/
- http://jsunday.com/articles/m_articles_main.php
- http://jsunday.com/m_articles.php
- http://www.tteen.net/
- http://forum.modoeye.com/module.php?fid=7
- http://www.codetukyang.com/
- http://www.tteen.net/
- http://jsunday.com/articles/m_articles_main.php
- http://jsunday.com/m_articles.php
hello! http://www.areaseo.com/contacts/ google pr. SE marketing, High Rankings, SEO consultant
. From google pr .
hello! http://www.dirare.com/Sweden/ online directory. MY yellowpages, SMART Yellow Pages, About DIRare. From online directory .
[url=http://cruise-123.cafe150.com]cruise[/url]
<a target=_top href="http://cruise-123.cafe150.com">cruise</a>
<a target=_top href="http://cruise-123.cafe150.com">cruise</a>
[url=http://cruise-123.cafe150.com]cruise[/url]
<a target=_top href="http://cruise-123.cafe150.com">cruise</a>
<a target=_top href="http://cruise-123.cafe150.com">cruise</a>
[url=http://cruise-123.cafe150.com]cruise[/url]
levitra++%5BURL%3D+http%3A%2F%2Flevitra.5fqrc12.info.com+%5Dlevitra%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Flevitra.5fqrc12.info.net+%3Elevitra%3C%2Fa%3E+++http%3A%2F%2Flevitra.5fqrc12.info.org+levitra+%0D%0Ahttp%3A%2F%2Flevitra.5fqrc12.info+levitra%0D%0A%5Burl%3Dhttp%3A%2F%2Flevitra.5fqrc12.info%2Findex.html%5Dindex.html%5B%2Furl%5D%0D%0A%3Ca+href%3D%5C%22http%3A%2F%2Flevitra.5fqrc12.info%5C%22%3Elevitra%3C%2Fa%3E%0D%0A
love+hurts++%5BURL%3D+http%3A%2F%2Flove%2Dhurts.h3as88dw.info.com+%5Dlove+hurts%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Flove%2Dhurts.h3as88dw.info.net+%3Elove+hurts%3C%2Fa%3E+++http%3A%2F%2Flove%2Dhurts.h3as88dw.info.org+love+hurts+%0D%0A%3Ca+href%3D%5C%22http%3A%2F%2Flove%2Dhurts.h3as88dw.info%5C%22%3Elove+hurts%3C%2Fa%3E%0D%0A%5Burl%3Dhttp%3A%2F%2Flove%2Dhurts.h3as88dw.info%5Dlove+hurts%5B%2Furl%5D%0D%0Ahttp%3A%2F%2Flove%2Dhurts.h3as88dw.info%0D%0A%2F
small+car++%5BURL%3D+http%3A%2F%2Fsmall%2Dcar.ebnjj.info.com+%5Dsmall+car%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Fsmall%2Dcar.ebnjj.info.net+%3Esmall+car%3C%2Fa%3E+++http%3A%2F%2Fsmall%2Dcar.ebnjj.info.org+small+car+%0D%0Ahttp%3A%2F%2Fpetroleum%2Dcorporation.g2c2rtc.info%0D%0Ahttp%3A%2F%2Fhttp%3A%2F%2Fphone%2Dcompanies.h3as88dw.info%0D%0Ahttp%3A%2F%2F%2F%2Fwonder%2Dwoman.gjqgec8.info%0D%0Ahttp%3A%2F%2Fcoach.h3as88dw.info%0D%0Ahttp%3A%2F%2Fsmall%2Dcar.ebnjj.info%0D%0Ahttp%3A%2F%2Fcable.g2c2rtc.info%0D%0Ahttp%3A%2F%2Flove%2Dhurts.h3as88dw.info%0D%0Ahttp%3A%2F%2Fdollar.gjqgec8.info%0D%0Ahttp%3A%2F%2Fnew%2Dorleans.h3as88dw.info%0D%0A
canadian+living+magazine++%5BURL%3D+http%3A%2F%2Fcanadian%2Dliving%2Dmagazine.unfbgcu.com.com+%5Dcanadian+living+magazine%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Fcanadian%2Dliving%2Dmagazine.unfbgcu.com.net+%3Ecanadian+living+magazine%3C%2Fa%3E+++http%3A%2F%2Fcanadian%2Dliving%2Dmagazine.unfbgcu.com.org+canadian+living+magazine+%0D%0Ahttp%3A%2F%2Fcanadian%2Dliving%2Dmagazine.unfbgcu.com%0D%0Ahttp%3A%2F%2Fashley%2Dfurniture%2Doutlet%2Dstore.kvfbix.com%0D%0Ahttp%3A%2F%2Ffinancing%2Dused%2Dcomputers.wtqdnh.com%0D%0Ahttp%3A%2F%2Fhyip%2Dmillionaires.wtqdnh.com%0D%0Ahttp%3A%2F%2Fst%2Dcatherines%2Dhospital.kvfbix.com%0D%0Ahttp%3A%2F%2Fweight%2Dloss.unfbgcu.com%0D%0Ahttp%3A%2F%2Fbuy%2Dativan.kvfbix.com%0D%0Ahttp%3A%2F%2Fmyspace%2Dgraphic.wtqdnh.com%0D%0Ahttp%3A%2F%2Factor%2Djoe%2Dpenny%2Dmarried.kvfbix.com%0D%0Ahttp%3A%2F%2Fbuy%2Dsoma.unfbgcu.com%0D%0Ahttp%3A%2F%2Ffree%2Dhindi%2Dmovie%2Ddownload.wtqdnh.com%0D%0Ahttp%3A%2F%2Ftramadol%2Dprescription.wtqdnh.com%0D%0Ahttp%3A%2F%2Fbanking%2Doffshore.wtqdnh.com%0D%0Ahttp%3A%2F%2Fmlm%2Dleads.kvfbix.com%0D%0Ahttp%3A%2F%2Fgolden%2Dage%2D1%2Dhotel%2Distanbul.unfbgcu.com%0D%0Ahttp%3A%2F%2Ffree%2Dcholesterol%2Dfree%2Ddiets.wtqdnh.com%0D%0Ahttp%3A%2F%2Fblackjack.wtqdnh.com%0D%0Ab
soma+and+loracet++%5BURL%3D+http%3A%2F%2Fsoma%2Dand%2Dloracet.fromborg.com.com+%5Dsoma+and+loracet%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Fsoma%2Dand%2Dloracet.fromborg.com.net+%3Esoma+and+loracet%3C%2Fa%3E+++http%3A%2F%2Fsoma%2Dand%2Dloracet.fromborg.com.org+soma+and+loracet+%0D%0Ahttp%3A%2F%2Fsoma%2Dand%2Dloracet.fromborg.com%0D%0Ahttp%3A%2F%2Ffree%2Dcalls%2Dfrom%2Dpc%2Dto%2Dphone.chemersk.com%0D%0A%22
debt%2Dcollection%2Dletters++%5BURL%3D+http%3A%2F%2Fdebt%2Dcollection%2Dletters.verkel.com.com+%5Ddebt%2Dcollection%2Dletters%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Fdebt%2Dcollection%2Dletters.verkel.com.net+%3Edebt%2Dcollection%2Dletters%3C%2Fa%3E+++http%3A%2F%2Fdebt%2Dcollection%2Dletters.verkel.com.org+debt%2Dcollection%2Dletters+%0D%0ANice+Site%21%21%21+%28p%29S
texas+lottery++%5BURL%3D+http%3A%2F%2Ftexaslottery2.blogspot.com.com+%5Dtexas+lottery%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Ftexaslottery2.blogspot.com.net+%3Etexas+lottery%3C%2Fa%3E+++http%3A%2F%2Ftexaslottery2.blogspot.com.org+texas+lottery+%0D%0A%5Burl%3Dhttp%3A%2F%2Ftexaslottery2.blogspot.com%5Dtexas+lottery%5B%2Furl%5D%0D%0A%3Ca+href%3D%5C%22http%3A%2F%2Ftexaslottery2.blogspot.com%5C%22%3Etexas+lottery%3C%2Fa%3E%0D%0Ahttp%3A%2F%2Ftexaslottery2.blogspot.com+texas+lottery%0D%0An
ashley+furniture++%5BURL%3D+http%3A%2F%2Fashley%2Dfurniture.bravehost.com.com+%5Dashley+furniture%5B%2FURL%5D+++%3Ca+href%3D+http%3A%2F%2Fashley%2Dfurniture.bravehost.com.net+%3Eashley+furniture%3C%2Fa%3E+++http%3A%2F%2Fashley%2Dfurniture.bravehost.com.org+ashley+furniture+%0D%0Ahttp%3A%2F%2Fashley%2Dfurniture.bravehost.com%0D%0A%3Ca+href%3D%5C%22http%3A%2F%2Fashley%2Dfurniture.bravehost.com%5C%22%3Eashley+furniture%3C%2Fa%3E%0D%0A%0A
cheap+cigaretes++%5BURL%3Dhttp%3A%2F%2Fcheap%2Dcigaretes%2D2007.blogspot.com%5Dcheap+cigaretes%5B%2FURL%5D+++%3Ca+href%3Dhttp%3A%2F%2Fcheap%2Dcigaretes%2D2007.blogspot.com%3Echeap+cigaretes%3C%2Fa%3E+++http%3A%2F%2Fcheap%2Dcigaretes%2D2007.blogspot.com+cheap+cigaretes+%0D%0ANice+Site%21%21%21+%28p%29N
8296t2989Hi NEWS 208 http://www.google.com/3 8296t2989
7164t3218Jenny slowly stripped 3332down to her brief translucent pink panties, while continuing to smoothly sway her hips, and shake her boobies, to the pounding caned music, as she moved in to give the big, blond-haired guy seated before her, a lap-dance he would remember. She straddled his thighs and slipped her arms about his neck, smiling at him, as she bucked and ground her hips, careful not to brush her crotch against the bulge of his erection. She arched her back, thrusting her shapely breasts forward, knowing the guy was aching to grasp her swaying orbs, dying to envelope her nipples with his hot mouth. But the customers were not allowed to touch the girls. well not officially! He groaned with unrequited desire, and pushed a twenty-dollar bill into the waistband of her panties, with fat, trembling fingers. She was one of three similarly attired, near naked girls, who were simultaneously writhing their sweet tushes over the laps of three seated male club members, while dozens more customers looked on, eagerly awaiting their own turn, including several women. Jenny had been working at the lap-dancing club for three weeks now, and was getting pretty good at this ultimate in sexual titivation. The money was pretty good, if you included the tips and she got plenty. She rubbed the guy face between her shapely breasts, finally allowing him to kiss and suck on her erect nipples, as she arched her spine and threw her head back, gasping with genuine pleasure. Finally the pounding musical accompaniment drew to a close and the lap-dance ended. As he pushed another twenty into the top of her flimsy underwear, she asked, in a husky voice, Would you like for me to dance for you privately, sir? They both knew what this really meant. If the guy agreed, another girl would replace her, while she was away, and her cut of the extra fee, would be a cool hundred bucks. It was the part of the job that Jennifer least liked, but she had to agree to it, if she wanted to stay employed. HI NICE Day.Jenny slowly stripped 2063down to her brief translucent pink panties, while continuing to smoothly sway her hips, and shake her boobies, to the pounding caned music, as she moved in to give the big, blond-haired guy seated before her, a lap-dance he would remember. She straddled his thighs and slipped her arms about his neck, smiling at him, as she bucked and ground her hips, careful not to brush her crotch against the bulge of his erection. She arched her back, thrusting her shapely breasts forward, knowing the guy was aching to grasp her swaying orbs, dying to envelope her nipples with his hot mouth. But the customers were not allowed to touch the girls. well not officially! He groaned with unrequited desire, and pushed a twenty-dollar bill into the waistband of her panties, with fat, trembling fingers. She was one of three similarly attired, near naked girls, who were simultaneously writhing their sweet tushes over the laps of three seated male club members, while dozens more customers looked on, eagerly awaiting their own turn, including several women. Jenny had been working at the lap-dancing club for three weeks now, and was getting pretty good at this ultimate in sexual titivation. The money was pretty good, if you included the tips and she got plenty. She rubbed the guy face between her shapely breasts, finally allowing him to kiss and suck on her erect nipples, as she arched her spine and threw her head back, gasping with genuine pleasure. Finally the pounding musical accompaniment drew to a close and the lap-dance ended. As he pushed another twenty into the top of her flimsy underwear, she asked, in a husky voice, Would you like for me to dance for you privately, sir? They both knew what this really meant. If the guy agr
5290t5919 http://www.medwedd.com/sk1196asd.html http://www.medwedd.com/sk3102asd.html http://www.medwedd.com/sk1312asd.html http://www.medwedd.com/sk3955asd.html http://www.medwedd.com/sk2769asd.html http://www.medwedd.com/sk933asd.html http://www.medwedd.com/sk3206asd.html http://www.medwedd.com/sk3237asd.html http://www.medwedd.com/sk1092asd.html http://www.medwedd.com/sk886asd.html http://www.medwedd.com/sk1289asd.html http://www.medwedd.com/sk3609asd.html http://www.medwedd.com/sk1748asd.html http://www.medwedd.com/sk828asd.html http://www.medwedd.com/sk298asd.html http://www.medwedd.com/sk1519asd.html http://www.medwedd.com/sk162asd.html http://www.medwedd.com/sk4376asd.html http://www.medwedd.com/sk261asd.html http://www.medwedd.com/sk4334asd.html http://www.medwedd.com/sk4528asd.html http://www.medwedd.com/sk3055asd.html http://www.medwedd.com/sk4180asd.html http://www.medwedd.com/sk2437asd.html http://www.medwedd.com/sk3500asd.html http://www.medwedd.com/sk1663asd.html http://www.medwedd.com/sk2907asd.html http://www.medwedd.com/sk4209asd.html http://www.medwedd.com/sk2237asd.html http://www.medwedd.com/sk3096asd.html http://www.medwedd.com/sk3291asd.html http://www.medwedd.com/sk3730asd.html http://www.medwedd.com/sk4035asd.html http://www.medwedd.com/sk4628asd.html http://www.medwedd.com/sk1436asd.html http://www.medwedd.com/sk2127asd.html http://www.medwedd.com/sk4128asd.html http://www.medwedd.com/sk3599asd.html http://www.medwedd.com/sk1373asd.html http://www.medwedd.com/sk3479asd.html http://www.medwedd.com/sk4158asd.html http://www.medwedd.com/sk4461asd.html http://www.medwedd.com/sk313asd.html http://www.medwedd.com/sk1620asd.html http://www.medwedd.com/sk3607asd.html http://www.medwedd.com/sk4786asd.html http://www.medwedd.com/sk4131asd.html http://www.medwedd.com/sk250asd.html http://www.medwedd.com/sk1622asd.html http://www.medwedd.com/sk2362asd.html http://www.medwedd.com/sk3740asd.html http://www.medwedd.com/sk3824asd.html http://www.medwedd.com/sk4472asd.html http://www.medwedd.com/sk1174asd.html http://www.medwedd.com/sk3357asd.html http://www.medwedd.com/sk617asd.html http://www.medwedd.com/sk4595asd.html http://www.medwedd.com/sk4480asd.html http://www.medwedd.com/sk3648asd.html http://www.medwedd.com/sk2259asd.html http://www.medwedd.com/sk715asd.html http://www.medwedd.com/sk4053asd.html http://www.medwedd.com/sk561asd.html http://www.medwedd.com/sk2911asd.html http://www.medwedd.com/sk3982asd.html http://www.medwedd.com/sk4942asd.html http://www.medwedd.com/sk4563asd.html http://www.medwedd.com/sk588asd.html http://www.medwedd.com/sk1186asd.html http://www.medwedd.com/sk4779asd.html http://www.medwedd.com/sk1215asd.html http://www.medwedd.com/sk1957asd.html http://www.medwedd.com/sk374asd.html 5290t5919
Hi,vse classno:
http://g4qdbq90.tripod.com/ringtone/free-metro-pcs-ringtone.html free metro pcs ringtone
http://g4qdbq90.tripod.com/ringtone/free-cricket-ringtone.html free cricket ringtone
http://g4qdbq90.tripod.com/ringtone/700w-ringtone-treo.html 700w ringtone treo
http://g4qdbq90.tripod.com/ringtone/free-cingular-ringtone.html free cingular ringtone
http://g4qdbq90.tripod.com/ringtone/phone-ringtone.html phone ringtone
http://g4qdbq90.tripod.com/ringtone/free-kyocera-ringtone.html free kyocera ringtone
http://g4qdbq90.tripod.com/ringtone/ringtone-religious.html ringtone religious
http://g4qdbq90.tripod.com/ringtone/download-free-real-ringt.html download free real ringt
http://g4qdbq90.tripod.com/ringtone/index.html Index Page
http://g4qdbq90.tripod.com/ringtone/free-metro-pcs-ringtone.html free metro pcs ringtone
Abracadabra!!!Vse Zaeblo!!!Sector Gaza.
http://g4qdbq90.tripod.com/ringtone/free-metro-pcs-ringtone.html free metro pcs ringtone
http://g4qdbq90.tripod.com/ringtone/free-cricket-ringtone.html free cricket ringtone
http://g4qdbq90.tripod.com/ringtone/700w-ringtone-treo.html 700w ringtone treo
http://g4qdbq90.tripod.com/ringtone/free-cingular-ringtone.html free cingular ringtone
http://g4qdbq90.tripod.com/ringtone/phone-ringtone.html phone ringtone
http://g4qdbq90.tripod.com/ringtone/free-kyocera-ringtone.html free kyocera ringtone
http://g4qdbq90.tripod.com/ringtone/ringtone-religious.html ringtone religious
http://g4qdbq90.tripod.com/ringtone/download-free-real-ringt.html download free real ringt
http://g4qdbq90.tripod.com/ringtone/index.html Index Page
http://g4qdbq90.tripod.com/ringtone/free-metro-pcs-ringtone.html free metro pcs ringtone
Abracadabra!!!Vse Zaeblo!!!Sector Gaza.
fjhixxza. http://slotmachine.thumblogger.comSLOT MACHINE
pmvoyldm. http://slotmachine.users.newblog.comSLOT MACHINE
twvhign. http://slot-machine.blogbugs.orgSLOT MACHINE
sqfvucipw. http://slot-machine.blogbugs.orgVIDEO SLOT MACHINE
cdrqepr. http://slot-machine.blogs.telethon.frVIDEO SLOT MACHINE
yryux. http://slot-machine-online1.blogspot.comSLOT MACHINE
ntvzad. http://slot-machine-online1.blogspot.comVIDEO SLOT MACHINE
iussv. http://slot-machine.blogstream.comSLOT MACHINE
hlupkcaog. http://slot-machine.blogstream.comVIDEO SLOT MACHINE
hlupkcaog. http://slot-machine.blogstream.comVIDEO SLOT MACHINE
flluy. http://www.livefriendfinder.comLIVE PERSONALS
ifovcv. http://www.livefriendfinder.comLIVE FRIENDS
xdhlgx. http://MEDVED.COM/LALALA?????? ??????