Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

header location not working in my php code

Tags:

php

header

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();

like image 534
michael Avatar asked Sep 21 '12 06:09

michael


People also ask

Why my header in PHP is not working?

Solution to the Problem To solve this problem, we have to store the header in a buffer and send the buffer at the end of the script, so to store the header in the buffer, we will use the php ob_start() function and to clean the buffer, we will use the ob_end_flush() function. See the below code snippet. This is it!

What is PHP header location?

Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory. replace: It is optional which indicates whether the header should add a second header or replace previous.

How do headers work in PHP?

The header() function in PHP sends a raw HTTP header to a client or browser. Before HTML, XML, JSON, or other output is given to a browser or client, the server sends raw data as header information with the request (particularly HTTP Request).

What PHP can do with header () command?

The header() function is an predefined PHP native function. With header() HTTP functions we can control data sent to the client or browser by the Web server before some other output has been sent. The header function sets the headers for an HTTP Response given by the server.


2 Answers

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; ?> 
like image 130
Mihai Iorga Avatar answered Oct 04 '22 06:10

Mihai Iorga


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

like image 24
air4x Avatar answered Oct 04 '22 05:10

air4x