Cookie Based 302 Redirector

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

If you have a site that is installed on your live server but for the moment you do not want to go live, this little code snippet will allow you to redirect any visitors who don’t have a secret GET variable in the URL to a holding subdomain with a 302 redirect.

A 302 redirect is a temporary redirect. Search engine spiders will see this as a temporary thing and will not remove the original URL from their index.

Just put this at the top of any page you want to protect. For a Magento or any other MVC php site, just putting this on the top of the index.php file will protect the whole system.

Note this isn’t real security, its just something to keep the spiders out. Please don’t regard it as secure.

if(isset($_GET['letmein'])){
	setcookie('letmeinmydomain.com');
}

if(!isset($_COOKIE['letmeinmydomain.com']) && !isset($_GET['letmein'])){
	header('HTTP/1.1 302 Found');
	header('Location: http://coming-soon.mydomain.com');
	exit;
}

Tags: php cookie302 redirect