header location not working in my php code

前端 未结 15 885
面向向阳花
面向向阳花 2020-11-28 13:17

i have this code,why my header location not working? its a form of updating and editing and deleting some pages in my control panel...and i have an index.php file in the sam

相关标签:
15条回答
  • 2020-11-28 13:51
    ob_start(); 
    

    should be added in the line 1 itself. like in below example

    <?php
    ob_start(); // needs to be added here
    ?>
    <!DOCTYPE html>
    <html lang="en">
    // your code goes here
    </html>
    <?php
    if(isset($_POST['submit']))
    { 
    //code to save data in db goes here
    }
    header('location:index.php?msg=sav'); 
    ?>
    

    adding it below html also doesnt work. like below

    <!DOCTYPE html>
    <html lang="en">
    // your code goes here
    </html>
    <?php
    ob_start(); // it doesnt work even if you add here
    if(isset($_POST['submit']))
    { 
    //code to save data in db goes here
    }
    header('location:index.php?msg=sav'); 
    ?>
    
    0 讨论(0)
  • 2020-11-28 13:56

    It should be Location not location:

    header('Location: index.php');
    
    0 讨论(0)
  • 2020-11-28 13:57

    Try adding ob_start(); at the top of the code i.e. before the include statement.

    0 讨论(0)
  • 2020-11-28 13:57

    The function ob_start() will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So browser will not receive any output and the header will work.Also we should make sure that header() is used on the top of the code.

    0 讨论(0)
  • 2020-11-28 13:59

    I use following code and it works fine for me.

    if(!isset($_SESSION['user'])) {
           ob_start();
           header("Location: https://sitename.com/login.php");
           exit();
    } else { 
    
    // my further code 
    
    }
    
    0 讨论(0)
  • 2020-11-28 14:02

    I had same application on my localhost and on a shared server. On my localhost the redirects worked fine while on this shared server didn't. I checked the phpinfo and I saw what caused this:

    While on my localhost I had this:

    So I asked the system admin to increase that value and after he did that, everything worked fine.

    0 讨论(0)
提交回复
热议问题