Real problem with session code

I am having awful trouble with sessions. I have copied the following code into a page and uploaded it. It comes back with the following warning messages

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (session1.php:1) session1.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

Here is the code. Exactly what am I doing wrong as I copied and pasted it and hacve tried other examples and its uts not working


<?php
session_start(); 
$_SESSION['views'] = 1; // store 

session data
echo "Pageviews = ". $_SESSION

['views']; //retrieve data
?>

<?php
session_start();
$_SESSION[‘views’] = 1; // store

session data
echo "Pageviews = ". $_SESSION

[‘views’]; //retrieve data
?>

You forgot to comment out that line?

and then… have some

random enter strokes.

Random enter strokes = broken things.

sorry

This is the code


<?php
session_start();  
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else $_SESSION['views'] = 1;
echo "views = ". $_SESSION['views']; 
?>

Its cut and pasted from an example from the internet. It gets the same warning message as I mentioned. It does not increment as it should do


<?php
session_start();
if( ! isset($_SESSION['views'])){
  $_SESSION['views'] = 1;
}else{
  $_SESSION['views']++;
}

echo 'You viewed this ', $_SESSION['views'], ' times.';

Hi Anthony

I cut and pasted your code with the same warnings and it doesn’t work.

The exact same warning? Place the code I provided in a script by itself, you shouldn’t have an issue.

Make sure there’s no whitespace, no blank lines, nothing before the <?php, as well.

Er… actually… Anthony’s code needs a slight modification…

echo 'You viewed this ‘. $_SESSION[‘views’]. ’ times.’;

No it doesn’t. :wink:

Are you sending any output in any scripts before this one?

Hi

I just opened the page in wamp server. So far I have been putting everything out onto the internet. Its works perfectly on wamp but on myt webspace it does not work and sends back these warnings.

I have the following session script which works on Wamp server.However it fails to work on the internet… why

[COLOR=#000000] [COLOR=#0000BB]


<?php
session_start();
if( ! isset($_SESSION['views'])){
  $_SESSION['views'] = 1;
}else{
  $_SESSION['views']++;
}

echo 'You viewed this ', $_SESSION['views'], ' times.';
?>

[/COLOR] [/COLOR]
It comes up with warnings that headers have been sent.

What is the error?

[Threads Merged]

These are the warnings

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (session1.php:1) session1.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

AND the program doesn’t work. The variable stays at 1 and never increments.

IT WORKS with wamp server but on the internet it fails. Its probably something to do with my web hosts.

You’re not using the code I posted, if you we’re, the error would be on line 1 not 5. If you post the actual code you’re using, we can try to help you.

sorry - I copied and pasted the errors from the post earlier. Here are the errors in full

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/castcou7/public_html/dreamsymbolism/forum/session1.php:1) in /home/castcou7/public_html/dreamsymbolism/forum/session1.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/castcou7/public_html/dreamsymbolism/forum/session1.php:1) in /home/castcou7/public_html/dreamsymbolism/forum/session1.php on line 2
You viewed this 1 times.

The code does work - there i nothing wrong with the code. It works on wamp server. So there is something about my server that is not working.

What version of PHP is running on the servers?

If looks like the script you have may need the BOM removing, your editor should be able to this for you.

This should prove useful.

Thanks

I always save in utf-8. I thought that was problem free. I saved this again as ansi and it worked first time

How do I get rid of BOM’s