Slapped some shit together
This commit is contained in:
parent
aeb8615383
commit
05ed8f0e5a
3 changed files with 103 additions and 0 deletions
23
composer.json
Normal file
23
composer.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "joshtronic/php-redirect",
|
||||
"description": "Permanent (301) and temporary (302) redirection",
|
||||
"version": "1.0.0",
|
||||
"type": "library",
|
||||
"keywords": ["http", "response", "redirect", "forward", "permanment", "temporary", "301", "302"],
|
||||
"homepage": "https://github.com/joshtronic/php-redirect",
|
||||
"license": "MIT",
|
||||
"authors": [{
|
||||
"name": "Josh Sherman",
|
||||
"email": "hello@joshtronic.com",
|
||||
"homepage": "http://joshtronic.com"
|
||||
}],
|
||||
"require-dev": {
|
||||
"php": ">=4.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"satooshi/php-coveralls": "dev-master"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {"joshtronic\\": "src/"}
|
||||
}
|
||||
}
|
54
src/Redirect.php
Normal file
54
src/Redirect.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Redirect
|
||||
*
|
||||
* Redirects the browser to another URL. Stops execution as to not run code
|
||||
* erroneously due to output buffering. HTTP/1.1 request an absolute URI, hence
|
||||
* the inclusion of the scheme, hostname and absolute path if :// is not found.
|
||||
* Don't hate the player, hate the RFC.
|
||||
*
|
||||
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
|
||||
* @usage joshtronic\Redirect('/dest'); // Permanent (301)
|
||||
* @usage joshtronic\Redirect('/dest', false); // Temporary (302)
|
||||
*/
|
||||
|
||||
namespace joshtronic;
|
||||
|
||||
class Redirect
|
||||
{
|
||||
/**
|
||||
* @param string $destination URL to redirect to
|
||||
*/
|
||||
public function __construct($url, $permanent = true)
|
||||
{
|
||||
if (strpos($destination, '://') === false) {
|
||||
if (
|
||||
!isset($_SERVER['HTTPS'])
|
||||
|| $_SERVER['HTTPS'] == 'off'
|
||||
|| $_SERVER['HTTPS'] == ''
|
||||
) {
|
||||
$destination = 'http';
|
||||
} else {
|
||||
$destination = 'https';
|
||||
}
|
||||
|
||||
$destination .= '://' . $_SERVER['HTTP_HOST'] . $destination;
|
||||
}
|
||||
|
||||
if ($permanent) {
|
||||
$code = 301;
|
||||
$message = $code . ' Moved Permanently';
|
||||
} else {
|
||||
$code = 302;
|
||||
$message = $code . ' Found';
|
||||
}
|
||||
|
||||
header('HTTP/1.1 ' . $message, true, $code);
|
||||
header('Status: ' . $message, true, $code);
|
||||
|
||||
header('Location: ' . $destination);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
26
tests/RedirectTest.php
Normal file
26
tests/RedirectTest.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
require_once '../src/Redirect.php';
|
||||
|
||||
class RedirectTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testPermanentRedirectWithProtocol()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function testTemporaryRedirectWithProtocol()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function testPermanentRedirectWithoutProtocol()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function testTemporaryRedirectWithoutProtocol()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue