[RESOLVED] White Spaces Error
Can anyone help me with this error?
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\guestbook.php:1) in C:\xampp\htdocs\guestbook.php on line 1
I've eliminated all tabs, spaces before and after my line 1, but that error message still displays.
Code:
<TITLE>Comments</TITLE><?php session_start();
if(!isset($_SESSION['nname']))
{
$_SESSION['nname'] = 'Anonymous';
}
if (!isset($_GET['command']))
{
echo "Welcome " . $_SESSION['nname'] . "<br>";
?>
<a href ="?command=add">Add Post</a>
<a href ="?command=display">Display Post</a>
<a href ="?command=login">Set Nickname</a>
<?php
}
else {
if ($_GET['command'] == 'add'){
add_comment();
}
elseif ($_GET['command'] == 'adding'){
adding();
}
elseif ($_GET['command'] == 'display'){
display_comments();
}
elseif ($_GET['command'] == 'login'){
set_name();
}
elseif($_GET['command']=='logging'){
logging();
}
}
function add_comment()
{
?>
<form action = "?command=adding" method = POST>
<textarea rows= 5 name = "comment">Insert your comments here...</textarea>
<input type = "submit" value = "Post">
</form>
<?php
}
function adding() {
$con1 = mysql_connect("localhost", "root", "password");
mysql_select_db("guestbook",$con1);
mysql_query("INSERT INTO comments (userid, comment, when_posted) VALUES (".$_SESSION['nname'].",".$_POST['comment'].", Now()" );
?>
<br>
<a href = "?command=display">View Comments</a>
<?php
mysql_close($con1);
}
function set_name() {
?>
<form action = "?command=logging" method = POST>
Set your nickname: <input type = "text" name = "nname">
<br><input type = "submit" value = "Set">
</form>
<?php
}
function display_comments() {
$con = mysql_connect("localhost", "root", "password");
mysql_select_db("guestbook",$con);
$results = mysql_query("SELECT * FROM comments ORDER BY id DESC");
echo "<TABLE BORDER = 1>";
while($row = mysql_fetch_array($results))
{
echo "<TR>";
echo "<TD BGCOLOR = PINK>" . $row['userid'] . "</TD>";
echo "<TD BGCOLOR = YELLOW>" . $row['comment'] . "</TD>";
echo "<TD BGCOLOR = SKYBLUE>" . $row['when_posted'] . "</TD>";
echo "</TR>";
}
echo "</TABLE>";
mysql_close($con);
}
function logging() {
$_SESSION['nname'] = $_POST['nname'];
echo "Hello " . $_SESSION['nname'];
?>
<a href = "guestbook.php">Back</a>
<?php
}
?>
Re: [RESOLVED] White Spaces Error
Some advice:
Your code structure is very hard to follow. It seems to be a muddle of logic and presentation. (Why are the database calls in the middle of the HTML? Why are you using echo to emit the output?)
As a general rule you should perform all logic (such as authentication and fetching data) before worrying about presentation. In a short script this can be accomplished simply, as in the following template:
PHP Code:
<?php
# Open session
session_start();
# Open database connection
$dbh = mysql_connect(...);
#...
# Fetch data into a variable (say $comments)
#...
?>
<html>
<!-- Define HTML markup -->
<!-- Output the data fetched: -->
<?php foreach ($comments as $comment): ?>
<h2><?php echo $comment['title'] ?></h2>
<p><?php echo $comment['comment'] ?></p>
<?php endforeach; ?>
<!-- Any other markup -->
</html>
This type of structure is much easier to follow, easier to maintain, and scales well if your website or web application develops beyond its original level of complexity.