session_start() and header()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phppatanas
Forum Newbie
Posts: 1
Joined: Thu May 20, 2010 7:08 am

session_start() and header()

Post 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>
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: session_start() and header()

Post by mikosiko »

just to start... check the usage of your "{" "}" you have at least 3 unbalanced
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: session_start() and header()

Post 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().
jaiswarvipin
Forum Commoner
Posts: 32
Joined: Sat Sep 12, 2009 3:43 pm
Location: India

Re: session_start() and header()

Post 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>
Post Reply