38

i have this code,why my header location not working? its a form of updating and editing and deleting some pages in my control panel...and i have an index.php file in the same folder of form.php...any help please?()i tryed to put the header after the editing and deleting...and still go to the form page not the index...

<?php
include "../../includes/site_includes.php";
//send
if ((isset($_POST["send"])) && ($_POST["send"] == 1)) {
    $pageid = $_POST["page_id"];
    $pagetitle = $_POST["page_title"];
    $nameinmenu = $_POST["page_menu_name"];
    $nameinurl = $_POST["page_name_url"];
    $link = $_POST["page_link"];
    $picture = $_POST["page_pic"];
    $desc = $_POST["page_desc"];
    $content = $_POST["page_content"];
}
if ((isset($_POST["act"])) && ($_POST["act"] == "add")) {
    $sql = insertpage();
    if ($result = $mysqli->prepare($sql)) {
        $result->bind_param("sssssss", $pagetitle, $nameinmenu, $nameinurl, $link, $picture, $desc, $content);
        $result->execute();
        $result->store_result();
        $rows = $result->num_rows;
    }
}
////edit
if ((isset($_GET["act"])) && ($_GET["act"] == "edit")) {
    $sql = getfrompages();
    if ($result = $mysqli->prepare($sql)) {
        $rekza = $_GET["id"];
        $result->bind_param("i", $rekza);
        $result->execute();
        $result->store_result();
        $rowsZ = $result->num_rows;
    }
    if ($rowsZ > 0) {
        $row = fetch($result);
        $pageid = $row[0]["page_id"];
        $pagetitle = $row[0]["page_title"];
        $nameinmenu = $row[0]["page_menu_name"];
        $nameinurl = $row[0]["page_name_url"];
        $link = $row[0]["page_link"];
        $picture = $row[0]["page_pic"];
        $desc = $row[0]["page_desc"];
        $content = $row[0]["page_content"];
    }
}
if ((isset($_GET["act"])) && ($_GET["act"] == "delete")) {
    $thedelid = $_GET["id"];
    $sql2 = delpage();
    if ($result2 = $mysqli->prepare($sql2)) {
        $result2->bind_param("i", $thedelid);
        $result2->execute();
        $result2->store_result();
        $rowsZ2 = $result2->num_rows;
    }
}
header('location: index.php');
exit();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> pages add </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
<form method="post" action="">
        <table>
            <tr>
                <td style="font-weight:bold;">title</td>
                <td><input type="text" name="page_title" value="<?=$pagetitle?>" /></td>
            </tr>
            <tr>
                <td style="font-weight:bold;">name in menu</td>
                <td><input type="text" name="page_menu_name" value="<?=$nameinmenu?>" /></td>
            </tr>
            <tr>
                <td style="font-weight:bold;">name in url</td>
                <td><input type="text" name="page_name_url" value="<?=$nameinurl?>" /></td>
            </tr>
            <tr>
                <td style="font-weight:bold;">link</td>
                <td><input type="text" name="page_link" value="<?=$link?>" /></td>
            </tr>
            <tr>
                <td style="font-weight:bold;">picture</td>
                <td><input type="text" name="page_pic" value="<?=$picture?>" /></td>
            </tr>
            <tr>
                <td style="font-weight:bold;">description</td>
                <td><textarea name="page_desc"><?=$desc?></textarea></td>
            </tr>
            <tr>
                <td style="font-weight:bold;">content</td>
                <td><textarea name="page_content"><?=$content?></textarea></td>
            </tr>
            <tr>
                <td colspan="2">
                <input type="hidden" name="send" value="1" />
                <input type="hidden" name="act" value="<?=$_GET["act"]?>" />
                <input type="hidden" name="page_id" value="<?=$pageid?>" />
                <input type="submit" value="add" /></td>
            </tr>
        </table>
</form>
 </body>
</html>

solved: with @ Mihai Iorga code i added ob_start();

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
michael
  • 847
  • 4
  • 10
  • 17

18 Answers18

74

That is because you have an output:

?>
<?php

results in blank line output.

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.

also after header('location: index.php'); add exit(); if you have any other scripts bellow.

Also move your redirect header after the last if.

If there is content, then you can also redirect by injecting javascript:

<?php
    echo "<script>window.location.href='target.php';</script>";
    exit;
?>
Black
  • 18,150
  • 39
  • 158
  • 271
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
55

Try adding ob_start(); at the top of the code i.e. before the include statement.

air4x
  • 5,618
  • 1
  • 23
  • 36
10

just use ob_start(); before include function it will help

Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
subindas pm
  • 2,668
  • 25
  • 18
9

Remove Space

Correct : header("Location: home.php"); or header("Location:home.php");

Incorrect : header("Location :home.php");

Remove space between Location and : --> header("Location(remove space): home.php");

JavaFWS
  • 91
  • 1
  • 2
6

The function ob_start() will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So browser will not receive any output and the header will work.Also we should make sure that header() is used on the top of the code.

Suyash Jain
  • 561
  • 7
  • 18
5
ob_start(); 

should be added in the line 1 itself. like in below example

<?php
ob_start(); // needs to be added here
?>
<!DOCTYPE html>
<html lang="en">
// your code goes here
</html>
<?php
if(isset($_POST['submit']))
{ 
//code to save data in db goes here
}
header('location:index.php?msg=sav'); 
?>

adding it below html also doesnt work. like below

<!DOCTYPE html>
<html lang="en">
// your code goes here
</html>
<?php
ob_start(); // it doesnt work even if you add here
if(isset($_POST['submit']))
{ 
//code to save data in db goes here
}
header('location:index.php?msg=sav'); 
?>
Sashi
  • 240
  • 4
  • 14
2

I use following code and it works fine for me.

if(!isset($_SESSION['user'])) {
       ob_start();
       header("Location: https://sitename.com/login.php");
       exit();
} else { 

// my further code 

}
Hemi
  • 829
  • 1
  • 9
  • 12
2

I had same application on my localhost and on a shared server. On my localhost the redirects worked fine while on this shared server didn't. I checked the phpinfo and I saw what caused this:

enter image description here

While on my localhost I had this:

enter image description here

So I asked the system admin to increase that value and after he did that, everything worked fine.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
paulalexandru
  • 9,218
  • 7
  • 66
  • 94
2

for me just add ob_start(); at the start of the file.

1

It took me some time to figure this out: My php-file was encoded in UTF-8. And the BOM prevented header location to work properly. In Notepad++ I set the file encoding to "UTF-8 without BOM" and the problem was gone.

morcibacsi
  • 43
  • 7
1

Check if below are enabled

bz, mbstring, intl, ioncube_loader and Json extension.

0

It should be Location not location:

header('Location: index.php');
Mahdi
  • 9,247
  • 9
  • 53
  • 74
0

In my case i created new config file with function 'ob_start()' and added this to my .gitignore file.

Pankaj Agrawal
  • 1,469
  • 2
  • 17
  • 27
0

I use this

header("Location:comments.php");

And it solve out..

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
0

In your HTML code, you are using a form and setting action to "", which I understand takes precedence over a header within the form.

I found rather using the action element within the form instead of header Location is one option. I assume you want different options on the link, thus the variable.

<?php $nextLink = "index.php"; ?>
<form method="post" action="<?php echo $nextLink; ?>">

I suggest placing the variable outside a $_POST to start testing with.

0

In my case, it was extra spaces after ?>

Removed the spaces, and voila it worked.

Paolo
  • 390
  • 2
  • 5
  • 20
0

in my case i just added ob_start(); before include anything and it worked !

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 28 '21 at 07:27
-1

Create config.php and put the code it will work

john
  • 9
  • 4
  • 2
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["How do I write a good answer??"](https://stackoverflow.com/help/how-to-answer). You might also want to learn about ["Minimal, Complete, and Verifiable Examples"] (https://stackoverflow.com/help/minimal-reproducible-example). – Oddmar Dam Jun 15 '19 at 13:22