Werbung

Werbung

Werbung

Archive

Folge uns auf Twitter

Werbung

Jede Menge neue Twitter Follower bekommen. Die Software hat einen kostenlosen Trial Mode, der bis zu 250 Follower bringt.

Willkommen

Willkommen im PHP Archiv Blog! Hier gibts Infos zum Thema PHP Codeschnipsel und Tipps. Bei unserem Projekt phparchiv.de, finden Sie über 5700 Scripte.

Tages-Archive: 16. Februar 2012

Safe redirect mit PHP

Diese kleine Funktion garantiert, dass Besucher zu einer URL weitergeleitet werden. Zuerst versucht die Funktion, den Benutzer mit der header() Funktion weiterzuleiten, geht das nicht, wird Javascript und META-Refresh versucht. Wenn auch das nicht geht, wird ein Link angezeigt.

function safe_redirect($url, $exit=true) {

    // Only use the header redirection if headers are not already sent
    if (!headers_sent()){

        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $url);

        // Optional workaround for an IE bug (thanks Olav)
        header("Connection: close");
    }

    // HTML/JS Fallback:
    // If the header redirection did not work, try to use various methods other methods

    print '';
    print 'Redirecting you...';
    print '';
    print '';
print '';

 // If the javascript and meta redirect did not work,
 // the user can still click this link
 print 'You should be redirected to this URL:
';
 print "<a href="$url">$url</a>

";

 print 'If you are not, please click on the link above.
';

 print '';
    print '';

    // Stop the script here (optional)
    if ($exit) exit;
}

Quelle: jonasjohn.de[/php]