PHP执行KSH脚本后无法跳转至指定页面求助
Hey Amar, let's break down why your redirect isn't firing after running run_gen_execute_map.ksh, and fix it step by step.
First, Spot the Obvious Syntax Error
Looking at your code, there's a missing closing double quote in your header call—this would cause a PHP parse error right off the bat:
header("location:cycle_task_actions.php); // Missing " at the end
Fix that first:
header("Location: cycle_task_actions.php");
Pro tip: While HTTP doesn't strictly require it, capitalizing Location and adding a space after the colon follows standard conventions.
Why the Redirect Still Fails (Even After Fixing Syntax)
The core issue is that exec() can capture output from your KSH script (either stdout or stderr) and send it to the browser before your header() call. Once any output is sent, PHP can't modify HTTP headers anymore—this is a super common gotcha with header redirects.
Even adding & to run the script in the background doesn't prevent this, because PHP might still hold onto output from the command before it fully detaches.
Step-by-Step Fixes
Redirect All KSH Script Output to Null
Modify yourexec()call to discard all output from the KSH script, so nothing leaks into PHP's response:exec("run_gen_execute_map.ksh > /dev/null 2>&1 &");> /dev/nullsends standard output (stdout) to the null device (discards it)2>&1redirects standard error (stderr) to the same place as stdout- The final
&ensures the script runs in the background, so PHP doesn't wait for it to finish before moving on
Eliminate Premature Output
Check your PHP file for any accidental output before theheader()call:- Leading/trailing spaces outside of
<?php ?>tags - A Byte Order Mark (BOM) at the start of the file (common if you edited the file on Windows)
- Any
echo,print, orvar_dumpstatements before the redirect
If you can't track down hidden output, use output buffering to clear it:
ob_start(); // Start buffering output at the very top of your file exec("run_gen_execute_map.ksh > /dev/null 2>&1 &"); ob_end_clean(); // Discard all buffered output before sending headers header("Location: cycle_task_actions.php"); exit;- Leading/trailing spaces outside of
Add
exitAfter the Redirect
Always stop execution immediately after sending the redirect header to prevent any additional code from running (which might generate output or interfere):header("Location: cycle_task_actions.php"); exit; // or die();
Final Working Code
Putting it all together, your code should look like this:
<?php exec("run_gen_execute_map.ksh > /dev/null 2>&1 &"); header("Location: cycle_task_actions.php"); exit; ?>
内容的提问来源于stack exchange,提问作者Amar Shah




