Jump to content
Killersites Forums

Problem with page redirecting: header("Location: $url");


quyen
 Share

Recommended Posts

Hello everybody,

 

This is a warning i've got whenever I use "header("Location: $url"); ".

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\index.php:15) in C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\user_manager.php on line 319

 

Is there any solution to redirect to a url without using "header("Location: $url"); Or somehow I can fix the above problem?

 

I've been reading some topics on some websites, and they are all about how to use header("Location: $url"); but i haven't found any right answer for my situation.

 

Note: "header("Location: $url");" runs ok on localhost at my computer, I only got this problem when I upload my project to a hosting.

 

Thank a lot for helping me!

Quyen,

Edited by quyen
Link to comment
Share on other sites

This may help.

http://forum.mamboserver.com/showthread.php?t=49427

 

Also

http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/

"3) Make sure there is no white space outside of the php start and end tags. While a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit." although some of my files have a blank space and still work.

 

Another reason is stated here:-

http://www.kirupa.com/forum/showthread.php?t=265403

"You're trying to modify header information after it's already sent to the browser. You need to move the PHP code that's trying to do this to the top of the page, above everything else, even a blank space."

Edited by Wickham
Link to comment
Share on other sites

Thanks for you reply,

 

I might have look at those links, and try to fix my problems. By the way, Do you know somehow we can redirect to a page without using header("Location: $url") ?.

 

In my case: I want to show a message before redirect to any pages?

 

Thank you very much for that.

Quyen,

Link to comment
Share on other sites

Well, I've tried that before but when you use headers to show a message before redirect to a page, actually you cannot see the message, the headers will execute straight away.

 

Yes, I suppose that's obvious and I should have thought of that !

 

It would be seen

1) if the header url didn't work

2) if you add in a delay to the header url function.

Link to comment
Share on other sites

I am not sure if this has been solved yet, but http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm may be of some assistance. After you understand how to use the meta tag you can then echo it to effectively redirect.

 

Ex. " echo ""; "

This will refresh the page at the new location ("http://example.com") in 2 seconds.

 

"http-equiv="refresh" " Is some what self explanatory, it will cause a refresh but if there is a url value in 'content' then it will refresh with a new location.

 

" content="2;url=http://example.com" " ' content="2 ' is where you can set the amount of time (in seconds) that the browser will wait before refreshing. ' url=http://example.com ' is where it will go upon refreshing.

 

It has proven a worthy alternative for me when "header("location: ")" redirecting just doesn't seem to fit.

 

Hope it helps you out.

 

-Adam

Edited by adamlen
Link to comment
Share on other sites

I've been trying to solve my problem by using http://www.desilva.biz/php/phprefresh.html and http://www.tizag.com/javascriptT/javascriptredirect.php, but I haven't solved my problems yet, I will try some other solutions, but I am still looking for someone to help me. I know "Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\index.php:15) in C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\user_manager.php on line 319" is just a warning, not an error, but the thing is I don't want this warning to show up on the page, and whenever this warning is occurs, it stops redirecting. I will try to use Adam's solution and see what'll happen....

 

Thank you very much Adam,

Quyen,

Link to comment
Share on other sites

if l wll be you, l will use a javascript.

 

define something like

$ready=;

 

 

then use javascript code and refer the $ready in the javascript.

 

It is very easy. If you can not handle it, let me know and l will post you a full written java script code!

 

 

However, your warning seems like you have two

 

if(isset($_POST['xxxx'])) {

header("Location: url");

 

 

if this is the case, do well to delete one since the error message is telling you that, the header has already sent a location and you are trying to overwrite on the older one....

 

 

 

 

 

"Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\index.php:15) in C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\user_manager.php on line 319"

 

check the line 319 of the user_mnager.php and the line 15 of the index.php. There is a conflict......>the index.php header you are trying to write has already been sent by the user_manager.php. And l bet you have included one of them in the other.

 

 

Best Regards

Link to comment
Share on other sites

Thanks for your reply,

 

Yeah you're right, I've used Header(); quite a lot in my php pages, ofcourse I used it for different purposes, I've been trying to comment all the //Headers, and use print "somthing"; to replaces Header commands, it works fine, but Whenever I remove one of the comment lines, the warning will appear straight away.

 

I am sure I did something wrong, but I haven't found it yet. Maybe I already used Header in the file which is required in the php page. But I think you can use multiple Headers command on the same page or include or require, right?

 

Thanks a lot for your help but really I don't want to use javascript in this case, because users can turn it off anytime, that means my website won't work ok without JS

 

Quyen,

Link to comment
Share on other sites

sure you can use header as many times as you want so far as they are in an

 

IF(){

 

yourcode

header('')

}elseif(){

yourcode 2

header('')

 

}

elseif(){

yourcode 3

header('')

 

}

 

same as for

Foreach(){} ect and also in a function myFunction(){}

 

Making sure none of the gloabal variables concides with another.

Link to comment
Share on other sites

Yeah this is my php code: it may show you something easy to understand.

 

               case 'formAdd':
                   require_once("menu_adding_form.php");
               break;
               case 'add':
                   $type = $_POST['type'];
                   $name = $_POST['name'];
                   $link_to = $_POST['link_to'];
                   $ordering = $_POST['ordering'];
                   $created_date = date("F j, Y, g:i a");
                   $last_edit = date("F j, Y, g:i a");
                   if(isset($_POST['published'])){
                       $status = 1;
                   }
                   else{
                       $status = 0;
                   }
                   if($name == ""){
                       header("location: alert_box.php?opt=empty");
                   }
                   else{

                       $table = "menu";
                       $cols = "menu_id,type,name,link_to,ordering,
                                status,created_date,last_edit
                                ";
                       $values = "'','$type','$name','$link_to','$ordering',
                                 '$status','$created_date','$last_edit'
                                 ";
                       $query = "INSERT INTO menu ($cols) 
                                 VALUES ($values)
                                ";
                       $result = mysql_query($query);
                       if($result){
                           header("location: index.php?content=menus");
                       }
                       else{
                           header("location: alert_box.php?opt=not_insert");
                       }
                   }
               break;
               case "more":
                   if(isset($_GET['id'])){
                       $menu_id = $_GET['id'];
                       $query = "SELECT * FROM menu 
                                 WHERE menu_id = '$menu_id'
                                 ";
                       $result = mysql_query($query);
                       $cnt_records = mysql_num_rows($result);
                       if($cnt_records > 0){
                           require("menu_editing_form.php");
                       }
                   }
               break;
               case "edit":
                   $menu_id = $_POST['menu_id'];
                   $type = $_POST['type'];
                   $name = $_POST['name'];
                   $link_to = $_POST['link_to'];
                   $ordering = $_POST['ordering'];
                   $last_edit = date("F j, Y, g:i a");
                   if(isset($_POST['published'])){
                       $status = 1;
                   }
                   else{
                       $status = 0;
                   }
                   if($name == ""){
                       header("location: alert_box.php?opt=empty");
                   }
                   else{
                       $set = "type = '$type', 
                               name = '$name',
                               link_to = '$link_to',
                               ordering = '$ordering', 
                               status = '$status', 
                               last_edit = '$last_edit' ";
                       $query = "
                                UPDATE menu
                                SET $set 
                                WHERE menu_id = '$menu_id'
                                ";
                       $result = mysql_query($query);
                       if($result){
                           header("location: index.php?content=menus");
                       }
                       else{
                           header("location: alert_box.php?opt=not_updated");
                       }
                   }
               break;

 

Thanks a lot

Quyen,

Link to comment
Share on other sites

ok, thefirst correction l will do in your code is to change the position of the require.

 

case 'formAdd':

require_once("menu_adding_form.php");

break;

 

 

should be:

<?php

require_once("menu_adding_form.php");

 

then switch something eg. switch($v){

 

case 'formAdd':

 

 

 

break;

 

 

}

?>

 

 

If u need more assist let me know..

Edited by christalix
Link to comment
Share on other sites

What is on line 15?

 

Or better yet, post the full page code, please.

There must be nothing (even a blank space) sent to the Browser before the header(), or that error results. Check in your included/required files as well.

 

*edit*

 

Wrap the entire page in an ob_start() function to avoid a leak to the browser.

 

http://php.net/manual/en/function.ob-start.php

Edited by jlhaslip
Link to comment
Share on other sites

Hello everyone,

 

Thanks a lot for your all replies, I do want to post all the coding which are require or include for you, but I'm wondering that my php pages are quite long, I hope you guys can understand all.

Please let me know if there is anything wrong. Sorry I didn't comment much in my php code, I should do that but sorry i am too busy. If you need any futher information, let me know pls.

Thanks a lot again

 

"The main problem occurs in menu_manager.php file, I think"

Quyen,

 

file content.php:


<?php 
if(!isset($_SESSION['user_id'])){
   header("location: alert_box.php?opt=no_permission");
}
else{
   if(isset($_GET["content"])){
       $content = $_GET["content"];
       switch($content){
           case "home":
           break;
           case "menus":
               require_once("menu_manager.php");
           break;
           case "users":
               require_once("user_manager.php");
           break;
           case "portfolio":
           break;
           case "cart":
           break;
       }
   }
}
?>

file menu_manager.php:

<?php                
   $query = "SELECT * FROM menu 
             ORDER BY last_edit DESC";
   $result = mysql_query($query);
   $cnt_records = mysql_num_rows($result);
   if($cnt_records){
   $per_page = 10;
   $start = 0;
   $num_of_page = ceil($cnt_records/$per_page);
   if(isset($_GET['page'])){
       $get_request = $_GET['page'];
       if($get_request!="")
       {
           $start = ($get_request-1) * $per_page;
       }
       else
       {
           $start = 0;
       }
   }
   else{
   }
   ?>




           <?php 
           if($num_of_page >=2){
           ?>

               Page:

Select one
                   <?php 
                   for($index=1;$index <= $num_of_page;$index++)
                   {
                       if($get_request == $index)
                       {

                           echo "$index ";
                       }
                       else
                       {
                           echo "$index ";
                       }    
                   }
                   ?>

               <?php 
               if(isset($_POST['page'])){
                   $selected = $_POST['page'];
                   for($index=1;$index <= $num_of_page;$index++)
                   {
                       if($get_request == $selected)
                       {
                           header("location: index.php?content=menus&page=$selected");
                       }
                       else
                       {
                           header("location: index.php?content=menus&page=$selected");
                       }    
                   }
               }
               ?>

           <?php 
           }
           ?>


   
               Item: 

Select one
               <?php 
               if($cnt_records){
                   while($row = mysql_fetch_array($result)){
                       ?>

                           <?php 
                           print $row['name']."--"; 
                           if($row['type']==0){print "B";}else{print "F";}
                           ?>

                       <?php
                   }
               }
               ?>





               Quick launch: 

Select one
All
Back-End
Front-End
Published
Un-Published

   




Delete


Published


None


Select All



Add


       <?php 
       if(!isset($_GET['act'])){
           require_once("menu_content.php");
       }
       else{
           $action = $_GET['act'];
           switch($action){
               case"item":
                   if(isset($_POST['menu_id'])){
                       $menu_id = $_POST['menu_id'];
                       $query = "SELECT * FROM menu
                                 WHERE menu_id = '$menu_id'
                                 ";
                       $result_items = mysql_query($query);
                       $cnt_records = mysql_num_rows($result_items);
                       if($cnt_records){
                       ?>

IDGroupNameLink name OrderingPublishedDetailsEditDelete#

                       <?php    
                       }
                   }
               break;

               case "pos":
                   if(isset($_POST['pos'])){
                       $ordering= $_POST['pos'];
                       $menu_id = $_POST['menu_id'];
                       $last_edit = date("F j, Y, g:i a");
                       $query = "UPDATE menu
                               SET ordering = '$ordering',last_edit = '$last_edit'
                               WHERE menu_id = '$menu_id' 
                               ";
                       $result = mysql_query($query);
                       $cnt_records = mysql_num_rows($result);
                       if($result){
                           header("location: index.php?content=menus");
                       }
                   }
               break;
               case 'formAdd':
                   require_once("menu_adding_form.php");
               break;
               case 'add':
                   $type = $_POST['type'];
                   $name = $_POST['name'];
                   $link_to = $_POST['link_to'];
                   $ordering = $_POST['ordering'];
                   $created_date = date("F j, Y, g:i a");
                   $last_edit = date("F j, Y, g:i a");
                   if(isset($_POST['published'])){
                       $status = 1;
                   }
                   else{
                       $status = 0;
                   }
                   if($name == ""){
                       header("location: alert_box.php?opt=empty");
                   }
                   else{

                       $table = "menu";
                       $cols = "menu_id,type,name,link_to,ordering,
                                status,created_date,last_edit
                                ";
                       $values = "'','$type','$name','$link_to','$ordering',
                                 '$status','$created_date','$last_edit'
                                 ";
                       $query = "INSERT INTO menu ($cols) 
                                 VALUES ($values)
                                ";
                       $result = mysql_query($query);
                       if($result){
                           header("location: index.php?content=menus");
                       }
                       else{
                           header("location: alert_box.php?opt=not_insert");
                       }
                   }
               break;
               case "more":
                   if(isset($_GET['id'])){
                       $menu_id = $_GET['id'];
                       $query = "SELECT * FROM menu 
                                 WHERE menu_id = '$menu_id'
                                 ";
                       $result = mysql_query($query);
                       $cnt_records = mysql_num_rows($result);
                       if($cnt_records > 0){
                           require("menu_editing_form.php");
                       }
                   }
               break;
               case "edit":
                   $menu_id = $_POST['menu_id'];
                   $type = $_POST['type'];
                   $name = $_POST['name'];
                   $link_to = $_POST['link_to'];
                   $ordering = $_POST['ordering'];
                   $last_edit = date("F j, Y, g:i a");
                   if(isset($_POST['published'])){
                       $status = 1;
                   }
                   else{
                       $status = 0;
                   }
                   if($name == ""){
                       header("location: alert_box.php?opt=empty");
                   }
                   else{
                       $set = "type = '$type', 
                               name = '$name',
                               link_to = '$link_to',
                               ordering = '$ordering', 
                               status = '$status', 
                               last_edit = '$last_edit' ";
                       $query = "
                                UPDATE menu
                                SET $set 
                                WHERE menu_id = '$menu_id'
                                ";
                       $result = mysql_query($query);
                       if($result){
                           header("location: index.php?content=menus");
                       }
                       else{
                           header("location: alert_box.php?opt=not_updated");
                       }
                   }
               break;
               case "delete":
                   if(!isset($_GET['id'])){}
                   else{
                       $menu_id = $_GET['id'];
                       $query = "DELETE FROM menu
                                 WHERE menu_id = '$menu_id';
                                ";
                       $result = mysql_query($query);
                       if($result){
                           header("index.php?content=menus");
                       }
                       else{
                           print mysql_error();

                           header("alert_box.php?opt=delete_error");
                       }
                   }
               break;

               case "status":
                   if(isset($_GET['id']) && isset($_GET['cur'])){
                       $menu_id = $_GET['id'];
                       $last_edit = date("F j, Y, g:i a");
                       $current_status = $_GET['cur'];
                       if($current_status == 0){
                           $status = 1;
                       }
                       elseif($current_status == 1){
                           $status = 0;
                       }
                       else{
                           header("location: alert_box.php?opt=no_permission");
                       }
                       $query = "UPDATE menu
                                 SET status = '$status', last_edit = '$last_edit' 
                                 WHERE menu_id = $menu_id
                                ";
                       $result = mysql_query($query);
                       if($result){
                           header("location: index.php?content=menus&page=1");
                       }
                       else{
                           header("location: alert_box.php?opt=not_updated");
                       }
                   }
               break;
           }
       }
   } 
?>

 

Required files:

menu_list.php



       <?php print $row['menu_id']?>


       <?php 
       if($row['type'] == 1){
           print "Front-end";
       }
       else{
           print "Back-end";
       }
       ?>


       <?php print $row['name'];?>


       <?php print $row['link_to'];?>



       <?php 
           print $row['ordering'];
           $current_id = $row['menu_id'];
       ?>

       <?php 
       if(!isset($_GET['act'])){
       }
       else{
           ?>

Select
               <?php 
               $query = "SELECT * FROM menu";
               $result = mysql_query($query);
               $cnt_records = mysql_num_rows($result);
               for($i= 1; $i<= $cnt_records; $i++){
                   ?>
<?php print $i;?>
                   <?php
               }
               ?>

        <?php 
            }
        ?>



       <?php 
       if($row['status'] == 1){
           print "Yes ";
       }
       else{
           print " No  ";

       }
       ?>


Detail


Edit


Delete








 

menu_adding_form.php

 



Add a new menuitemGroup of menu 

Front-End
Back-End

Menuitem nameLink to

Home page
About Us
Web Designs
Web Templates
Contact Us
Shopping Cart

Menus
Users

Ordering

                   <?php 
                   $query = "SELECT * FROM menu";
                   $result = mysql_query($query);
                   $cnt_records = mysql_num_rows($result);
                   for($i= 1; $i<= $cnt_records; $i++){
                       print ''.$i.'';
                   }
                   ?>
<?php print $cnt_records + 1?>

Published

 Enter or click






 

menu_editing_form.php


Detail of menuitems  " <?php print $menu_id; ?> "Group of menu 

                   <?php 
                   while($row = mysql_fetch_array($result)){
                   ?>

                       <?php 
                       $menu_id = $row['menu_id'];
                       $type = $row['type'];
                       $name = $row['name'];
                       $link_to = $row['link_to'];
                       $status = $row['status'];
                       $ordering = $row['ordering'];
                       $created_date = $row['created_date'];
                       $last_edit = $row['last_edit'];
                       if($type ==  0){
                           print "Back-End";
                       }
                       else{
                           print "Front-End";
                       }
                       ?>

                   <?php 
                   }
                   if($type == 0){
                       ?>
Front-End
                       <?php
                   }
                   else{
                       ?>
Back-End
                       <?php
                   }
                   ?>


Menuitem nameLink to


<?php print $link_to;?>
                   <?php 
                   $query = "SELECT * FROM menu
                             WHERE link_to <> '$link_to'
                             ORDER BY ordering ASC
                            ";
                   $result = mysql_query($query);
                   $cnt_records = mysql_num_rows($result);
                   if($cnt_records){
                       while($row = mysql_fetch_array($result)){
                           $type = $row['type'];
                       ?>

                           <?php 
                           print $row['link_to']."--";
                           if($type == 0){
                               print "Back";
                           }
                           else{
                               print "Front";
                           }
                           ?>

                       <?php
                       }
                   }
                   ?>

Ordering

                   <?php 
                   $query = "SELECT * FROM menu";
                   $result = mysql_query($query);
                   $cnt_records = mysql_num_rows($result);
                   for($i= 1; $i<= $cnt_records; $i++){
                   $start = $i + 1;
                   ?>
selected="selected" <?php }?>>
                       <?php  print $i;?>

                   <?php
                   }
                   ?>

Published
checked<?php } ?> />
Created on 
<?php print $created_date; ?>
Last edited
<?php print $last_edit ; ?>
 Enter or click






Edited by quyen
Link to comment
Share on other sites

  • 4 weeks later...

The server parsing this sgml document will add the necessary header(s) to Location: so the output from your PHP should be limited to print "Location: $url"; rather than header("Location: $url") ( would have the headers output in duplicate).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...