cPanel PHP 5.1到5.4更改,标题(位置:index.php)

时间:2017-12-23 21:59:46

标签: php mysql

我将网站移动到cPanel托管,并从php 5.1移动到5.4。唯一没有使用移动的是在MySql数据插入后返回index.php页面。数据插入正常,但我得到一个空白页而不是index.php页刷新。

我研究过论坛并使用我观察到的想法:删除了PHP代码中的空格;在脚本中移动了标题(Location:index.php);在Location:和index.php之间占用了一个空格,但是在插入后无法显示index.php页面。该URL位于浏览器行中,如果单击它,则刷新页面。这在过去是自动发生的。

以下是代码:

<?php
function renderForm($mscdate, $mscname, $mscaffirm, $mscstatus, $error)
{
?>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script type="text/javascript">
    $(function() {
        $('#datepicker').datepicker({dateFormat: 'MM d, yy', altField: '#dbDate', altFormat: 'yy-mm-dd'});
    });
    </script>
<?php 
}

include('connect to database');

if (isset($_POST['submit']))
{ 
    $mscdate = mysqli_real_escape_string(htmlspecialchars($_POST['mscdate']));
    $mscname = mysqli_real_escape_string(htmlspecialchars($_POST['mscname']));
    $mscaffirm = mysqli_real_escape_string(htmlspecialchars($_POST['mscaffirm']));
    $mscstatus = mysqli_real_escape_string(htmlspecialchars($_POST['mscstatus']));
    if ($mscdate == '' || $mscname == '' || $mscaffirm == '')
    {
    $error = 'ERROR: Please fill in all required fields!';
    renderForm($mscdate, $mscname, $mscaffirm, $mscstatus, $error);
    } else {
        mysqli_query("INSERT mscrides SET mscdate='$mscdate', mscname='$mscname', mscaffirm='$mscaffirm', mscstatus='$mscstatus'")
        or die(mysqli_error()); 
        header("Location:index.php"); 
    }
}else { // if the form hasn't been submitted, display the form
renderForm('','','','','');
}
?>

2 个答案:

答案 0 :(得分:0)

尝试在代码顶部添加ob_start()

答案 1 :(得分:0)

您正在向浏览器发送Location HTTP重定向,而不会告诉PHP停止执行,这通常通过exit()完成。在PHP应用程序中,这通常会导致竞争条件,这取决于多种因素,主要是互联网/网络速度,以及重定向是否有效。

比赛具体是在Location标题之后首先出现的比赛:

  • 浏览器接收重定向并断开连接(重定向将起作用)
  • 服务器在GET操作中发送HTML正文(重定向可能会失败,也可能取决于浏览器)

即使你认为你的脚本会自然结束,如果你有一个附加处理程序或关闭处理程序处于活动状态,有一个明确的exit()是一个很好的做法。

相关问题