Skip to content

PHP redirect Class with time delay, OOP.

Share Button
<?php

/** PHP redirection with delay class
*   Takes two parameters: 
*   $delay      - time delay in seconds i.e. 4
*   $location   - redirect location i.e. linuxget.co.uk   
*/ 

class TimeDelayRedirect{
    
    private $delay;
    private $location;
    
    /** Two Parameters passed to the constructor */
    public function __construct($delay, $location){
        $this->delay = $delay;
        $this->location = $location;
    }
    
    /** Get time delay in seconds */
    private function getDelay(){
        return $this->delay;
    }

    /** Get URL redirect location */
    private function getLocation(){
        $preURL = "http://www.".$this->location;
        return $preURL;
    }
    
    /** header will refresh after x seconds and redirect to given url */ 
    public function runRedirect(){
        echo "Redirection to {$this->getLocation()} within {$this->getDelay()} seconds";
        header("refresh:{$this->getDelay()};url={$this->getLocation()}");
    }
}

/** Class instance with 2 arguments passed(number of seconds & link location) */
$redirectWithDelay = new TimeDelayRedirect(3, "linuxget.co.uk");
/** Calls delayRedirect() method */
$redirectWithDelay->runRedirect();
?>
Share Button