session is not woking after a header function plz help me

itcep200870

New Member
Messages
3
Reaction score
0
Points
0
my php code for login
PHP:
<?php 
$db_host = 'localhost'; // don't forget to change 
$db_user = 'cepit08_root'; 
$db_pwd = 'root1';
$database = 'cepit08_cepit';
if (!mysql_connect($db_host, $db_user, $db_pwd))
   die("Can't connect to database");
if (!mysql_select_db($database))
  die("Can't select database");
if($res=mysql_fetch_array(mysql_query("SELECT * FROM login WHERE username='$_POST[username]' AND password = '$_POST[password]'"))){
    if(($res['username']==$_POST[username])&&($res['password']==$_POST[password])){    
      session_start();
         
     $_session['username']=$_POST[username];
    
     header("Location:http://www.cepit08.x10.mx/mainpage.php");
     die();
    } 
}    
else {    
    header("Location:http://www.cepit08.x10.mx/index.php?msg=Invalid username or password");
}
?>
code in main page .php

PHP:
<?php
session_start();

if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
}
else
{
echo "session variable was not set";
}
?>
results output
session variable was not set

help me plzzzzzzzzzzzz
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
$_session['username']=$_POST[username];

$_SESSION
 

roelpaulo93

New Member
Messages
17
Reaction score
0
Points
1
A header() function cannot be called if you already executed PHP or HTML before that. The problem is output_buffering is turned off on this server. You have to call ob_start() first at the top of your file before anything else and ob_end_flush() at the bottom to end the ob_start(). That way, you can execute header() and session_start() even if you output already HTML or PHP before it. Hope this helps.
 

itcep200870

New Member
Messages
3
Reaction score
0
Points
0
A header() function cannot be called if you already executed PHP or HTML before that. The problem is output_buffering is turned off on this server. You have to call ob_start() first at the top of your file before anything else and ob_end_flush() at the bottom to end the ob_start(). That way, you can execute header() and session_start() even if you output already HTML or PHP before it. Hope this helps.
i tried your suggestion but its not working
now i think it is not the problem of header but the session variable is not setting since i had executed a session testing script
it works correctly
test file 1
PHP:
<?php
session_start();

$_SESSION['test'] = 'testing';
?>
test file 2
PHP:
<?php
session_start();

if (isset($_SESSION['test']))
{
echo $_SESSION['test'];
}
else
{
echo "session variable was not set";
}

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>
executed seprately
results an output



testing
Array ( [test] => testing )
 
Last edited:

roelpaulo93

New Member
Messages
17
Reaction score
0
Points
1
No, you have to call ob_start() before anything else, and then after everything, you have to call ob_end_flush() at the end of your document. Like this...
PHP:
<?php

   // You have to call this first...
   ob_start();
   
   // And then all your PHP commands goes next...
   session_start();
   header("Location: http://linktosomewhere.com");
   exit;
?>

<!--Your html goes here or whatever-->

<?php

   // And then to end the ob_start() which goes...
   ob_end_flush();

?>

hope this helps...
 
Last edited by a moderator:

itcep200870

New Member
Messages
3
Reaction score
0
Points
0
thankz roelpaulo
i solved this problem by dual reirect ie
>>
after login i redirect to a page

>>
from that page i redirect to main page


now working correctly

thanks for Ur valuable suggestion

further suggestion are welcomed
 
Top