PHP header() 不会重定向问题 [英] PHP header() will not redirect issue

查看:28
本文介绍了PHP header() 不会重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了标题问题(位置:index.php?action=messagesent"),在用户按下提交并运行 php 后它不会重定向.通常它会重定向,但由于某种原因它不起作用,它只是在点击提交后重新加载页面.但它确实在标题代码下回显了Message Sent".关于为什么不重定向的任何想法?提前致谢.

这是我的代码:

 
<div class="form-group"><label for="message">回复</label><textarea class="form-control" id="message" name="message" rows="5"></textarea>

<button type="submit" name="sendmessage" class="btn btn-purple wave-effect wave-light">发送消息</button></表单>

<?php$servername = "本地主机";$username = "我的用户名";$password = '密码';$dbname = "数据库";尝试 {$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);//将 PDO 错误模式设置为异常$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//准备sql并绑定参数$stmt = $conn->prepare("INSERT INTO 消息(fromid, toid, message, tousername,fromusername,userprofile,senddate,messagebefore)值 (:fromid, :toid, :message, :tousername,:fromusername, :userprofile, :sentdate, :messagebefore)");//插入一行$toid = $fromid;$fromid = $_SESSION['user']['id'];$messagebefore = $message;$message = $_POST['message'];$tousername = $row['fromusername'];$fromusername = $_SESSION['user']['username'];$userprofile = $row['userprofile'];$sentdate = $date = date('H:i, jS F Y');//绑定输入$stmt->bindParam(':fromid', $fromid);$stmt->bindParam(':toid', $toid);$stmt->bindParam(':message', $message);$stmt->bindParam(':tousername', $tousername);$stmt->bindParam(':fromusername', $fromusername);$stmt->bindParam(':userprofile', $userprofile);$stmt->bindParam(':sentdate', $sentdate);$stmt->bindParam(':messagebefore', $messagebefore);$stmt->execute();echo "消息已发送.";header("位置:inbox.php?action=messagesent");}捕获(PDOException $e){}$conn = 空;?>

解决方案

header(Location: ...) 仅在您尚未将输出发送到浏览器时才有效.

在脚本的前面输出表单,因此 header() 失败并显示错误消息.

如果您查看错误日志,就会看到错误消息.

错误报告添加到例如,在您打开 PHP 标记之后测试时的文件顶部

现在您将在浏览器页面上看到错误.

I'm having an issue with the header("Location: index.php?action=messagesent") it will not redirect after The user presses submit and the php is ran. Normally it will redirect but for some reason it's not working, It just reloads the page after they hit submit. But it does echo "Message Sent" right under the header code. Any ideas on why it is not redirecting? Thank You in advance.

Heres my Code:

        <form action="" method="post">
            <div class="form-group">
                <label for="message">Reply</label>
                    <textarea class="form-control" id="message" name="message" rows="5"></textarea>
            </div>
            <button type="submit" name="sendmessage" class="btn btn-purple waves-effect waves-light">Send Message </button>
        </form>
    </div>
</div>

<?php

$servername = "localhost";
$username = "myusername";
$password = 'password';
$dbname = "database";

try {

    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO messaging
                (fromid, toid, message, tousername, 
                fromusername,userprofile, sentdate, messagebefore)
        VALUES (:fromid, :toid, :message, :tousername,
                :fromusername, :userprofile, :sentdate, :messagebefore)");

    // insert a row
    $toid = $fromid;
    $fromid = $_SESSION['user']['id'];
    $messagebefore = $message;
    $message = $_POST['message'];
    $tousername = $row['fromusername'];
    $fromusername = $_SESSION['user']['username'];
    $userprofile = $row['userprofile'];
    $sentdate = $date = date('H:i, jS F Y');

    //Bind Inputs
    $stmt->bindParam(':fromid', $fromid);
    $stmt->bindParam(':toid', $toid); 
    $stmt->bindParam(':message', $message);
    $stmt->bindParam(':tousername', $tousername); 
    $stmt->bindParam(':fromusername', $fromusername);
    $stmt->bindParam(':userprofile', $userprofile);
    $stmt->bindParam(':sentdate', $sentdate);
    $stmt->bindParam(':messagebefore', $messagebefore);

    $stmt->execute();

    echo "Message Sent. ";
    header("Location: inbox.php?action=messagesent");
}
catch(PDOException $e){
}
$conn = null;
?>

解决方案

A header(Location: ...) will only work if you have not already sent output to the browser.

Earlier in your script you output the form, hence header() fails with an error message.

If you look in your error log, you will see the error message.

Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1);

Now you will see the errors on the browser page.

这篇关于PHP header() 不会重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆