1

I have a really nasty problem with sessions and includes in PHP. I have a page called index.php in which I have a switch function between the different pages:

<?php $p=(isset($_GET["p"])?$p=intval($_GET["p"]):$p=1); switch($p) {
case 1: $pagina = "login_and_registration.php";
break;
case 2: $pagina = "admin/registrazione.php";
break;
case 3: $pagina = "admin/login_test.php";
break;
case 4: $pagina = "logged_gym.php";
break;
case 5: $pagina = "logged_trainer.php";
break;
case 7: $pagina = "logout.php";
break;
case 8: $pagina = "admin/completa_profilo_trainer_sql.php";
break;
case 10: $pagina = "error.php";
break;
case 12: $pagina = "profilo_min.php";
break;
case 13: $pagina = "splash.php";
break;
default: $pagina = "index.php";
break;} ?>

Then down the code in the body of the file i have some includes:

<header class="container_12">
    <?php include("login_errors.php")?>
    <div id="logo_container" class="grid_4 push_4"></div></header >

And this one:

<section>
    <?php include_once($pagina);?>
</section>

When I fill the form which is in this page, corresponding to page 1 in the switch function, i get this error on the login (where I start a new session with a name):

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/FitnessOptimize/index.php:63) in /Applications/MAMP/htdocs/FitnessOptimize/admin/login_test.php on line 73

In the index.php on line 63 i have the first include that I just posted you. Any ideas on how to avoid this bug? Also my header ("Location: "); don't work i think for the same reason. Any help will be appreciated! Thanks to you all!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1447316
  • 123
  • 2
  • 11
  • Where are you calling `session_start()` ? It should be before *anything* has been sent to back to the browser. – Brendan Sep 17 '12 at 13:14
  • I haven't began the session anywhere. I want to start it after the login, not before! – user1447316 Sep 17 '12 at 13:14
  • Or if I have to be more precise: i want to have two different sessions because I have two different types of users. When I create only one session and I login to two different accounts, one escapes and the other one continues to exist (the older escapes, the new one goes on). I want to fix that issue! – user1447316 Sep 17 '12 at 13:16
  • you have to initialize session with `session_start()` if theres even a possibility you might need them. You can't do it after any output, and thats jsut the way it is. – Tom Sep 17 '12 at 13:17

4 Answers4

2

No output before sending headers!

the header error is caused if you have typed anything before header ,even white spave before the <?php will cause the error

for more information check the Headers already sent by PHP question on the stackoverfow

check this link to Having two different sessions in same domain

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • Ok so lets say that I put on top the session start! When I get logged in, I have the same session! I need two different sessions! – user1447316 Sep 17 '12 at 13:19
  • @user1447316 You should call session_name before calling session_start. This sets the name of the cookie used to identify the session (by default this is PHPSESSID). – NullPoiиteя Sep 17 '12 at 13:22
1

use session_start(); function at the top of the page on which you are using $_SESSION .Be ensure that you donot have a single space before session_start()

in php first of all all the header transfers and than the text is sent to browser if there is a single space is present your session will not be started and you cannot use $_SESSION[] so write this at the top of the page

StaticVariable
  • 5,253
  • 4
  • 23
  • 45
  • So you suggest: `code` – user1447316 Sep 17 '12 at 13:18
  • You maybe don't understand the real issue! Let me point it out better: i have two types of users: gyms and trainers. When I log in, the last logged in overwrites the one logged before...let's say I'm logged in like trainer Marco. Then I login Like Sarah. When I make a refresh on the page of Marco it pushes me out saying that session is not opened! – user1447316 Sep 17 '12 at 13:27
  • 1
    why you allow user to login again if session is already set.If user is already logged in than redirect to another page don't let him login again. – StaticVariable Sep 17 '12 at 13:49
  • Hm..so you suggest that only one user per pc can be logged in? It has sence! I have to figure out how to play this nicely! I'll have a try! Hmm..but I'm thinking..if i put my session_start(); in the index, that session will never be expired. It will be always open..that is what is actually happening now! I have to open the session when the users logs in..not when the user hits my index (which is actually the page which includes all the others, including the connected pages)..and here we stuck again with the same problem! – user1447316 Sep 17 '12 at 15:27
  • the problem is that you have to start the session in index.php bcoz when user is logged in and if you want to check something on index.php than the session should be opened that time other wise it will say session is undefined ...! – StaticVariable Sep 19 '12 at 13:32
0
session_start();

should be at the beginning of the page

Surace
  • 701
  • 3
  • 8
0

The problem may be caused by unprintable characters at the start of code, or you're writing some value in header.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • hm..can i do something like this for the other two sessions `case 4: $pagina = "logged_gym.php"; session_name("something") break; case 5: $pagina = "logged_trainer.php"; session_name("something_else") break;` – user1447316 Sep 17 '12 at 13:21
  • Appereantly I can! Interesting..let's see if I have fixed my original issue! – user1447316 Sep 17 '12 at 13:24
  • The only solution seems to be this one: put connected pages on their own, not passing through the index. In this way I can open a session on each of the pages and check if there's another one opened. – user1447316 Sep 17 '12 at 15:32