The header function is not working on online server?

ⅰ亾dé卋堺 提交于 2019-12-08 15:05:14

问题


hi i just dont understand why my code is not working. i am using yahoo server for my site.

This is my logout code.(which is successfully run on localhost) but when i upload this code online then its not work. plz help

<?php  
//logout code

include("../Config.php");
if (!isset ($_SESSION['username']))
    {
header( 'HTTP/1.1 301 Moved Permanently' );
header('Location: ../index.php');

if (!headers_sent())
  { 
  header('Location: http://www.mysite.com/index.php');
  exit;
  }
    }
    else
    {

$_SESSION = array();
session_destroy();
session_unset();
header( 'HTTP/1.1 301 Moved Permanently' ); 
header('Location: ../index.php');

if (!headers_sent())
  { 
  header('Location: http://www.mysite.com/index.php');
  exit;
  }
}

?>

the config.php file includes session code (like start session)


回答1:


You need to use the full URI in the header, and I recommend to use exit() right after the location header. There is no need for the 301 header for a simple log out. And don't use the closing tag in php. If it is working on your system, it looks, there is some output (maybe just an empty line) in at least one of your php files (before the starting php tag, or after the closing php tag), and it seems that output buffering is enabled in your PHP, which work around this error, but disabled on the production server.

Try this:

<?php
// for debugging purposes only, don't use on production server (just for debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);

//logout code

include("../Config.php");

if (isset($_SESSION['username']))
    session_destroy();

header('Location: http://www.mysite.com/index.php');
exit;



回答2:


  echo '<script type="text/javascript">
    function delayer(){
     window.location = "../index.php"
 }
 setTimeout("delayer()", 1000);

   </script>';

You could put this instead of header




回答3:


This will work

<script type="text/javascript">
  window.location="http://www.newlocation.com"; 
</script>


来源:https://stackoverflow.com/questions/4183075/the-header-function-is-not-working-on-online-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!