[PHP]31.網址導向 header() 用法

  • 4221
  • 0
  • php
  • 2022-12-03

文、意如

1.網址導向
2.第一頁使用下拉選單讓使用者選擇要導向的畫面,送往第2頁處理網址導向
3.自動導向手機版網頁或電腦版網頁
1.網址導向
<?php
$URL="https://www.google.com.tw/"; 
header("Location: $URL");// 將網址導向  https://www.google.com.tw/
?>

參考

2.第一頁使用下拉選單讓使用者選擇要導向的畫面,送往第2頁處理網址導向

 

 

 

 

myurl.php

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body>
    <form method="post" action="myurl2.php">
      <select name="goweb" size="1"> 
        <option value="https://dotblogs.com.tw/YiruAtStudio">Yiru@studio</option> 
        <option value="http://www.yahoo.com">Yahoo</option> 
        <option value="http://www.google.com.tw">Google</option>
      </select>
      &nbsp;<input type="submit">
    </form>
  </body>
</html>

參考

myurl2.php

<?php
  $URL = $_POST['goweb'];
  header("Location: $URL");
?>

參考

3.自動導向手機版網頁或電腦版網頁

先建立幾張簡單手機的網頁 ,和電腦版網頁

例如:

電腦版網頁:PC.php

<?php
echo "電腦版網頁";
?>

iphone手機版本:iPhone.php

ipad版本:ipad.php

android版本:android.php

 

接下來就寫一支程式抓取使用者的環境,根據使用者的環境去跳轉專屬的頁面

 

test.php

<?php
  $agent = strtolower($_SERVER['HTTP_USER_AGENT']); 
  #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
  #轉小寫strtolower()
  echo $agent ;
  #mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/103.0.0.0 safari/537.36
  if (stripos($agent, "iphone") != false){ #搜尋字串中是否有此文字,有即回傳true
    $url = "iPhone.php"; #跳轉
  }else if (stripos($agent, "ipad") != false) {
    $url = "iPad.php";	
  }else if (stripos($agent, "android") != false) {
    $url = "Android.php";
  }else{
    $url = "PC.php";
  }
  header("Location: $url");
?>

參考

 

 

Yiru@Studio - 關於我 - 意如