开发者

Confused about how and when to use header() to redirect to different page?

I had been using the header() a lot in my application to redirect to different pages, have a look at my code below.i have put the code in top of the page and i haven't used any comments

if(isset($_SESSION['loggedin'])){
header("Location: dashboard.php");
exit;
}
else {
require_once('models/validation.php');
require_once('config/database.php');  

and below on line 50 i have put on some codes to check the login credentials and redirect it to dashboard.php

if( !empty($_POST['username']) || !empty($_POST['password'])) {
if( check_login($_POST['username'], $_POST['password'])) {
    header("Location: dashboard.php");
    exit;    

the first set of code works fine because when i login and try to access the index.php the first set of code come into action and push me back to dashboard.php

i am facing the problem with the second set of code below when i lo开发者_Go百科gin successfully it does not redirect me to dashboard.php instead it gives me the following error

Warning: Cannot modify header information - headers already sent by (output started at /home/bhatkaln/public_html/test/admin-login/index.php:17) in /home/bhatkaln/public_html/test/admin-login/index.php on line 54

where i am going wrong? is it fine to use header() for redirecting or i should consider using alternative

<meta http-equiv="refresh" content="0;dashboard.php"/>";

Edit : Sorry Guys i updated my error code and here is my line 53, 54 and 55.

if( check_login($_POST['username'], $_POST['password'])) {
    header("Location: dashboard.php");
    exit;


Header() must be called before any other output from your PHP.


Yes, it is fine to use header() for redirecting. Moreover its the only standard way.
Thus, you have to repair this error. Just check line 17 in the index.php as it stated in the error message. It's always worth to read error messages diligently

It seems more clarification needed.

Looking onto our site, we can discover that it works using HTTP protocol.
Looking onto HTTP protocol we can discover that it's very simple plain text protocol, and server response have very primitive structure: it's just some lines of text, divided by 2 parts: control headers part and page body part. Parts being separated by empty line. First empty line in the server response is a separator.

Thus, usual HTTP server response looks like

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sun, 19 Sep 2010 15:15:44 GMT
Content-Length: 7644

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

    <title>Frequently Asked Questions - Stack Overflow</title>

We can notice an empty line between headers and body. All other empty lines do not have any special effect, only first one does.

There is also a rule: Headers must be always present. There can be headers without body, but no page body can be shown without at least one header.

So, the rest is clear: it's impossible to send a header, if there is body part been sent already. An empty line - a separator - was already sent and there is no way to tell a browser that it's header we are sending right now. It will be interpreted as a part of body and has no special effect. Thus, PHP won't send it but raise an error.

To prevent such errors, a wise application layout should be used. In general, each page should be consist of 2 parts: logic part and visualization part. No visualization should be started before all logic is done.
Knowing that, we can assume that having index.php with part of HTML layout is very bad idea. Very tempting but very bad and misleading. You can use index.php as a front controller all right, but it should contain no HTML tags. But instead include a file.

Make your page layout like this (assume we don't use front controller but separate scripts):

news.php:

<?
include "config.php";
//you can do any redirects here

$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);
$tpl_file = "tpl.news.php";
//or here
//only if everything is OK and you have all data ready - start output:
include "template.php";
?>

template.php (it's something like your index.php):

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>

where tpl.news.php is not a regular PHP file that contains any data manipulations but just a template, contains only (D)HTML and basic PHP used to display previously prepared data. That's very important! If you gonna use $page as a regular php script, it will spoil the whole idea.


This sounds more like you have whitespace in one of your include files then an improper use of headers. Check the validation.php file to make sure there isn't white space or something being echo'd out on line 141.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜