วันจันทร์ที่ 17 กรกฎาคม พ.ศ. 2560

ว่าด้วยการ Redirect

วิธีการ Redirect หน้าเว็บไซต์ ด้วย PHP Code 

ทำการ PHP Redirect - Header Location
<?
 header( "location: http://www.youurl.com" );
 exit(0);
?>

** location ต้องตามติดด้วย ( : ) เสมอ
 
ทำการ PHP Redirect - Header Refresh แบบตั้งเวลา
case 1
นำเอา HTML META มาแทรก PHP

echo "<html><meta http-equiv=\"refresh\" content=\"6;URL='http://youurl.com/'\">
<br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p>
</div></html>";
case 2
กรณีนี้ถ้าไม่ทำงานให้ใช้ โดยนำวางบรรทัดแรกของ PHP Code
@ob_flush(); //flush the output buffer
flush(); //flush anything else  
 sleep(10);//รอ 10 วินาทีก่อนจะ  Redirect
   header("Location: http://www.youurl.com");
case 3
 header("refresh: 2; url='http://www.youurl.com'");
exit(0);
** refresh ต้องตามติดด้วย ( : ) เสมอ
** ค่าตัวเลข 2 หมายถึง จำนวนวินาที

** สามารถระบุปลายทางด้วย ที่อยู่เว็บไซต์ ( URL ) หรือ Path / Folder ที่ต้องการ
** การระบุ exit(0); หมายถึง ให้ทำงานสิ้นสุดแค่คำสั่ง header เท่านั้น
** การใช้ function header ใน php ต้องไม่มีการสั่ง echo , print เกิดขึ้นก่อน ฟังก์ชั่น header

วิธีการ Redirect หน้าเว็บไซต์ ด้วย HTML, Button

<meta http-equiv="refresh" content="6;URL='http://youurl.com'">

วิธีการ Redirect หน้าเว็บไซต์ ด้วย Input Button  เปิดหน้า Popup ขึ้นมา

<input type="button" name="button" id="button" value="Update Data" 
onClick="window.open('http://www.youurl.com','pwin',
'location=no,status=no,scrollbars=no,resizeable=yes,toolbar=no')" />

วิธีการ Redirect หน้าเว็บไซต์ ด้วย Input Button // Style สีพื้นหลังปุ่ม / สีต่อมาคือสีของตัวหนังสือของปุ่ม

<input type="button" name="button1" id="button1" value="Send" 
onClick="location.href='manual.php'" style="background-color:#0000FF;
color:#FFFFFF" /> 

ตัวอย่าง PHP วิธีการ Redirect หน้าเว็บไซต์  

<?php
header('Refresh: 5;URL=http://www.google.com/'); // refresh header
echo "<html><head>";
// meta refresh
echo "<meta http-equiv=\"refresh\" content=\"5;URL=http://www.google.com/\" />"; 
echo "</head><body><br><br><br><div id='loading'><p><img src='loader.gif'>
Please wait 6 seconds...</p></div></body></html>";
?>
<script type="text/javascript">
// javascript refresh
window.setTimeout(function(){location.href="http://www.google.com";},5000);
</script>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น