Jump to content

[SOLVED] Trouble when declaring "session_start()"???!!!


GuyFreakz

Recommended Posts

Hi guys, I'm a student who learns php. And now I'm learning about session.

I face a problem when i started session with session_start().

First, i declared the "session_start" in page1.php like this

 

<?php

  session_start();

  $_SESSION["username"]="Kay";

?>

 

And after that i running my opera browser and there is no problem when i entered http://localhost/practice/page1.php.

But then i make another page2.php

 

<?php

  session_start();

  $user = $_SESSION["username"];

  echo $user;

?>

 

and got these messages:

-------------------------------------------------------------------------

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\practice\page2.php:1) in C:\xampp\htdocs\practice\page2.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\practice\page2.php:1) in C:\xampp\htdocs\practice\page2.php on line 2

 

Kay

-------------------------------------------------------------------------

 

I've tried to close the browser but still the messages appeared. Then i copy the page2.php to another folder, and when i opened again there is no problem. My question is where is my fault?

 

Thank you  :)

Link to comment
Share on other sites

Make sure you do not have an enter before the <?php

 

IE:

WRONG


<?php
//code here
?>

 

RIGHT

<?php
// code here
?>

 

I've checked it many times and there is not even 1 space or tab. I'm not sure but after i followed the link given by corbin, they tell me that the trouble may lays on the text editor. I usually used Notepad++, but i've created the page1.php using TextPad and just for fun i made the second used notepad++. After that i copy the content of page2.php to new document i created in textpad and save it there but in different document.

So my deduction is there is error in Notepad++, when it create a new document, just maybe, it's automatically add an invisible character at the beginning of document.

Link to comment
Share on other sites

It's possible that \r (new line for most ms editors) or \n (newline for rest of the world:) might be added with some editors. These are invisible characters, these characters are recognised by the editor and shown.

 

When you open a .txt file with vi you see strange chars like ^M, this one refers to "\r". This might be the problem. I had encountered this problem too. Use an editor which you trust :)

Link to comment
Share on other sites

If its the text editor you are using, skip newline or \r or \n and just put it all on one line...

 

<?php session_start();

 

Then plug in your code from there.  You can also try OUTPUT BUFFERING (OB) but it may cause a performance hit. 

 

<?
ob_start();
session_start();

ob_end_flush();

$user = $_SESSION['user'];
echo $user;

Link to comment
Share on other sites

It's possible that \r (new line for most ms editors) or \n (newline for rest of the world:) might be added with some editors. These are invisible characters, these characters are recognised by the editor and shown.

 

When you open a .txt file with vi you see strange chars like ^M, this one refers to "\r". This might be the problem. I had encountered this problem too. Use an editor which you trust :)

 

Ho... so it can be like that huh?! I see that's explained why my code always generates error. Thx guyz ;D.

Link to comment
Share on other sites

When the error refers to output occurring on line 1 and you have checked and removed any possible content before the <?php tag, then the error is most likely due to the page being saved in UTF-8 encoding and the BOM (Byte Order Mark) characters that your editor places at the start of the file is the problem. Either save your file as ANSI/ASCII or if you must save it as UTF-8,  pick the option in your editor to save it without the BOM (or get a different editor.)

Link to comment
Share on other sites

When the error refers to output occurring on line 1 and you have checked and removed any possible content before the <?php tag, then the error is most likely due to the page being saved in UTF-8 encoding and the BOM (Byte Order Mark) characters that your editor places at the start of the file is the problem. Either save your file as ANSI/ASCII or if you must save it as UTF-8,  pick the option in your editor to save it without the BOM (or get a different editor.)

 

You're right!!! That's the best solution. After i changed the setting of my notepad++, there is no error anymore. Thank you very much. Thank you!!!  :D :D :D :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.