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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.