1

First of all don't think that its dublicate question. I have tried all sollutions but nothing helps me.

I get the following error: "Cannot modify header information - headers already sent by (output started at /home/gogiavag/public_html/maxkapital/user.php:7) in /home/gogiavag/public_html/maxkapital/func.php on line 4"

All pages I have converted to utf8 (without BOM). I have no leading space in begining, but besides nothing helps.

Here is my code:

login.php

<?php session_start();?> 
 <html>
 <head>
 <meta charset="utf-8" /> 
 <link rel="stylesheet" type="text/css" href="style.css"/>
 </head>    
 <body>
 <?php include "header.php"; require_once 'func.php';?> 
    <form method="POST" action="user.php">
    <table style="margin-top: 10px;">
    <tr>
    <td><label for ="txtuser">name:</label></td>
    <td><input type="text" style="padding:5px;" id="txtuser" name="txtuser" value="<?php if (isset($_SESSION['txtuser'])
    ){echo $_SESSION['txtuser'];}else{echo '';} ?>"  </input></td>
    </tr>
    <tr>
    <td><label for ="txtpassword">password:</label></td>
    <td><input type="password" style="padding:5px;" id="txtpassword" name="txtpassword"> </input></td>
    </tr>

    <tr>
    <td></td>
    <td><input type="submit" value ="Enter" style="padding:5 55 5 55;background-color:#3f4194;color:#fff;" name="btnsubmit" id="btnsubmit"> </td>
    </tr>   

    </table>    
    </form>

    <?php 
    if (isset($_SESSION['err'])){
        if ($_SESSION['err']===true){
            echo gg_stringformat("<img src='error.png' style='margin-left:50px;'><img/> <span style='font-size:10pt; color:#ff0000'>{0}</span>",     $_SESSION['errmsg']);
        }   
    }

    if(isset($_SESSION['err'])){unset ($_SESSION['err']);};
    if(isset($_SESSION['errmsg'])){unset ($_SESSION['errmsg']);};
    if(isset($_SESSION['txtuser'])){unset ($_SESSION['txtuser']);};
    if(isset($_SESSION['txtpassword'])){unset ($_SESSION['txtpassword']);};
    ?>

</body> 

</html>

user.php

<?php session_start();?>
<html>
<head>
<meta charset="utf-8"/> 
</head>
<body>
<?php require_once'func.php';
if (!isset($_POST['btnsubmit'])){
    gg_redirect('block.php');
    exit;
}
$user=$_POST['txtuser'];
$pass=$_POST['txtpassword'];
$_SESSION['txtuser'] = $user;
$_SESSION['txtpassword'] = $pass;
if (gg_trim($user)===''){
    $_SESSION['err']=true;
    $_SESSION['errmsg']='User name required';
    gg_redirect('login.php');
    exit;
}elseif(gg_trim($pass)===''){$_SESSION['err']=true;$_SESSION['errmsg']='Password required';gg_redirect('login.php');
 exit;  
} 
echo $user, "<BR>", $pass;
?>
</body>
</HTML>

header.php

<div id="divheader" >
<p> <img src="coins.png"></img>MAX_KAPITAL</p>
</div>

func.php begins with ...

<?php
    mb_internal_encoding("UTF-8");
    function gg_redirect($url){
        header("location: $url");
    }

....

It gives me the error when user don't enters password or username.

Please find error in my code. thanks in advance.

regards George Gogiava

GGSoft
  • 439
  • 6
  • 15
  • 1
    Call `exit();` after `header()`, otherwise script execution will continue. Setting header alone is not enough to redirect. – Rajdeep Paul Dec 03 '15 at 11:21
  • **I am afraid it is a duplicate!** You are sending HTML to the browser before running the `gg_redirect()` – RiggsFolly Dec 03 '15 at 11:34
  • @RajdeepPaul Its more relevant that they are sending 5 lines of HTML to the output buffer before calling the `gg_redirect()` – RiggsFolly Dec 03 '15 at 11:39

2 Answers2

3

PHP is not lying to you, you indeed already started output at line 2 in user.php - you print <html> to response there.

Then you print <head> and some more HTML, then you call the function gg_redirect() from func.php if !isset($_POST['btnsubmit']), which causes the error, because it is not longer possible to send the redirect header since output was started already.

You need to check the inputs and possibly redirect before you send anything back to the client (apart of other response headers)., specifically, don't print any HTML before you're done handling the possible redirects:

<?php
    // includes here - they must have no output!
    // check if all is OK, set $redirectURL if redirect is needed to that URL
    if ($redirectUrl) {
        header("location: $redirectUrl");
        exit(); // header() won't cause the script to stop executing
    }
?>
<html>
<head>
...

The files included before the redirect must not print any output - not even a blank line, so they must all have <?php as the first characters of the file, whole file must be PHP without any output to response body, and must end with ?> with no newline or space afterwards (PHP may trim some whitespace in this case but don't rely on that).

Call to session_start() is safe and can be before the redirect (useful if you need session variables), since it will not send any response body. It may set a cookie, but that's OK because cookies are sent in headers.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • @Jin thanks for your quick reply. but I don't know how to rearange my code now. please shortly about main principes. – GGSoft Dec 03 '15 at 11:27
  • @Jiri Tousek, watch out for the includes before the redirect. If the OP happens to include a php script that already has headers, the same problem can occur. – davejal Dec 03 '15 at 11:44
1

While @Jiri already explained it correctly, to be more explicit:

move this:

<?php require_once'func.php';
if (!isset($_POST['btnsubmit'])){
    gg_redirect('block.php');
    exit;
}

to the very top of you php script, maybe even adding the first line to it like this:

<?php 
if (!isset($_POST['btnsubmit'])){
    gg_redirect('block.php');
    exit;
}
require_once'func.php';
session_start();
?>

and then the rest of your page.

EDIT The func.php is adding headers, by including it before your redirect, you get that error. Move the inlude line to some place after the redirect or check your func.php, see the edited code above

davejal
  • 6,009
  • 10
  • 39
  • 82
  • 1
    `session_start()` should be no problem, and session might be needed for the redirect decision in a general case. It's good that you mentioned it though so we can discuss it. – Jiri Tousek Dec 03 '15 at 11:38
  • @JiriTousek, that's correct. I just moved it to be save, you never know If the OP wants to include any other script in the top and that also already has headers ... – davejal Dec 03 '15 at 11:42
  • @Jiri Cannot modify header information - headers already sent by (output started at /home/gogiavag/public_html/maxkapital/user.php:13) in /home/gogiavag/public_html/maxkapital/func.php on line 4 – GGSoft Dec 03 '15 at 12:12
  • ", $pass; ?> – GGSoft Dec 03 '15 at 12:13
  • @davejal where is the error? – GGSoft Dec 03 '15 at 12:14
  • the error is in your func.php – davejal Dec 03 '15 at 12:53
  • ould you add that also? – davejal Dec 03 '15 at 12:53
  • @Jiri I have removed tags and it has been done, thanks, but a bit problem is that when I removed meta tag I haven't UTF-8 support on web page. how to archieve it? it's last problem and I will cloce theme. Unfortunatly I can't select both question as usefule, but i will vote up both. – GGSoft Dec 04 '15 at 09:36
  • @Jiri I have done. When I wrote meta teg after php script, it supports utf-8, thanks a lot all who took part. – GGSoft Dec 04 '15 at 09:58