<?php
function redirect($url = '', array $params = array()) {
// スキーム省略に対応
$url = preg_replace_callback(
'@^/{2}(?!/)@',
function ($matches) {
return
empty($_SERVER['HTTPS']) || strcasecmp($_SERVER['HTTPS'], 'off') ?
'http://' :
'https://'
;
},
$url
);
// URLをパースする
if (!$p = parse_url($url)) {
throw new BadMethodCallException('URLのパースに失敗しました');
}
// 相対URLで書かれた場合には絶対URLに補正して再パースさせる
if (!isset($p['host'])) {
$url = rtrim($GLOBALS['SITE_URL'], '/') . '/' . ltrim($url, '/');
if (!$p = parse_url($url) or !isset($p['host']) or !isset($p['scheme'])) {
throw new BadMethodCallException('URLのパースに失敗しました');
}
}
// クエリ―ストリングをパースして連想配列にする
parse_str(isset($p['query']) ? $p['query'] : '', $query);
// URLを組み立てていく
$user_and_pass[] = isset($p['user']) ? $p['user'] : null;
$user_and_pass[] = isset($p['pass']) ? $p['pass'] : null;
$host_and_port[] = $p['host'];
$host_and_port[] = isset($p['port']) ? $p['port'] : null;
$server[] = implode(':', array_filter($user_and_pass, 'is_scalar'));
$server[] = implode(':', array_filter($host_and_port, 'is_scalar'));
$server_and_path[] = implode('@', array_filter($server, 'strlen'));
$server_and_path[] = isset($p['path']) ? ltrim($p['path'], '/') : '';
$sp_and_query[] = implode('/', $server_and_path);
$sp_and_query[] = http_build_query($params + $query, '', '&');
$spq_and_fragment[] = implode('?', array_filter($sp_and_query, 'strlen'));
$spq_and_fragment[] = isset($p['fragment']) ? $p['fragment'] : null;
$scheme_and_spqf[] = $p['scheme'];
$scheme_and_spqf[] = implode('#', array_filter($spq_and_fragment, 'is_scalar'));
$url = implode('://', $scheme_and_spqf);
// 改行コードやNULLバイトを削除する
$url = str_replace(array("\r", "\n", "\0"), '', $url);
return $url;
}
$SITE_URL = 'http://user:' . "\n\0\n" . 'pass@exam' . "\n" . 'ple.com:87/p'."\n".'ublic';
var_dump(redirect("\n" . '//test.php?a=b'));
- Output for git.master, git.master_jit, rfc.property-hooks
- string(59) "http://user:___pass@exam_ple.com:87/p_ublic/_//test.php?a=b"
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
52.89 ms | 401 KiB | 8 Q