本人因為大四專題的關係製作了一個購物網站結合了資料庫
有與資料庫連結的互動網站有留言板、會員管理、購物車
但目前製作的互動留言板網站發生三個bug
第一個是會員登入以後,重新整理就會直接登出會員,之後就必須再重新登入..
第二個是會員登入以後,留言,送出以後卻無法轉向頁面到留言板首頁,頁面上方跳出錯誤訊息
錯誤訊息如下:
Warning: Cannot modify header information - headers already sent by (output started at /home/u7643403/public_html/guestbook/post.php:5) in /home/u7643403/public_html/guestbook/post.php on line 134
第三個是會員登入以後,按下登出管理,跳出錯誤訊息
錯誤訊息如下
Warning: Cannot modify header information - headers already sent by (output started at /home/u7643403/public_html/guestbook/post.php:5) in /home/u7643403/public_html/guestbook/post.php on line 29
我找不到到底那兩段的程式碼有問題...
嘗試了很多次,也詢問過一些人
有說要用session變數..
我有放進去在程式碼最上方...後來會員登入可以了
但重新整理卻會直接登出....
也有說header不能有已輸出的資料..
但會員姓名,性別,e-mail都需要顯示並和留言與留言主題一起輸出...
我不太懂到底怎麼使用這個變數
希望各位高手指點迷津...
post.php 此為留言板的程式碼..
<?php session_start();
//開啟session的語法 要放在檔案最上方?>
<?php require_once('../Connections/connSQL.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "index.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo"); <-----**line29**
exit;
}
}
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "memberlogin.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO board (boardname, boardgender, boardsubject, boardtime, boardip, boardcontent, boardemail) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['boardname'], "text"),
GetSQLValueString($_POST['boardgender'], "text"),
GetSQLValueString($_POST['boardsubject'], "text"),
GetSQLValueString($_POST['boardtime'], "date"),
GetSQLValueString($_POST['boardip'], "text"),
GetSQLValueString($_POST['boardcontent'], "text"),
GetSQLValueString($_POST['boardemail'], "text"));
mysql_select_db($database_connSQL, $connSQL);
$Result1 = mysql_query($insertSQL, $connSQL) or die(mysql_error());
$insertGoTo = "/guestbook/index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo)); <----**line134**
}
$colname_Recordset1 = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_Recordset1 = $_SESSION['MM_Username'];
}
mysql_select_db($database_connSQL, $connSQL);
$query_Recordset1 = sprintf("SELECT * FROM memberdata WHERE m_username = %s", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $connSQL) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
評論
如果你的程式碼盡是一堆dreamweaver產生的「廢code」。
那你光是要debug起來就非常的麻煩。
再來有很多的狀況都可能造成「事先的輸出」。
其中一種就是檔案編碼的utf-8的bom,如果不是選無bom模式的話。
那麼在檔頭就會有什麼被先輸出只是你看不到。
良心建議第一個,不要再使用dreamweaver了。
去重新找一套ide。
也不要把你原本那混亂的dreamweaver產生的廢code又引繼進來使用。