Thread: Php session

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2

    Php session


    1. file1.php

    <?php
    session_start();
    $_SESSION['favcolor'] = 'green';
    ?>
    <Form name ="form1" Method ="Post" ACTION ="radioButton.php">
    <Input type = 'Radio' Name ='gender' value= 'male'
    >m
    <Input type = 'Radio' Name ='gender' value= 'female'
    >f
    <P>
    <Input type = "Submit" Name = "Submit1" Value = "Select a Radio Button">
    </FORM>




    2. file2.php



    <?php
    session_start();
    echo $_SESSION['favcolor'];

    if (isset($_POST['Submit1'])) {

    $selected_radio = $_POST['gender'];

    if ($selected_radio == 'male') {

    print $selected_radio;
    }
    else if ($selected_radio = 'female') {

    print $selected_radio;
    }
    }

    ?>

    once run file1.php

    i get error as below:

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/48/8466248/html/sqlplsql24/form/radioButton.php:3) in /home/content/48/8466248/html/sqlplsql24/form/radioButton.php on line 4
    greenmale


    Please advise.

    -Phpstudent
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    PHP Code:
    session_start(); 
    should be at the very top of the document.

    (so before anything is outputted to the browser)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    I updated as below. put session start at the top and i still get error message in both pages.


    Error:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/48/8466248/html/sqlplsql24/form/MathS.php:4) in /home/content/48/8466248/html/sqlplsql24/form/MathS.php on line 5


    1.
    <html>
    <?php
    session_start();
    ?>
    <body>

    <?php

    $_SESSION['num1'] = mt_rand(2,5);
    $q1 = $_SESSION['num1'] ;
    ?>

    <form name="lab5" method="post" action="MathS.php">

    <table border=1 cellspacing=0 cellpading=0>
    <tr> <td> Q1 </td> <td> <?php echo $q1 ?> * <?php echo $q2 ?> </td> <td> <input type = 'text' size = '2' name = one > </td> </tr>
    <tr><td colspan=6 align='right'><input type='submit' value='Score' name='btn_score'></td></tr>
    </table>
    </form>

    </body>
    </html>


    2.



    <html>
    <?php
    session_start();
    ?>
    <body>
    <?php
    $UserV = $_POST['one'];

    $f1 = $_SESSION['num1'];
    echo $f1 ;
    ?>

    </body>
    </html>

    I get out put just fine just getting the error message.
    Please help!!
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    I updated as below. put session start at the top and i still get error message in both pages.
    No you didn't. The top of your page is the very top
    if anything is outputted to your browser, you get that "headers already sent"-error

    Have a read here for the idea behind it


    PHP Code:
    <?php
        session_start
    ();  // this must be placed above everything else
        // any echo or html below this
        
    $_SESSION['some_var'] = 'bla';
        
    ?>
    <!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <?php  echo $_SESSION['some_var']; ?>
    </body>
    </html>
    Last edited by aeternus; December 23rd, 2012 at 03:13 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    Some improvements:

    file1.php i dont have the error as i put the session code on the top but i still get the error for file2.php even thou i have session on the top.

    Please advise.

    ---------------
    1. file1.php

    <?php
    session_start();
    ?>

    <html>

    <?php

    $_SESSION['num1'] = mt_rand(2,5);
    $q1 = $_SESSION['num1'] ;
    ?>

    <form name="lab5" method="post" action="MathS.php">

    <table border=1 cellspacing=0 cellpading=0>
    <tr> <td> Q1 </td> <td> <?php echo $q1 ?> * 2 </td> <td> <input type = 'text' size = '2' name = one > </td> </tr>
    <tr><td colspan=6 align='right'><input type='submit' value='Score' name='btn_score'></td></tr>
    </table>
    </form>


    </html>

    2. file2.php

    <?php
    session_start();
    ?>

    <html>

    <body>
    <?php
    $UserV = $_POST['one'];

    $f1 = $_SESSION['num1'];
    echo $f1 ;
    ?>

    </body>
    </html>
  10. #6
  11. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    I edited my message and included a link to a resource that i recommend you read: http://stackoverflow.com/questions/8...dy-sent-by-php

    Your probably have some whitespace before the <?php tag if you really put it at the very top. If not post the exact error you got. (that should give the line of your script where something is outputted. Either whitespace or other stuff
    Last edited by aeternus; December 23rd, 2012 at 03:19 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,403
    Rep Power
    594
    Check for white space at the top. You cannot have anything first, not even a space.

    P.S. Please enclose your code in [ PHP ] tags. See the sticky at the top of the forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    I get this error after i click score.

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/48/8466248/html/sqlplsql24/form/MathS.php:1) in /home/content/48/8466248/html/sqlplsql24/form/MathS.php on line 2
    2
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,403
    Rep Power
    594
    You still have white space before the <?php.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Look at the error and read it. It clearly states:
    Code:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/48/8466248/html/sqlplsql24/form/MathS.php:1) in /home/content/48/8466248/html/sqlplsql24/form/MathS.php on line 2
    #1 So Go to the file MathS.php
    #2 go to line 2 of that file and notice either whitespace or other stuff that is outputted to the browser.
    #remove that and make sure session_start(); is the first thing and there is no whitespace before the <?php tag

    Did you read the article I linked?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    <?php
    session_start();
    ?>

    Top of MathS looks as above.

    I dont have any white spaces before the start of first 3 lines.

    Still getting the error.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    If i put the content of MathS into Math, i dont get the error.

    Not sure whats happening.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    I deleted the MathS file and did all from the scratch. Now I dont see the error.

    Thanks for your help.
  26. #14
  27. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    You probably had a byte-order-mark at the beginning of the MathS file. When you switch encoding in your editor, most have two options: UTF-8 and UTF-8 with a BOM. A BOM is not a printable character so it will not show in your editor, but does count as output.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo