[SOLVED] header location

This is a discussion on "[SOLVED] header location" within the PHP Forum section. This forum, and the thread "[SOLVED] header location are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 12th, 2007, 10:21
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] header location

I know headers have got to be before any output to the browser but I have finally come up against a problem where i need to use header ( "Location:

This is a very cut down version of the layout for my site and you can see that I have a switch that gets the page from the url and includes that page for display
PHP: Select all

<?php 
   session_start
(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
</head>
<body>
<div id="wrapper">
 <div id="content">
     <div id="header">
  <img src="images/title.jpg" alt="title" />
     </div>
     <div id="left">
  <div class="news">
      <div class="heading">
   <h3 class="head">Navigation</h3>
      </div>
      <-- Navigation goes here -->
  </div>
   
     </div>
     <div id="content2">
      <?php
   
//Main page content changes here
       
switch ($_GET['page'])
           {
       case 
"user_details":
       include(
'user_details.php');
       break;
             default:
       include(
'home.php');
           }
          
?>
     </div>
    </div>
</div>
<div id="footer"> 
</div>
</body>
</html>
The problem that I'm going to come up against in one of the pages that get included, there is a header ("Location: and I'm pretty sure that I'm going to get that headers already sent message.

Is there any way I can get around this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 12th, 2007, 10:27
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

Well, you could use a javascript redirect.....

Otherwise you are going to have to include your pages before you output anything.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 12th, 2007, 10:34
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

You could use output buffering. This will store all of the output until requesting it be sent to the page.

put ob_start(); at the top of your script and ob_end_flush(); when you know you won't have to change the headers after that point.

This will allow you to send headers after 'writing' to the output buffer.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 12th, 2007, 14:13
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

In your opinion, do you think I should try and rewrite my site so that and headers are at the top of th page before anything is being output or stick with what I have got and use ob_start() and ob_flush().

The second option seems a much quicker solution as well as saving a lot of time and hard work and also saves on duplicated code but it could be good practice to learn the other way.

Any opinions?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 12th, 2007, 14:17
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

There's no big issue with using output buffering and it gives you the ability to perform functions on ALL the data before outputing it.

The problem comes when you have a large page and you're storing it in a buffer. Imagine a large page being hit by 100 users at once -- could bring down a shared server for a time.

In most cases though there is more advantages than not.. I rarely use it as I usually send all headers before outputting anything.. It would be coding prefence really.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 12th, 2007, 15:38
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: header location

Quote:
Originally Posted by Rakuli View Post
You could use output buffering. This will store all of the output until requesting it be sent to the page.

put ob_start(); at the top of your script and ob_end_flush(); when you know you won't have to change the headers after that point.

This will allow you to send headers after 'writing' to the output buffer.

Cheers
Thanks again Rakuli for that, added to my collection of useful functions
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 12th, 2007, 16:51
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

Would this work?

At the top of the page put this
PHP: Select all

<?php
    
if( $location !=null )
    {
         
header"Location:$location" );
    }
?>
and somewhere in the included file, if neccessary put this

PHP: Select all

$location "whatever page you want to direct to"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 12th, 2007, 16:54
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: header location

PHP checks in a logical order, top to bottom.
So I don't think that will work if $location is below the if statement.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 12th, 2007, 19:13
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

What about this?

PHP: Select all

<?php
    $location 
$_POST['location'];

    if( 
$location !=null )
    {
         
header"Location:$location" );
    }
?>
But how would i set the $_POST variable without using a form?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 12th, 2007, 21:04
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: header location

That would work.
The form could be on another page, which links to this page or you could use GETs.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 12th, 2007, 23:05
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: header location

I have been ooing and ahhing about using the above solution and there are instances where i think it wouldn't work whcih is a pain. I want to try and make my code as efficient as poosible (possibly getting it drilled into us at college and uni).

Can someone please provide and example of a basic contact form or another page which uses a browser redirect so i can sede how it's done?

The ob_start() and ob_flush() works fine and that's fine for what i'm doing now but I want to try and get up to scratch with php and the proper procedures as I am thinking of doing my final year project at uni, a website that uses php and I'm going to need OOP and the correct way of doing things as I'm sure they're going to be reading the code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problems with CSS 'Header Image' - How to make fluid?? slimboyfatz32 Web Page Design 5 Jan 28th, 2008 09:09
co-location tankara Hosting & Domains 5 Jan 18th, 2008 07:54
[SOLVED] User Location Monie Classic ASP 3 Jan 8th, 2008 00:55
Search by location andrwcris Website Planning 1 Sep 11th, 2007 17:33
Need to Trace this Location Map davva Graphics and 3D 4 Aug 31st, 2006 19:08


All times are GMT. The time now is 10:59.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42