header('Location: '.$HTTP_SERVER_VARS['HTTP_REFERER']) ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

header('Location: '.$HTTP_SERVER_VARS['HTTP_REFERER']) ???

Post by mmc01ms »

Im having trouble getting my script to do what i want it to do. I upload a file to a newsboard. when i select upload a script does some sql and then i want to return back to that page. the code i use is

Code: Select all

header('Location: '.$HTTP_SERVER_VARSї'HTTP_REFERER']);
	exit;
this isn't working is there an alternative for what i want to do?


the error i get is

Code: Select all

Cannot modify header information - headers already sent

code is:

Code: Select all

<?php
	
	//script written with the help of chapter 26 PHP & Web Development by Luke Welling 04/02/2005
	

	require('page.inc');
	
	require_once('news_fns.php');
	
	
	$link_id = db_connect();
	
	$now = time();
	
	$story = $_GET['story'];
	$query = "update stories set published = $now
        where story_id = $story";
	$result = mysql_query($query, $link_id);

	header('Location: '.$HTTP_SERVER_VARS['HTTP_REFERER']);
	exit;
		
?>
w.geoghegan
Forum Newbie
Posts: 7
Joined: Mon Jan 03, 2005 7:27 pm
Location: Bucks, UK.

Post by w.geoghegan »

You can only use the header() function if no output has been sent. It looks like page.inc is sending output. If you can, try moving require('page.inc') below the code that handles the redirect.

Hope this helps.

Cheers

William Geoghegan
GEOTEK Computer Services
- http://www.geotekcs.co.uk -
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah, there may be some blank lines you don't realize are being sent by the time the header call is being made. Also beware that some browsers/devices don't send the referer information. When I need to know where in the site they came from, I pass the location information along through multiple means, but typically try to keep it outside the user's submission.. so the session variable space is used most often.
keiths
Forum Newbie
Posts: 7
Joined: Fri Dec 24, 2004 8:46 pm

Post by keiths »

Going along with this question, I have a question.

In the php.ini file you can turn on output_buffering and you can use headers after there has been output. Can some one tell me why you wouldn't want to do this? Does it just slow down PHP a little?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

keiths wrote: Does it just slow down PHP a little?

yes. depending on various factors, it could speed things up, or slow them down. usually it slows it down a bit though.


if your pages are relatively small, the performance hit is very small. but if you have some pages that are very large, and take a while to process(like this forum would be a prime example) output buffering would slow things down.
Post Reply