1

用户在第 1 页填写表格。表格通过 POST 提交到第 2 页。第 2 页使数据库插入,但需要插入存储在会话变量中的用户 ID,因此我需要启动会话以获取该会话变量,但这会阻止我在末尾使用标头重定向来停止重新提交。我不想将用户 ID 设置为第 1 页上的隐藏输入,因为这很容易被欺骗。我显然在做一些不符合最佳实践的事情,我应该怎么做?

    <?php
@session_start();
require_once '../Classes/Communication.php';
require_once '../Classes/Communication_db.php';
$noteDate=$_POST["noteDate"];
$noteContact = $_POST["noteContact"];
$note= $_POST["note"];
$custID = $_POST["custID"];

$comm = new Communication();
$comm->setCustomerID($custID);
$comm->setCommunicationDate(date("Y-m-d", strtotime($noteDate)));
$comm->setCustomerContactID($noteContact);
$comm->setNotes($note);
$comm->setUserID($_SESSION["userID"]);

Communication_db::saveCommunication($comm);

header("Location: ./index.php?action=newCustomer",303);

?>
4

2 回答 2

6

去掉<?php标签前的空格。那个空格被发送出去,然后你就不能打电话了header()

此外,您可以在更早的地方调用 session_start (或者图书馆正在这样做)。您只能调用一次。尝试这个:

if (!isset($_SESSION)) {
   session_start();
}
于 2012-09-28T17:38:53.593 回答
1

session_start 不会阻止您设置任何标头,但是会在标头声明之前吐出任何输出,即使这只是一个 php 警告或通知。摆脱@Ray 注意到的空间,并修复可能在标题行之前生成任何类型输出的任何其他内容。

于 2012-09-28T17:38:59.567 回答