Page 1 of 1

session_start() and header()

Posted: Thu May 20, 2010 7:45 am
by phppatanas
Hi!
Before, I had this problem with session_start() - Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Users\Administrador\Documents\xampp\htdocs\login.php:1) in C:\Users\Administrador\Documents\xampp\htdocs\login.php on line 3

I've been surfing a bunch of php forums and they all say that i can't have any blank spaces before <?php tag. I've corrected it and now... nothing happens! It doesn't send the error message, but nothing happens!

The purpose is that, after a correct login, it opens a new window with the current user's name. but the new page doesn't open.
If I create a regular html link (with <a>), i manage to open the new page, but the session user name is not sent.

Can anyone help me, please??! :banghead:

Code: Select all

<?php
ob_start();
session_start();
$user= $_POST['user'];
$pwd= $_POST['pwd'];
if ($user && $pwd)
{
$connection = mysql_connect("localhost", "root","") or die("Sem ligação aos registos!");
$db=mysql_select_db("logins",$connection) or die("bd não encontrada!");
$sql = "SELECT * FROM users WHERE username='".$user."'";
$query=mysql_query($sql);
$total=mysql_num_rows($query);
if ($total!=0)
{
   $linha=mysql_fetch_assoc($query);
   $dbuser=$linha['username'];
   $dbpwd=$linha['password'];
   if ($dbuser==$user && $dbpwd==$pwd)
   {
      if (!isset($_SESSION['nome'])) 
          $_SESSION['nome']=$user;
      header("Location:lista_users.php");
   }
   else
   {
         // several error messages in case user and/or password incorrect
    }  
?>
<html>  settings for background and font </html>

Re: session_start() and header()

Posted: Thu May 20, 2010 8:49 am
by mikosiko
just to start... check the usage of your "{" "}" you have at least 3 unbalanced

Re: session_start() and header()

Posted: Thu May 20, 2010 12:00 pm
by Chalks
ob_start() is your problem.
php.net wrote:This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
You need to either stop using output buffering, or flush your output with ob_end_flush().

Re: session_start() and header()

Posted: Thu May 20, 2010 2:31 pm
by jaiswarvipin

Code: Select all

<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
$user= $_POST['user'];
$pwd= $_POST['pwd'];
if ($user && $pwd)
{
$connection = mysql_connect("localhost", "root","") or die("Sem ligação aos registos!");
$db=mysql_select_db("logins",$connection) or die("bd não encontrada!");
$sql = "SELECT * FROM users WHERE username='".$user."'";
$query=mysql_query($sql);
$total=mysql_num_rows($query);
if ($total!=0)
{
   $linha=mysql_fetch_assoc($query);
   $dbuser=$linha['username'];
   $dbpwd=$linha['password'];
   if ($dbuser==$user && $dbpwd==$pwd)
   {
      if (!isset($_SESSION['nome'])) 
          $_SESSION['nome']=$user;
      header("Location:lista_users.php");
   }
   else
   {
         // several error messages in case user and/or password incorrect
    }  
?>
<html>  settings for background and font </html>