Jump to content

Why wont header() redirect when called in a function?


daydreamer

Recommended Posts

my page includes my function file like this:

 

include 'process.php';
//after form submit
process();

 

In process.php is this:

 

function processdata(){
//put into db etc
}

function process(){
if($timerexceed){header("Location: index.php");}
       processdata();
       }

 

 

For some reason my header() line is not working, it wont redirect to index, and will processdata() even if $timerexceed is true.

 

Why is this? (this is a simplified example of my situation).

 

Thanks.

yeh there is way too much code to post! but i think ive fixed it:

 

<?php
function process(){
   if($timerexceed){header("Location: index.php"); } else {
       processdata();
    }       
}
?>

 

For some reason without the else there it will not redirect, maybe the header function will only redirect once the current function has reached its closing bracket.

 

Weird.

function process(){
   if ($timerexceed) {
      header("Location: index.php");
      exit;
   } else {
       processdata();
    }       
}

$timerexceed isn't set anywhere for your if test

 

Simply setting the header to redirect won't actually execute the redirect until the script sends that information back to the browser, i.e. after it has finished any othe rprocessing within the script, unless you force it with an exit or a flush

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.