0

No output is being displayed in the index2.php after pressed submit button on index.php

    //index1.php
   <form action="index2.php" name="firstSubmit" method="POST">
<table border="0" cellspacing="10">
    <tr>
        <td>Full Name: </td>
        <td><input name="fullname" id="name" type="text" size="20" maxlength="80"></td>
    </tr>
    <tr>
        <td>Title:</td>
        <td><input name="title" id="title" type="text" size="20" maxlength="80"></td>
    </tr>
    <tr>
        <td>Company:</td>
        <td><input name="company" id="company" type="text" size="20" maxlength="80"></td>
    </tr>
    <tr>
        <td>Course:</td>
        <td>PHP Basics</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td colspan="2"><input type="Submit" name="submit" value="Proceed"></td>
    </tr>
</table>
</form>

//index2.php
<?php session_start();
$_SESSION['fullname']       = $_POST['fullname'];
$_SESSION['title']          = $_POST['title'];
$_SESSION['company']        = $_POST['company'];
$_SESSION['time']           = $_POST['time'];
?> 

<form action="certificate.php" name="certificateSubmit" method="POST">
<table border="0" cellspacing="10">
    <tr>
        <td>Full Name: </td>
        <td><?php echo  $_SESSION['fullname']; ?></td>
    </tr>
    <tr>
        <td>Title:</td>
        <td><?php echo  $_SESSION['title']; ?></td>
    </tr>
    <tr>
        <td>Company:</td>
        <td><?php echo  $_SESSION['company']; ?></td>
    </tr>
    <tr>
        <td>Course:</td>
        <td>PHP Basics</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td colspan="2"><input type="Submit" name="submit" value="Generate Certificate &gt; &gt;"></td>
    </tr>
</table>
</form>

---------I just updated the script. This time no warning about session_start() appears but no result is display after pressing process button+----

  • This looks almost like your warning got turned into an exception/fatal error. – mario Feb 25 '13 at 02:57
  • This doesn't make sense. The warning message doesn't match your description at all. I don't think you're showing us the actual code – Phil Feb 25 '13 at 03:11
  • 1
    Also, my guess is a [byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark) at line 1 of `index.php`. It's a tricky one to spot – Phil Feb 25 '13 at 03:13
  • @phil, turn that into an answer, that's a good solution. – JakeParis Feb 25 '13 at 03:15
  • 1
    @JakeParis It's answered already in the duplicate listed above – Phil Feb 25 '13 at 03:16

2 Answers2

1

Make sure there's no whitespace at the beginning of index.php and index2.php before the opening <?php, or after the closing ?>. Also, a tip that has been adopted as a best practice by many: don't include the closing ?> tag at the bottom of any file; when PHP encounters the end of a file it adds it automatically. I mention it because this can also help avoid whitespace issues.

As requested, here's an example of basic proper session usage (although the issue here really has more to do with debugging included files and whitespace, etc. to be sure no output is occurring before session_start is called): http://www.php.net/manual/en/session.examples.basic.php

Matt Browne
  • 12,169
  • 4
  • 59
  • 75
  • Not including the ?> in a PHP page should only be done if it doesn't display anything. –  Feb 25 '13 at 03:11
  • @MisterMelancholy Even if it does display something, the closing ?> (at the very bottom of a file) is unnecessary. When would not including it cause a problem? – Matt Browne Feb 25 '13 at 03:15
  • When I've tried told PHP to echo things and excluded the closing `?>`, it doesn't display. As soon as I add the `?>` to the end of the document, the content displays. I don't remember the exact code, but it's happened to me far more than once. –  Feb 25 '13 at 03:17
  • Interesting. I've just tried this on my home server, and it worked just fine. It may be some kind of special setting that I'm not aware of on our BlueHost servers at my work place. I was wrong. –  Feb 25 '13 at 03:20
  • ...might be a stupid question, but when this happened, you did have the semicolon after your `echo` at the bottom of the file, right? Otherwise, maybe the PHP team fixed this somewhere along the line. – Matt Browne Feb 25 '13 at 03:22
  • No white space at the beginning of index 2.php. I tried many times already but the problem still do not solved. the code is exactly as above. – user2089532 Feb 25 '13 at 03:24
  • Did you check into @Phil's suggestion about it being a byte order mark at the beginning of the file? (which you wouldn't be able to visibly see or delete - it's an encoding issue). Especially check into this if you ever saved the file using Windows Notepad. – Matt Browne Feb 25 '13 at 03:28
  • I should have looked at the error message more carefully...it tells you exactly where output started: at line 1 of index.php. I updated my answer to reflect this (mentioning whitespace at the end of the file as well for edification purposes, but in your case it's definitely line 1 of index.php that's the problem). – Matt Browne Feb 25 '13 at 03:33
  • Actually, I might be wrong about that...on my installation of PHP (version 5.3) if I force the error it says "headers already sent (output started at [line number])" but in your case I think the line number might be where sending the header is attempted...which is weird though because `session_start` is in your index2.php, not index.php. – Matt Browne Feb 25 '13 at 03:43
  • I just update the script – user2089532 Feb 25 '13 at 03:47
  • If you access `index2.php` directly from your web browser what do you get? – Matt Browne Feb 25 '13 at 03:48
  • Warning: session_start(): Cannot send session cookie - headers already sent in C:\Documents and Settings\Administrator\Desktop\gr\index2.php on line 1 Call Stack: 0.0013 326352 1. {main}() C:\Documents and Settings\Administrator\Desktop\gr\index2.php:0 0.0013 326384 2. session_start() C:\Documents and Settings\Administrator\Desktop\gr\index2.php:1 Warning: session_start(): Cannot send session cache limiter - headers already sent in C:\Documents and Settings\Administrator\Desktop\gr\index2.php on line 1 Call Stack: 0.0013 326352 1. {main}() – user2089532 Feb 25 '13 at 04:26
  • Settings\Administrator\Desktop\gr\index2.php:1 Notice: Undefined index: fullname in C:\Documents and Settings\Administrator\Desktop\gr\index2.php on line 2 Call Stack: 0.0013 326352 1. {main}() C:\Documents and – user2089532 Feb 25 '13 at 04:26
  • These are the error appear if direct run index2.php – user2089532 Feb 25 '13 at 04:27
  • 1
    PHP notices count as output...be sure to correct the Undefined index error. – Matt Browne Feb 25 '13 at 04:33
  • Can anyone give me the simple session source code that can really works? i nt sure if it is problem on the server.. – user2089532 Feb 25 '13 at 06:32
  • Updated answer with a link to a basic example. – Matt Browne Feb 25 '13 at 06:40
1

The problem is that you have a php comment before the opening <?php. Take away the //index2.php and you should be good to go.

Unless, of course, you just added that for this example! If that is the case, then just check that the <?php is the absolute first character in the file.

JakeParis
  • 11,056
  • 3
  • 42
  • 65