PHP : การทำ redirect ด้วยฟังก์ชั่น header()

ฟังก์ชั่น header ใช้ได้กับทั้ง (PHP 4, PHP 5) (php.net)

นิยาม header — Send a raw HTTP header

[code lang=”php”]
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
[/code]

ฟังก์ชั่น header ในภาษา PHP นำมาประยุกต์ใช้ได้หลากหลาย หนึ่งในนั้นคือการทำ redirect

[code lang=”php”]
<?php
header(‘Location: http://www.phaisarn.com/’);
?>
[/code]

ที่มา : www.devsolution.net

PHP: แสดง thumbnail ของภาพแรกใน Post

นำภาพแรกใน post (ในที่นี้สมมุติข้อความใน post ไว้ใน $post ในบรรทัดที่ 10) มาแสดง (บรรทัดที่ 22)
[code lang=”php”]
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8">
<title>ทดสอบ Create thumbnail</title>
</head>
<body>
<?php
function catch_that_image() {
$post = ‘some text with two pictures: <img src="http://www.example.com/image1.jpg" /> <img src="http://www.example.com/image2.png" />’;
$first_img = ”;
$output = preg_match_all(‘/<img.+src=[\’"]([^\’"]+)[\’"].*>/i’, $post, $matches);
$first_img = $matches[1][0];
/*$second_img = $matches[1][1];*/

if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
?>
<p><img src="<?php echo catch_that_image() ?>" width="110"></p>
</body>
</html>
[/code]

ดัดแปลงโค๊ดมาจาก http://www.wprecipes.com, http://css-tricks.com

เขียนสคริปส่ง e-mail ด้วย PHP

form_sendmail.html

[code lang=”html”]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="sendmail.php" method="post">
<p>ทดสอบการส่ง e-mail</p>
<table width="400" border="0">
<tr>
<td>To:</td>
<td><input name="MailTo" type="text" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input name="MailSubject" type="text" /></td>
</tr>
<tr>
<td> Message:</td>
<td><textarea name="MailMessage" ></textarea></td>
</tr>
<tr>
<td>From:</td>
<td><input name="MailFrom" type="text" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
[/code]

sendmail.php

[code lang=”php”]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p><a href="form_sendmail.html">form_sendmail</a></p>
<?php
$MailTo = $_POST[‘MailTo’] ;
$MailFrom = $_POST[‘MailFrom’] ;
$MailSubject = $_POST[‘MailSubject’] ;
$MailMessage = $_POST[‘MailMessage’] ;

$Headers = "MIME-Version: 1.0\r\n" ;
$Headers .= "Content-type: text/html; charset=UTF-8\r\n" ;
$Headers .= "From: ".$MailFrom." <".$MailFrom.">\r\n" ;
$Headers .= "Reply-to: ".$MailFrom." <".$MailFrom.">\r\n" ;
$Headers .= "X-Priority: 3\r\n" ;
$Headers .= "X-Mailer: PHP mailer\r\n" ;

if(mail($MailTo, $MailSubject , $MailMessage, $Headers, $MailFrom))
{
echo "ส่ง e-mail เรียบร้อย" ; //ส่งเรียบร้อย
}else{
echo "ไม่สามารถส่ง e-mail ได้" ; //ไม่สามารถส่งเมล์ได้
}

?>
</body>
</html>
[/code]

ที่มา: thaicreate.com

PHP: วิธีกำหนดค่า register_globals

การกำหนดค่า register_globals สามารถทำได้ 2 วิธีคือ กำหนดที่ไฟล์ php.ini หรือที่ไฟล์ .htaccess

วิธีที่ 1: แก้ไขไฟล์ php.ini

  • เปิดไฟล์ c:\WINDOWS\php.ini
  • ค้นหาคำว่า register_globals แล้วกำหนดให้เป็น Off ดังนี้ register_globals=off

วิธีที่ 2: แก้ไขไฟล์ .htaccess

  • สร้างไฟล์ .htaccess ที่ htdocs
  • พิมพ์ php_value register_globals 0