Last active
December 19, 2015 06:46
-
-
Save mia-0032/4384705 to your computer and use it in GitHub Desktop.
PJAX通信の実装(PHP側)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* pjax通信かどうか判定する関数。 | |
* ヘッダーがあるかないかで判定。 | |
*/ | |
function isPjax() { | |
$headers = getallheaders(); | |
//ブラウザごとに送られるヘッダーの大文字小文字が違うので吸収・・・だけどもっといい方法ありそう | |
if ((isset($headers['X-PJAX']) && $headers['X-PJAX'] === "true") || | |
(isset($headers['x-pjax']) && $headers['x-pjax'] === "true") || | |
(isset($headers['X-Pjax']) && $headers['X-Pjax'] === "true")) { | |
$is_pjax = true; | |
} else { | |
$is_pjax = false; | |
} | |
return $is_pjax; | |
} | |
$content = 'pjax!!'; | |
$html = <<<HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
</head> | |
<body> | |
<div id="loading_image" style="display:none;">loading...</div> | |
<div id="content_area">not pjax</div> | |
<a id="load_link">load</a> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script src="./js/pjax.js"></script> | |
</body> | |
</html> | |
HTML; | |
//pjax通信のときはコンテンツだけ返す。 | |
//ただしそのページに直接アクセスしてきたときとかはpjax通信ではないので全文返す | |
if (isPjax()) { | |
echo $content; | |
} else { | |
echo $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment