session_start() error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

session_start() error

Post by vfm »

Hi,

I'm very new to PHP and i'm getting the following error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/tunethe1/public_html/viewforme.com/step1.php:8) in /home/tunethe1/public_html/viewforme.com/step1.php on line 28

I read a few things on this but none of it made much sense to me and I'm wondering whether someone could help clear it up.

This is how my code looks:

Code: Select all

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>ViewForMe</title>

<script type="text/JavaScript">

// If the length of the element's string is 0 then display helper message
function checkform(){
	var urls = document.getElementById('urls');
	if(urls.value.length == 0){
		alert("Please submit a URL to continue.");
		urls.focus(); // set the focus to this input
		return false;
	}
	<?php $step1="1" ?>
	return true;
}

</script>

</head>

<body>

<?php session_start(); ?>
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: session_start() error

Post by phu »

Move the session_start() call to the top, before you start outputting HTML.

Better yet, move the session_start() call to whatever file calls the file containing that HTML. If your files just like this -- HTML with PHP interspersed like that -- you should re-evaluate the way you are writing scripts, as this is not a maintainable way to build or maintain a website.
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: session_start() error

Post by vfm »

Thanks for that!

Any good tips on where to read up about the stuff you were talking about?
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: session_start() error

Post by phu »

Well... generally speaking, the whole idea of mixing PHP with HTML gets you into trouble. That's what those of us who start on PHP and spend a few years with it learn. ;)

The best idea is to either learn how the language interacts with the web server and the browser yourself or pick up a framework (which will help you to the same end)... CodeIgniter is decent, as is Symfony.

However, if you have any option at all... pick up Python and learn Django. I spent 9 years writing PHP; in the last two years I picked up Django, started a web consultancy based on it (Comfy Chair Consulting), and the only reason I've come back is to write something similar to Django to leverage the massive install base of PHP itself. Python is far more powerful, intuitive and mature as a language, and the tools available to you are immeasurably more usable and worthwhile.

As far as PHP... learn about the MVC (model-view-controller) pattern, and even if you don't take it completely to heart -- skepticism is always healthy -- you should be able to take a lot from it in terms of separating your actual PHP logic from your HTML (presentation layer).
Post Reply