Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirecting php

Status
Not open for further replies.

kitus

Programmer
Sep 22, 2005
10
0
0
DE
Hi guys,

could anybody please give my a hand??

Currently I'm programming a web-based environment that is meant to help in programming switches (hehe, this is not important at all!!) I'm very much a beginner but up to now everything more or less has worked out, so i cannot complain.

At the moment I've already programmed one welcoming html page with a kind of formular that when correctly filled out calls a php script stored in another file.. this secoond script, which main structure is switch-case based on gets more or less the whole work done (besides a mysql connection) but in one situation i would like to redirect the user to the previous html page and then is when everything gets messed up...

I've already tried the header command that I've found here, namely:
<?
$url=" header ("Location: ".$url);
?>

but it doesn't help... to me, it looks like that when calling a script from an html this redirection stuff gets stuck with the script page. This is the warning text shows:

Warning: Cannot modify header information - headers already sent by (output started at /home/Kitus/workspace/prova/database_part.php:3) in /home/Kitus/workspace/prova/database_part.php on line 100

line 100 is where this redirection is called.

Sorry for this long post but with my English i don't know how to make myself clear in a shorter manner...

Thank you very much,

Kitus
 
Call to header function (as the error points out) must be done before any output was sent to browser. If you need to have some sort of output (as in conformation or something), you will have to use client side redirection, such as meta tags or javascript. However, if the script that redirects only processes things and puts them in a database, no output is necessary. If it is so, you can check your code if any output exists before this call and remove it. When looking for output, you should look for:
1. Any print or echo sentences.
2. Any part of the code that could generate error or warning.
3. Any html code outside php tags and print/echo statements. This includes doctypes and <html> tags.
4. Any whitespace that exists outside the <?php ?> brackets.

Bare in mind that any of those outputs can appear in the included files as well and cause your error.
 
Here is a trick that I use when writing ASP pages, and it should work with PHP as well (I'm also new to PHP...).

In situations like this where you get input from page 1, then based on that input maybe you want to go to page 2 or page 3, I use an intermediate page. This intermediate page has just processing logic - no html code at all, and it decides what to do. Let me explain:

Page 1 - contains a form with the necessary fields on it. <head><body> and all that stuff. The form on this page is set up to go to page1a.php

Page 1a.php - this page contains only php code - no html statements at all (no <html>, no <head>, none of this) - it starts with <? and ends with ?> This page gets the fields from page 1 and figures out what to do. As an example, maybe it needs to verify a password, so it might look something like this (not real php code, but you'll get the idea)
<?
$pass_from_screen = $_REQUEST[passwd];
..code to access db goes here..
if ($pass_from_form != $pass_from_db)
{$url = "page2.php" } //bad password, handle it
else
{$url = "page3.php" } //good password, continue
header ("Location: ".$url); // let's go somewhere
?>

Page 2 - handles errors - regular html maybe mixed with php code

Page 3 - handles correct passwords - again regular html with php code

So page1a.php acts like a switch statement (you could use one instead of the if I used), and it should work because it doesn't do any prints or have html code in it. This is totally transparent to the user - they will see page1 followed by page2 or page3, depending on what they entered on page1

The trick is that once you do a print or execute any html statement, the header stuff will be written to the browser and then the redirection (the header statement) will fail. Using an intermediate "page" like this gets around this since it doesn't output anything.

Good luck
 
This seems to work for me and I use Header all over the place:

start every page with:
ob_start();


and finish every page with:
ob_end_flush();

I've been using this method for years now and it's never failed me. Essintially, you are starting clearing the output buffer so you don't get any header errors. At least that's my understanding of it.

Let me know if this helps,
 
Hi guys,

i just got to redirect it closing the php "zone" and using plain html redirection code.. Thank you very very much guys... nice trick atomix.

Could you guys do me a favour? I posted a topic some days ago and no one has already answered... every suggestion will definitely be appreciated. I'm in a dead end. :(

thanks in advance!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top