+ Reply to Thread
Results 1 to 6 of 6

Thread: session is not woking after a header function plz help me

  1. #1
    itcep200870 is offline x10Hosting Member itcep200870 is an unknown quantity at this point
    Join Date
    Jun 2011
    Posts
    3

    session is not woking after a header function plz help me

    my php code for login
    PHP Code:
    <?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 Code:
    <?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 by itcep200870; 06-23-2011 at 01:56 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: session is not woking after a header function plz help me

    Code:
    $_session['username']=$_POST[username];
    $_SESSION
    Nothing is always absolutely so.

  3. #3
    roelpaulo93's Avatar
    roelpaulo93 is offline x10Hosting Member roelpaulo93 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    17

    Re: session is not woking after a header function plz help me

    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.

  4. #4
    itcep200870 is offline x10Hosting Member itcep200870 is an unknown quantity at this point
    Join Date
    Jun 2011
    Posts
    3

    Re: session is not woking after a header function plz help me

    Quote Originally Posted by roelpaulo93 View Post
    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 Code:
    <?php
    session_start
    ();

    $_SESSION['test'] = 'testing';
    ?>
    test file 2
    PHP Code:
    <?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 by itcep200870; 06-24-2011 at 04:56 AM.

  5. #5
    roelpaulo93's Avatar
    roelpaulo93 is offline x10Hosting Member roelpaulo93 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    17

    Re: session is not woking after a header function plz help me

    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 Code:
    <?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 callumacrae; 06-24-2011 at 07:45 AM. Reason: Added [php] tags

  6. #6
    itcep200870 is offline x10Hosting Member itcep200870 is an unknown quantity at this point
    Join Date
    Jun 2011
    Posts
    3

    Re: session is not woking after a header function plz help me

    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

+ Reply to Thread

Similar Threads

  1. php mail function using From header
    By updatedoffers.com66 in forum Free Hosting
    Replies: 0
    Last Post: 02-20-2011, 04:18 AM
  2. Replies: 4
    Last Post: 07-17-2009, 10:15 AM
  3. Problem using php header() function
    By fguy64 in forum Programming Help
    Replies: 17
    Last Post: 05-18-2009, 10:42 PM
  4. Function header()
    By mrnigma in forum Free Hosting
    Replies: 5
    Last Post: 05-01-2009, 08:17 AM
  5. header function fails even on pure php pages
    By geirgrusom in forum Programming Help
    Replies: 8
    Last Post: 04-05-2009, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers