Page 1 of 1

whats the general code to redirect to another page?

Posted: Mon Jun 26, 2006 1:30 am
by saumya
Hi,
I ahve a strage problem here. I have a form and its action is specified to a new php page.When I submit the form the ddata is getting inserted into the database.Well all this is doen once my form is submitted to the submit.php page.This submit.php page gets my form data insterted into database.Once inserted, I ahve use header() method to get to my original page.While all this seems to work fine in my own computer, I get error message when I run from remote server. The error message says, "header information all ready sent".So the thing hangs on my "submit.php" page.If I go back by pressing back key of my browser, then refresh the page, I get the updated information from my database.The data is already put into the databse.But the page does not redirect.I am going wrong way? I mean whats the general method to get back to the origianl page after inserting tha data.

Posted: Mon Jun 26, 2006 1:37 am
by Benjamin
Have a look at this...

viewtopic.php?t=1157

Posted: Mon Jun 26, 2006 1:38 am
by bdlang
header() is the correct method to redirect. If you're getting 'header information already sent...' errors then there is something wrong with the script, either an error message is shown to do with the database code, or there is a blank space or other character sent on the page prior to the call to header(). Pretty common error, check the PHP manual and search Google or this forum.

[edit]

^ what he said.

Posted: Mon Jun 26, 2006 1:38 am
by Christopher
There is some output being set before you make the header() call. It could be a space or anything. Check everything before the header() call for echo/print statements and any characters outside the <?php ?> tags.

Posted: Mon Jun 26, 2006 2:51 am
by saumya
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I guess the same.But I got confused to see, that the script works on my own pc, not from server. Any way here is the script I am using

Code: Select all

<?php
require_once('DbConnector.php');
$MYconnector = new DbConnector();
	//Gets the user submitted values
	$volume = $_POST['volume'];
	$article = $_POST['article'];
	$publishDate = $_POST['publishdate'];
	$author = $_POST['author'];
	$content = $_POST['data'];
	//checks whether user has submitted anything
	if($volume!="" && $article!="" && $publishDate!="" && $author!="" && $content!=""){
		$contentFill_sql= "INSERT INTO `articles` (`volume`,`article`,`publishdate`,`content`,`author`) VALUES('$volume','$article','$publishDate','$content','$author');";
		mysql_query($contentFill_sql) or die('error : '.mysql_error());
		echo('successfully put');
		header( "Location: ../index.php" );
	}else{
		echo('please fill the form');
		header( "Location: ../index.php" );
	}
?>
thats everything I have in my putData.php file.

any help?


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jun 26, 2006 2:53 am
by Benjamin
Looks like there is a space before the <?php flag.

Posted: Mon Jun 26, 2006 3:10 am
by saumya
but how this works in my local machine ?!!!

After reading the topics you people have suggested me, I think there is a echo statement in my code before, header.But not sure of. :(

Posted: Mon Jun 26, 2006 3:19 am
by Benjamin
In php.ini, if you have output buffering turned on, you can have spaces and echo data before you send a header. On your local server, you may have output buffering turned on, while your remote server has it turned off. Either way, there is a space, linefeed, or data being sent before the header, otherwise you would not get the error you are receiving.

Posted: Mon Jun 26, 2006 4:14 am
by saumya
Thats some cool hint.
Thank you
for the hint, that my local machine settings are different from server.

But I could not figureout where is the header info before calling the header function.I am going to look at it again and let you know.
saumya

Posted: Mon Jun 26, 2006 4:23 am
by Charles256
erm..any output before your "header" call makes that error happen, not specificall another header() call. :-D

Posted: Mon Jun 26, 2006 4:24 am
by saumya
Now,I turned off "output buffering" on my machine and the script I am running now is

Code: Select all

<?php
require_once('DbConnector.php');
$MYconnector = new DbConnector();
	//Gets the user submitted values
	$volume = $_POST['volume'];
	$article = $_POST['article'];
	$publishDate = $_POST['publishdate'];
	$author = $_POST['author'];
	$content = $_POST['data'];
	//checks whether user has submitted anything
	if($volume!="" && $article!="" && $publishDate!="" && $author!="" && $content!=""){
		$contentFill_sql= "INSERT INTO `articles` (`volume`,`article`,`publishdate`,`content`,`author`) VALUES('$volume','$article','$publishDate','$content','$author');";
		mysql_query($contentFill_sql);
		header( "Location: ../index.php" );
		//mysql_query($contentFill_sql) or die('error : '.mysql_error());
		//echo('successfully put');
	}else{
		echo('please fill the form');
		header( "Location: ../index.php" );
	}
?>
Now I could not figure out where is the blank space.I have already commented evrything and calling my header function. In my development environment I found nothing at the browser.It comes to this page and hangs.It never goes back to the redirect ( :(

Posted: Mon Jun 26, 2006 4:29 am
by Charles256
go to your db connector.php page and make sure all the white space at the top is removed there too..i.e.
db connector page start---
(blank line here!)
<?php
code here
?>
main page here.
header call here (uh oh, we got a blank link on the included page!)

ya follow? :-D

Posted: Mon Jun 26, 2006 5:57 am
by saumya
are the comments have to be eleiminated !!?Because now I have removed all the white spaces too, but without any effect in my script.This still shows me a blnak screen on submission.No redirection :( :(

Code: Select all

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';
class DbConnector extends SystemComponent {
var $theQuery;
var $link;
//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){
//echo('evaluating DB Connector <br/>');
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
//echo($host.' : '.$db.' : '.$user.' : '.$pass.'<br/>');
// Connect to the database
$this->link = mysql_connect($host, $user, $pass)or die('Could not connect to Database Server'.mysql_error().'<br/>');
mysql_select_db($db) or die('DB not Found'.mysql_error().'<br/>');
register_shutdown_function(array(&$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query) {
//echo('Querying DB<br/>');
$this->theQuery = $query;
return mysql_query($query, $this->link);
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {
return mysql_fetch_array($result);
}
//*** Function: close, Purpose: Close the connection ***
function close() {
//echo('closing connection');
mysql_close($this->link);
}
}
?>
Then the other file

Code: Select all

<?php
class SystemComponent {
var $settings;
function getSettings() {
		//echo('inside system component getSettings<br/>');
		// System variables
		//$settings['siteDir'] = '/path/to/your/intranet/';
		// Database variables
		$settings['dbhost'] = 'localhost';
		$settings['dbusername'] = 'saumya';
		$settings['dbpassword'] = 'saumya123';
		$settings['dbname'] = 'universal';
		return $settings;
	}
}
?>

Posted: Mon Jun 26, 2006 11:21 am
by RobertGonzalez
Every page in your app needs to have ABSOLUTELY NO spaces, echos, or output before the call to header(). If anything, and I mean anything, gets sent to the browser before the call to header(), you are going to get that error message.

I would suggest reading the PHP manual on header() to familiarize yourself with the use of the function. Also, I noticed in one of your posts you have this code...

Code: Select all

<?php
{
    $contentFill_sql= "INSERT INTO `articles` (`volume`,`article`,`publishdate`,`content`,`author`) VALUES ('$volume','$article','$publishDate','$content','$author');";
    mysql_query($contentFill_sql);
    header( "Location: ../index.php" );
    //mysql_query($contentFill_sql) or die('error : '.mysql_error());
    //echo('successfully put');
}else{
    echo('please fill the form');
    header( "Location: ../index.php" );
} 
?>
That little echo('please fill the form') will kill your header redirect everytime (unless you use output buffering). But I would seriously try reading the manual, and searching these forums. This is the fifth or sixth 'Headers already sent' post I have responded to within the last week.

PS A blank page is usually a sign of a critical error message thrown by PHP coupled with display_errors being off. If you want to see what PHP is telling you about the error, turn display_errors on.

Code: Select all

<?php
ini_set('display_errors', TRUE);
?>

Posted: Mon Jun 26, 2006 11:50 pm
by saumya
Thank you so much.
Let me try my best now.