There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen blue screen of death boot bsod computer connection crash css dell display driver drivers email error explorer firefox firefox 3 hard drive internet internet explorer itunes laptop lcd malware monitor network networking outlook outlook 2003 outlook express printer problem problems ram router security slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: php header() command


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
snorkytheweasel's Avatar
Computer Specs
Senior Member with 143 posts.
 
Join Date: May 2006
Experience: UNIX/VMS:Elder Geek; DOS/Windows:Fossil
18-Oct-2007, 01:25 PM #1
Solved: php header() command
I'm trying to do a simple page redirection using PHP

PHP 4.3.8
Apache 1.3.29
(upgrading either or both is not an option)

The code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<HEAD>
<TITLE>Retrieve Data From BBP Database</TITLE>
</HEAD>

<BODY>

<?
header("location: http://www.mydomain.com/reports/report1.php");
?>

</BODY>

</HTML>


Note that there is nothing written to output prior to issuing the command (not even from HTML)

The redirection fails. To confirm , I added some code so it reads as follows:


header("location: http://www.mydomain.com/reports/report1.php");
echo("Error: Using literal for new page");


The redirection does not take place, and my error message displays.

I also tried:


$newpage=" http://www.mydomain.com/reports/report1.php"
header("location: http://www.mydomain.com/reports/report1.php");
echo("Error: Using variable for new page");


The redirection does not take place, and my error message displays.

In the case shown above, I could use javaScript to do the redirection. However, later in the process (on other php pages) I need to pass variables from a form, and use those variables in conditional redirects. javaScript is a nuisance when mixed in with php.

Besides, with PHP I know that I won't have to deal with client-side annoyances like javaScript turned off.

What's happening? How do I do this correctly?

TIA, TTFN
dudeking's Avatar
Computer Specs
Senior Member with 352 posts.
 
Join Date: Feb 2007
Location: UK, Midlands
Experience: I'm Pro...
18-Oct-2007, 03:36 PM #2
I beleive header() needs to be before the <html> tag
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,250 posts.
 
Join Date: Oct 2006
18-Oct-2007, 05:56 PM #3
Dudeking is correct, to elaborate:

A header must be sent to the browser before any output. Why do you output html if you want the page to redirect? If you want to output html before the page redirects you can use a <meta> tag.

http://tools.ietf.org/html/rfc2616#section-4.2
__________________
Guys, please, this is free tech support, act accordingly.
snorkytheweasel's Avatar
Computer Specs
Senior Member with 143 posts.
 
Join Date: May 2006
Experience: UNIX/VMS:Elder Geek; DOS/Windows:Fossil
19-Oct-2007, 07:04 PM #4
If I understand correctly, a header( ) command, including header("location:"), precedes any HTML, including the <HTML> tag. That renders the <HTML> tags, and everything in between, superfluous. If that is so, then a php page with a header( ) command doesn't need anything on that page except
<?php

header("location:yyy.xxx" );

// plus
// relevant comments
// and
// irrelevant comments

?>

On the page that triggered the need for a redirect, I wimped out:
  1. Eliminate the form from which the user makes the selection
  2. Substitute a series of <A HREF> links to the "new" pages
  3. Pretty it up using CSS to style nice buttons where the text goes between <A HREF> and </A>
The simple nature of this circumstance made the <A HREF> solution workable. There is no doubt, however, that in the future a PHP-scripted solution will be in order.

That brings us back to the original problem: using php, how to I perform a conditional redirect?

While on page abc.php,
  • if user selects (x), then redirect to (or open) page def.php
  • if user selects (y), then redirect to (or open) page ghi.php
  • if user selects (z), then redirect to (or open) page jkl.php
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,250 posts.
 
Join Date: Oct 2006
20-Oct-2007, 06:52 AM #5
Quote:
* if user selects (x), then redirect to (or open) page def.php
* if user selects (y), then redirect to (or open) page ghi.php
* if user selects (z), then redirect to (or open) page jkl.php
Using <a> tags would be the most appropriate.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:33 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.