I want to make authorization on sessions in PHP. The script below causes errors:

When logging in:

[17-Aug-2012 14:48:33] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/[...]/index.php on line 37

When exiting the script: (index.php? Logout)

[17-Aug-2012 14:49:39] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/[...]/index.php on line 37

[17-Aug-2012 14:49:39] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/[...]/index.php on line 42

And to exit does not redirect to index.php

header ("Location: index.php");

  <?php function draw_form($bad_login = false) { ?> <form action="" method="post"> <input type="text" name="login"></input><br/> <input type="password" name="pass"></input> <input type="submit" name="submit"></input> </form> <?php if ($bad_login) echo 'неправильный логин и/или пароль'; } function check_login($login, $pass) { return ($_POST['login'] == 'admin') && ($_POST['pass'] == '565656'); } session_start(); if (isset($_GET['logout'])) { session_unset(); session_destroy(); header("Location: index.php"); exit(); } if (!isset($_SESSION['login'])) { $login = $_POST['login']; $pass = $_POST['pass']; if (count($_POST) <= 0) draw_form(); else { if (check_login($login, $pass)) $_SESSION['login'] = $login; else draw_form(true); } } isset($_SESSION['login']) or die(); ?> 

How can I fix it? I do not understand what could be the case ...

  • This is authentication ... - timka_s
  • Thank you for the amendment =)) I'll write down on the crust - Factory

2 answers 2

session_start() must be called before any output to the browser. Try to move to the very beginning of the script.

  • I already tried ... - Factory

Try to give her a name before the session starts.

session_name('name'); eg.

  • PHP Warning: session_start () [<a href='function.session-start'> function.session-start </a>]: Cannot send session cache limiter - headers already sent (started at / home / - Factory
  • one
    then the problem is that you have something displayed before the session starts - C_oO_re
  • I put the session the very first: PHP Warning: session_start () [<a href='function.session-start'> function.session-start </a>]: Cannot send session cache limiter - headers /[...[/index.php on line 13 - Factory
  • @Factory, why the 13th line? Or do you have 12 blank lines? - BogolyubskiyAlexey
  • Because, there was a markup there)) Thanks to everyone for the help, I figured out ... - Factory