1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

1人コンプAdvent Calendar 2022

Day 16

phpでリダイレクトをする関数を作った

Posted at

はじめに

phpでリダイレクトする時、通常は以下のような書き方をします。

header("location:URL");
exit;

ヘッダー情報を上書きするんですね。
ただ、この二行を書くのが面倒臭かったので関数にしました。
需要はほぼなしですが...

コード

function redirect_page($status = 0,$status_one_url = null){
    if($status===0){
        header("location:".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        exit;
    }else if($status===1){
        header("location:".$status_one_url);
        exit;
    }
}

使い方

同じページにリダイレクトする場合

redirect_page(0);

指定したページにリダイレクトする場合

ridirect_page(1,"https://google.com/");

終わりに

「需要が、」
   ゼーロー

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?