简体   繁体   中英

PHP die() shows brief white screen

I'm using a redirect function to redirect visitors to a certain page when they don't have access to the current page. The function is as follows:

$user->redirect('www.google.com');
die();

The redirect function links to:

public function redirect($url="",$time=0) {
    header("Refresh: $time; url=$url");
}

Whenever you get redirected you briefly see a white page followed by the page you get redirected to. I'm assuming the white page is caused by die() .

I don't like it and I really want to get rid of this.

The problem is not related to the die() call, but that you are sending the wrong header to handle a redirection.

Instad of doing

public function redirect($url="",$time=0) {
    header("Refresh: $time; url=$url");
}

do:

public function redirect($url) {
    header("Location: $url", true);
}

Or even:

public function redirect($url, $temporal = false) {
    header("Location: $url", true, $temporal ? 302 : 301);
}

With this you are sending a status code "301" or "302", to indicate that the redirection is either temporal, or a permanent. Defaulting to a permanent redirect, since temporals are very rarely useful (although in your case, being a redirection because of permissions, a temporary redirection might be in order)

(I also removed the default value for $url , because it doesn't make much sense to redirect to an empty url)

Instead of using a 'Refresh:' header, you can just use 'Location:'. This way, the page does not have to be refreshed and will probably result in your desired behaviour.

public function redirect($url="/") {
    header("Location: $url");
}

edit: added default $url '/' so that if no url was given it redirects to homepage

I'm disapointed to the prev. posts. It is about the die() !

When sending a header the browser takes a little time to take action. Between this time the php process then calls die() before you are redirected. This is asyc. Location and Redirect So it shows you a black page and then you/visitors get redirected.

Good: setting die() to end the process to avoid futher actions inside your program

Bad: The blank page first.

This is depending on your enviroment of your browser! Eg: a very slow workstation will show you this behavior and you have to care about. Imagin the browser does know about header redirections (Today it (mostly) doesnt exists anymore)

To give the browser a change to take action, use this:

function someaction() {
    // ... 
    $user->redirect('www.google.com');
    // which give the time for the browser  
    // without further execution of your code
    return; 
}

In this case the visitor stays as long on the current page until the browser knows about headers or the cpu is slow but will take action when it is able to take action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM