Help - Search - Members - Calendar
Full Version: Header Problem
Weborum Webmaster Forum > Web Page Design > PHP
sjthomas
I'm running a script that needs to send a header: location at the end. The problem I'm having is that the header function is messing up everything that goes before it! The Header line itself works fine and redirects the user to the right page but above the header line are a couple of other statements to do stuff in the DB. When the header function is there the DB simply doesn't get updated. If I simply remove the header line then the database updates but obviously I don't get the re-direct! I really can't understand why this is happening! I'm not getting any errors (I have error_reporting set to all and I'm not even getting any notices!) and it seems like the entire operation of the script is being affected by this very last line! I even changed the last line to an echo to see what all the variables were set to and whatt he last query was and that worked. Yet change that to the header line, which seemingly works perfectly in itself, and nothing!
mad.gif its doing my head in!
aviansuicide
Why don't you try output buffering just in case...so it holds the header till everything else is done:

PHP

<?
ob_start
();

/* db calls */

header("Location: blah");
exit;
ob_end_flush():
?>

sjthomas
Already tried it. This one has me beat I think, I really don't understand why this is happening!
aviansuicide
Code?
sjthomas
QUOTE(aviansuicide @ Mar 26 2005, 11:58 PM)
Code?
[right][snapback]25303[/snapback][/right]



PHP
<?php
$viewPointID
= $HTTP_GET_VARS['viewPointID'];
$nodeID = $HTTP_GET_VARS['nodeID'];
$modelID = $HTTP_GET_VARS['modelID'];
require_once(
'includes/DbConnector.php');
// Create an instance of DbConnector
$connector = new DbConnector();
require_once(
'includes/Validator.php');
// Create an instance of Validator
$validator = new Validator();
if (
$viewPointID != '') {
    
$validator->validateNumber($viewPointID,'Selected View Point');
    
$validator->validateNumber($nodeID,'Selected Node');
    if (
$validator->foundErrors() ){
        echo
'There was a problem with the '.$validator->listErrors('<br />'); // Show the errors, with a line between each
        
echo("<br /><a href='mainframe.php?modelID=".$modelID."'>Return to the model</a>");
    }else{
        
// echo("Passed Validation<br />");
        
$fromNameVPA = $connector->query("SELECT * FROM viewpoint_node WHERE d_nodeID = '".$nodeID."' AND s_viewPointID = '".$viewPointID."'");
        
$fromNameVPArray = $connector->fetchArray($fromNameVPA);
        if (
$fromNameVPArray['s_viewPointID'] != '') {
            
// Node already exists in viewpoint so delete it
            
$connector->query("DELETE FROM viewpoint_node WHERE d_nodeID = '".$nodeID."' AND s_viewPointID = '".$viewPointID."'");
            
// echo("Link Deleted<br />");
        
} else {
            
// Node does not exist in view point so add it
            
$insertQuery = "INSERT INTO viewpoint_node (s_viewPointID, d_nodeID) VALUES ('".$viewPointID."', '".$nodeID."')";
            
$resultInsert = $connector->query($insertQuery);
            
// echo("Link Added<br />");
        
}
        
header("Location: mainframe.php?modelID=".$modelID);
    }
}
?>



Its from the same site as the other thread lol. The DB connector and validator do exactly what they say on the tin! The code here doesn't work (well it doesn't throw any errors and redirects) but take out the header line and it works fine!
aviansuicide
*copys into editplus* I'll see what I can make of it.
aviansuicide
Why not just do it like this?

PHP

<?
$viewPointID
= $_GET['viewPointID'];
$nodeID = $_GET['nodeID'];
$modelID = $_GET['modelID'];
require_once(
'includes/DbConnector.php');
// Create an instance of DbConnector
$connector = new DbConnector();
require_once(
'includes/Validator.php');
// Create an instance of Validator
$validator = new Validator();
if (
$viewPointID != '') {
    
$validator->validateNumber($viewPointID,'Selected View Point');
    
$validator->validateNumber($nodeID,'Selected Node');
    if (
$validator->foundErrors() ){
        echo
'There was a problem with the '.$validator->listErrors('<br />'); // Show the errors, with a line between each
        
echo("<br /><a href='mainframe.php?modelID=".$modelID."'>Return to the model</a>");
        exit;
    }
        
// echo("Passed Validation<br />");
        
$fromNameVPA = $connector->query("SELECT * FROM viewpoint_node WHERE d_nodeID = '".$nodeID."' AND s_viewPointID = '".$viewPointID."'");
        
$fromNameVPArray = $connector->fetchArray($fromNameVPA);
        if (
$fromNameVPArray['s_viewPointID'] != '') {
            
// Node already exists in viewpoint so delete it
            
$connector->query("DELETE FROM viewpoint_node WHERE d_nodeID = '".$nodeID."' AND s_viewPointID = '".$viewPointID."'");
            
// echo("Link Deleted<br />");
        
} else {
            
// Node does not exist in view point so add it
            
$insertQuery = "INSERT INTO viewpoint_node (s_viewPointID, d_nodeID) VALUES ('".$viewPointID."', '".$nodeID."')";
            
$resultInsert = $connector->query($insertQuery);
            
// echo("Link Added<br />");
        
}
        
header("Location: mainframe.php?modelID=".$modelID);
}
?>

sjthomas
Tried it lol Still nothing. Any ideas? Personally I can't see anythign wrong with the code at all!
aviansuicide
QUOTE(sjthomas @ Mar 26 2005, 08:15 PM)
Tried it lol  Still nothing.  Any ideas?  Personally I can't see anythign wrong with the code at all!
[right][snapback]25308[/snapback][/right]


Other than the depreciated get vars..which I fixed...neither can I.But it's hard to say without testing the code myself.Lets just..for argument sake...try replacing the "header.." with a meta refresh redirect.
sjthomas
Bizzarely that actually works! I have no idea why but it does. Nice suggestion! Any ideas why the ehader wasn't working?
aviansuicide
QUOTE(sjthomas @ Mar 26 2005, 08:23 PM)
Bizzarely that actually works!  I have no idea why but it does.  Nice suggestion!  Any ideas why the ehader wasn't working?
[right][snapback]25311[/snapback][/right]



Hehe...gotta love odd bugs ^_~.Well...atchually..no..no idea...unless it's executing too early *shrugs*...can you use a meta refresh, or do you need header location?
sjthomas
I guess I can use a meta refresh but it just doesn't seem right for some reason lol!

Its certainly a weird one.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.