Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Question On Header() Function
MacFly
post Nov 11 2004, 12:24 PM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 36
Joined: 9-November 04
Member No.: 2,202



Hello

I have a question regarding the header(location:http:// etc) function. I have a php document that looks similar to the followiing


<? include("./top.php"); //this file contains loads of html and is resident on all pages in the site IT HAS TO BE HERE

//now some code specific to this page only
if blah blah {

do some stuff

} else {

header("location : http://some_page.php");

}

include ("./bottom.php"); //this file contains loads of html and is resident on all pages in the site IT HAS TO BE HERE

-----

my question is this, how can i redirect to a new page using the header() function in the middle of a file like this ie when i have already passed a load of html and other stuff to the browser. When I run the above i get the following error

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/include/top-left.inc.php:129) in /var/www/html/comments.php on line 19

I am reasonably new to PHP and after investigation found out that to use the header() redirect there can not be any html or white space sent to the browser before the actual header() command

Im sure there is a really simple solution to this but im a bit stumped and would appreciate some help.
Go to the top of the page
 
+Quote Post
BoSZ
post Nov 11 2004, 12:32 PM
Post #2


the one with a short temper
*********

Group: Members
Posts: 247
Joined: 26-July 04
Member No.: 211



what about exchanging
header with include
Go to the top of the page
 
+Quote Post
greenmask
post Nov 11 2004, 10:29 PM
Post #3


Newbie [Level 1]
*

Group: Members
Posts: 11
Joined: 11-November 04
Member No.: 2,236



The only way I can think of doing this is adding the code into an if statement.

<?php

if ($something = something) {
include("top.php");

// page specific code here

include("bottom.php");

} else {
header("Location: http://whatever.com");
}

?>

-gm
Go to the top of the page
 
+Quote Post
Spectre
post Nov 17 2004, 06:24 AM
Post #4


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



Because the header() function sends raw headers, it has to be called before any other data is sent to the client - eg. via the echo() or printf() functions.

My guess is that the script top.php (which is being called first) is sending some kind of output - you will have to cancel this if you want to send headers.

greenmask's example should work fine.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Delphi(1)
  2. Getting List Of Directories And Files Using Php(6)
  3. Converting To Unix Timestamp(1)
  4. How To Use A Link To Call Function In Php?(8)
  5. Include File.php?id=something(13)
  6. Php Header Problem(11)
  7. Genuine Help With Smtp And Php Mail() Function(3)
  8. How Good Is This Data Cleaning Function?(2)
  9. [php](simple) Using Functions To Combine Values In A Form(2)
  10. Mail() Clone(5)
  11. The Best Zip Function(1)
  12. Site Header (request)(6)
  13. Can Somebody Edit A Flash Header For My Site?(5)
  14. Php Explode Function Help(4)
  15. Wordpress Error : Warning: Cannot Modify Header Information(5)
  1. How To Check If Fsockopen Function Is Enabled?(2)
  2. Aef Forum Modifications [resolved](3)
  3. Problem With Where Clause And Max Function(2)
  4. Arrays Outside A Function(3)
  5. Inkscape, A Open Source Vector App That Support Lot Function.(0)
  6. Help: Disable All Buttons Inside A Div Element(8)
  7. User Permission Function [php](3)
  8. Php - Forms, Date And Include(0)
  9. Endif function?(6)
  10. Question Regarding Php Function(12)
  11. PHP Function To Add Previous and Next Page Feature(5)
  12. Flash Header Edit?(2)
  13. Disabling Right Click Function(9)


 



- Lo-Fi Version Time is now: 8th October 2008 - 03:50 AM