Click to See Complete Forum and Search --> : session difficulty


Lucky-8
March 19th, 2008, 12:16 AM
hello,

My aim is to acheive user authentication using session. on my local server (wamp) on my system it seems to work fine but when i upload the files to my host server i get this "headers sent" warning and eventually the session does not get created and i can further navigate the website.

below is the code

<?php include "mysql.php" ?>

<?php

ob_start();

// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

$obj=new db();
//$obj-> connect("root","","drycleaning");
$obj->connect("scp_training","vTech123","scp_training");
//$obj->connect("drycleaning","vTech123","drycleaning");


$query="SELECT * FROM user_info WHERE email='$myusername' and password='$mypassword'";
$result=$obj->query($query);

$count = $obj->numrow($result);


if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername']=$myusername;

include("home2.php");
}
else {
include("index2.php");
}

ob_end_flush();
?>



And at the begining of everypage i have the following code embedded.



<?php
session_start();
if(!$_SESSION['myusername'])
{
header("location:index.php");
}
?>

PeejAvery
March 19th, 2008, 10:44 AM
If you are receiving the error Warning: Cannot modify header information - headers already sent, this means that you have outputted some HTML before all headers within your PHP code. Make sure that all PHP for this sessions is before any HTML as well.

Lucky-8
March 19th, 2008, 11:09 AM
If you are receiving the error Warning: Cannot modify header information - headers already sent, this means that you have outputted some HTML before all headers within your PHP code. Make sure that all PHP for this sessions is before any HTML as well.


I have posted the code in my question and as you can see there isnt any html before the php code.

Once i type in the username and password i seem to get this warning.

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/content/n/a/k/nakad007/html/drycleaning/mysql.php:9) in /home/content/n/a/k/nakad007/html/drycleaning/home2.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/content/n/a/k/nakad007/html/drycleaning/mysql.php:9) in /home/content/n/a/k/nakad007/html/drycleaning/home2.php on line 5

PeejAvery
March 19th, 2008, 02:02 PM
No, you haven't posted all the code in question. The problem is in home2.php as clearly stated by the error log.

Lucky-8
March 19th, 2008, 02:37 PM
No, you haven't posted all the code in question. The problem is in home2.php as clearly stated by the error log.

hey i guess i resolved the error.Does white space also help in sending headers, coz as soon as i deleted all the white space, it seemed to work just fine.

Another question i have is, if i write some code in php.... and after lets say n lines i do some kind of condition check and if the condiiton fails i just want to load another page, how do i do it.

there are two ways i looked up from various resources,

1. is to do all the checking before the headers are sent and then load the desired page.

2. use include function ( which is not wat i want because it still carries the html layout from the previous file and includes it in the middle.


There is some super global phpserver could you throw some light on that.

Nibinaear
March 19th, 2008, 03:20 PM
Yes, even a blank line before your opening <?php will cause the headers already sent error. This is a common mistake that lots of people make when using sessions for the first time.