Last active
October 24, 2019 07:25
-
-
Save Lewiscowles1986/909d62227a8cd11497cf6b754c1032f3 to your computer and use it in GitHub Desktop.
PHP Redirect PHP, JS, HTML Meta Tag
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 | |
const OPTION_DEFAULTS = [ | |
'lang' => 'en', | |
'meta' => true, | |
'js' => true, | |
'title' => 'Redirection notice', | |
'link_sentence' => 'If you are not redirected automatically, follow <a href="%s">%s</a>', | |
'link_label' => 'this link', | |
'head_extra' => '<link rel="stylesheet" href="/css/style.css" />', | |
]; | |
function redirection($url) { | |
redirect_to($url); | |
terminate_program(); | |
} | |
function terminate_program($code=0) { | |
exit($code); | |
} | |
function set_http_header($header, $value) { | |
if (!headers_sent()) { | |
header("${header}: ${value}"); | |
} | |
} | |
function redirect_to($url, $options=[]) { | |
set_http_header('Location', $url); | |
output_redirection_html($url, process_options($options)); | |
} | |
function process_options($options) { | |
return array_merge(OPTION_DEFAULTS, $options); | |
} | |
function output_redirection_html($url, $options) { | |
?> | |
<!doctype html> | |
<html <?= $options['lang'] ? "lang=\"${options['lang']}\"" : '';?>> | |
<head> | |
<meta charset="UTF-8"> | |
<?php if($options['meta']): ?><meta http-equiv="refresh" content="0; url=<?= $url; ?>"> | |
<?php endif; ?> | |
<?php if($options['js']): ?> | |
<script type="text/javascript"> | |
window.location.href = "<?= $url; ?>" | |
</script> | |
<?php endif; ?> | |
<title><?= $options['title']; ?></title> | |
<?= $options['head_extra']; ?> | |
</head> | |
<body> | |
<?= sprintf($options['link_sentence'], $url, $options['link_label']); ?> | |
</body> | |
</html> | |
<?php | |
} |
output_redirection_html('https://www.google.com', ['link_label' => 'expected_text', 'js'=>false, 'meta'=>false, 'head_extra'=>'', 'lang'=>false])
Expected Output (captured from PHP+XDebug
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<title>Redirection notice</title>
</head>
<body>
If you are not redirected automatically, follow <a href="https://www.google.com">expected_text</a></body>
</html>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a filthy way to force redirect with several layers of fallback using PHP