开发者

Redirect pages in PHP

开发者 https://www.devze.com 2023-01-27 02:24 出处:网络
Is there is a way to redirect pages in PHP without using header(\'Location:\'.\'\'); becouse it always make problems with sessions (:Cannot send session cache limite开发者_开发百科r - headers already

Is there is a way to redirect pages in PHP without using header('Location:'.''); becouse it always make problems with sessions (:Cannot send session cache limite开发者_开发百科r - headers already sent )


There are other options for redirecting in HTML (like using META or JavaScript). But it’s not a proper HTTP redirection.

So instead of looking for an alternative you should better get the HTTP variant working. In this case you can only modify the HTTP header if it hasn’t been sent already. And that happens whenever you do some output (either explicitly or implicitly). You can prevent that by buffering the output calling the output control’s ob_start before any output had happened, possibly directly at the start of the script file you have requested:

<?php
ob_start();

// rest of the file

Make sure that there is nothing before the opening PHP tag <?php like blank lines or a UTF BOM.


No, without header you're out of luck (you could use Apache's mod_rewrite but that would happen before your file is even touched).

Why not use ob_start (output buffering). See the PHP manual for a quick-start.

Good luck


As has been mentioned before use ob_start() right at the very top of your application page. Do not implement the dodgy javascript functions mentioned in other answers, I'd say they are extremely bad practice. PHP has been around for a while now so its tested and works, you might have erroneous white space or be outputting something to the browser then trying to modify header information which you cant do. So:

use ob_start check for erroneous white space and delete it

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号