Jump to content


Photo

*SOLVED* header issues


  • Please log in to reply
8 replies to this topic

#1 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 19 May 2006 - 01:05 AM

Hi,

I have a pretty typical nav setup:

an index.php file contains a case switch to load content:

$content = isset($_GET['content']) ? $_GET['content'] : 'home';
switch($content) {

   case "fares":
     include("fares.php");
     break;

   case "deals":
     include("deals.php");
     break;
 }
?>

The index.php also includes a header.php file - the header.php file contains an unordered list that creates the nav bar - it also directs the case switch in index.php:

?php
echo "<ul>\n";
echo "<li" . ($content == 'fares' ? " id=\"current\"" : '') . "><a href=\"?content=fares\">Fares</a></li>\n";
echo "<li" . ($content == 'deals' ? " id=\"current\"" : '') . "><a href=\"?content=deals\">Deals</a></li>\n";
echo "</ul>\n";

Pretty straightforward, right? I have two issues though:

1. The Fares page (fares.php) has a form with a submit button; the submit button opens 'results.php'. With the results page open, clicking another nav button adds the dynamic URL to the end of 'results.php' so instead of, say, "?content=deals" the url is "results.php?content=fares".

How do I get the switch statement to return to the root?

2. Another link in the nav bar (and in the case switch) needs to open a forum; this forum has it's own index.php file - so, when the case switch is invoked to open the forum, it tries to place a different index.php inside of an existing header (the one that's already included).

How do I open a different index.php using the above switch case/include file scheme? (Does that make sense? Hope so...)

I've looked for help on this but since I haven't been successful I thought I'd give the ol' PHP Freaks a try. Any advice? It'd be GREATLY appreciated!

Thanks.

_Bhaal

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 19 May 2006 - 02:18 AM

For starters this isn't a header() issue. header() is a php function and has nothing to do with includes and switches or your problems.

Your first issue is simply fix by changing the links in your header.php file to read...
href=\"index.php?content=deals\">Deals</a></li>\n";
This way your links will always call the page that actually has the switch in it (index.php).

As for your second issue, Im unclear on what exactly it is you meen. Could you elaberate some?

#3 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 19 May 2006 - 04:07 AM

Thanks for the reply.

Let me elaborate on that second issue.

One of the cases in the case switch is:

   case "forum":
     include("forum/index.php");
     break;

But this produces an error:

session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\site\www\header.php:5)

I'm assuming this is because there is already a header.php as an include file.

Therefore, this case is attempting to insert 'forum/index.php' inside of the original 'index.php'.

Does that make more sense?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 May 2006 - 08:25 AM

[!--quoteo(post=375133:date=May 19 2006, 05:07 AM:name=Bhaal)--][div class=\'quotetop\']QUOTE(Bhaal @ May 19 2006, 05:07 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for the reply.

Let me elaborate on that second issue.

One of the cases in the case switch is:

   case "forum":
     include("forum/index.php");
     break;

But this produces an error:

session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\site\www\header.php:5)

I'm assuming this is because there is already a header.php as an include file.

Therefore, this case is attempting to insert 'forum/index.php' inside of the original 'index.php'.

Does that make more sense?
[/quote]

No it is not to do with having a header.php file but the problem is have something outputted (html/text/whitespace) to the browser before you include your forums index.php file, and so you get the headers already sent error message
From what the error message is saying it has detected out on line 5 in index.php.

Post the coie you have ob lines 1 - 8 so we can help you sort out this header problem.

#5 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 19 May 2006 - 10:03 AM

Hey - I appreciate the help!

OK - here's the pertinant code:

INDEX.PHP:

<?php
require_once ('header.php');
require_once '../dbsettings/databasesettings.php';
include 'caseswitch.php';
require_once ('footer.php');
?>

CASESWITCH.PHP:

<?php

$content = isset($_GET['content']) ? $_GET['content'] : 'home';
switch($content) {

   case "fares":
     include("fares.php");
     break;

   case "deals":
     include("deals.php");
     break;

   case "trains":
     include("trains.php");
     break;

   case "forums":
     include("forums/index.php");
     break;

   case "links":
     include("links.php");
     break;

   case "search":
     include("trainsearch.php");
     break;

   default:
     include("home.php");
 }
?>

HEADER.PHP:

<?php
echo "<ul>\n";
echo "<li" . ($content == 'fares' ? " id=\"current\"" : '') . "><a href=\"index.php?content=fares\">Fares</a></li>\n";
echo "<li" . ($content == 'deals' ? " id=\"current\"" : '') . "><a href=\"index.php?content=deals\">Deals</a></li>\n";
echo "<li" . ($content == 'trains' ? " id=\"current\"" : '') . "><a href=\"index.php?content=trains\">Trains</a></li>\n";
echo "<li" . ($content == 'forums' ? " id=\"current\"" : '') . "><a href=\"index.php?content=forums\">Forums</a></li>\n";
echo "<li" . ($content == 'links' ? " id=\"current\"" : '') . "><a href=\"index.php?content=links\">Travel Links</a></li>\n";
echo "<li" . ($content == 'search' ? " id=\"current\"" : '') . "><a href=\"index.php?content=search\">IntelliSearch</a></li>\n";
echo "</ul>\n";
?>




#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 May 2006 - 10:29 AM

The problem is to do with the order in which you include your pages. Header.php is included before caseswitch.php

Now in caseswitch.php you include your forum when you click the forums link. Now when you include your forum index file it has session_start as the first line of code. Which inturn stops the script from running as you have output from header.php

To solve this you need to add ob_start() as the first line of code in your index.php file and the ob_end_flush as the last line of code in your index.php filem like so:
<?php
ob_start();

require_once ('header.php');
require_once '../dbsettings/databasesettings.php';
include 'caseswitch.php';
require_once ('footer.php');

ob_end_flush();
?>


#7 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 19 May 2006 - 11:21 AM

I've never heard of ob_start() or ob_end_flush().

It 'works' - but this places the 'home page' of the forum into the site's header and footer.

That would be great, except whenever you then interact with the forum, the site's header and footer disappear - which is a little jarring.

I guess I should have been clearer: all I need to do is open the forum - as a stand-alone site.

(Or have the forum always stay within the site's header and footer - but that's probably either not possible or practical. Dunno.)

Thanks, though. I certainly learned something new!

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 May 2006 - 03:07 PM

The use header instead! Like so:
   case "forums":
     header("Location: ./forums/index.php");
     break;
By still ob_start, and ob_end_flush apply.

Read about [a href=\"http://uk.php.net/manual/en/function.ob-start.php\" target=\"_blank\"]ob_start here[/a] and [a href=\"http://uk.php.net/manual/en/function.ob-end-flush.php\" target=\"_blank\"]ob_end_flush here[/a]

Basically it allows to 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. And so this needed in your case as you have output before the use of session_start() or header() for that matter. Without ob_start you will continue to get headers already sent error message.


#9 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 19 May 2006 - 08:07 PM

Thanks wildteen!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users