Jump to content

Recommended Posts

Hi,

I have a pretty typical nav setup:

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

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

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

   case "deals":
     include("deals.php");
     break;
}
?>
[/code]

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:

[code]
?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";
[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/
Share on other sites

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...
[code]
href=\"index.php?content=deals\">Deals</a></li>\n";
[/code]
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?
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37049
Share on other sites

Thanks for the reply.

Let me elaborate on that second issue.

One of the cases in the case switch is:

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

But this produces an error:

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

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?
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37072
Share on other sites

[!--quoteo(post=375133:date=May 19 2006, 05:07 AM:name=Bhaal)--][div class=\'quotetop\']QUOTE(Bhaal @ May 19 2006, 05:07 AM) [snapback]375133[/snapback][/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:

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

But this produces an error:

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

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.
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37098
Share on other sites

Hey - I appreciate the help!

OK - here's the pertinant code:

[b]INDEX.PHP:[/b]

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

[b]CASESWITCH.PHP:[/b]

[code]
<?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");
}
?>
[/code]

[b]HEADER.PHP:[/b]

[code]
<?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";
?>
[/code]


Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37108
Share on other sites

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:
[code]<?php
ob_start();

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

ob_end_flush();
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37111
Share on other sites

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.

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

(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!
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37127
Share on other sites

The use header instead! Like so:[code]   case "forums":
     header("Location: ./forums/index.php");
     break;[/code]
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.
Link to comment
https://forums.phpfreaks.com/topic/9964-solved-header-issues/#findComment-37176
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.