-1

购物.php

<html>
<head>
<?php 
if(isset($_POST['sub'])){
    $a=$_POST['prod'];
    $b=$_POST['qty'];
    $_SESSION[$a]=$b;    
}    
?> 
</head>
<body>
<form method="post" action="Billing.php">

Select any Product:
<select name="prod">
   <option>Nokia</option>
   <option>Reliance</option>
   <option>Samsung</option>
   <option>LG</option>
</select>

<br><br>
Quantity:<input name="qty"> <br><br>

<input type="submit" name="sub" value="ADD">
</form>
</body></html>

计费.php

<html>
<head>

<?php  
session_start();
echo session_id();
echo "<br>";
echo "selected products are:<br>";
// print_r($_SESSION);
foreach($_SESSION as $x=>$y){  
   echo "product is $x and Quantity is:$y<br>";
}
?>
</head> </html>

输出

 Warning: session_start() [function.session-start]: Cannot send session cookie - 
 headers already sent by (output started at E:\PHP programs\Billing.php:2) in   E:\PHP     programs\Billing.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - 
headers already sent (output started at E:\PHP programs\Billing.php:2) in E:\PHP programs\Billing.php on line 4

请任何人提供上述警告消息的解决方案,我怎样才能成功执行程序?

4

4 回答 4

2

您需要session_start(); 在任何类型的输出之前放置(在文件的第一行)。session_start需要发送一个 cookie 标头,如果您已向用户发送任何输出,该标头将失败。

<?php
session_start();
?>
**shopping.php**

<html>

<head>

<?php 

if(isset($_POST['sub'])){
于 2013-09-19T07:53:13.553 回答
2

SESSION必须在文件的顶部。以 .开头的代码session_start();

看起来您首先声明<html>了然后您已经开始了会话,billing.php只需进行如下更改...

计费.php

<?php session_start(); ?>

<html>
<head>
于 2013-09-19T07:55:20.490 回答
0

可能你正在尝试做这样的事情......检查一下,有两个文件就像你创建的一样。您忘记做的主要事情是 session_start() 在您的第一个文件中。所以 $_SESSION 充当用户创建的数组变量而不是会话变量。

<?
    //you always need to start session before using the session in php
    session_start();
?>
<html>
<head>
<?php 
if(isset($_POST['sub'])){
    $a=$_POST['prod'];
    $b=$_POST['qty'];
    $_SESSION[$a]=$b;  

    // check if its being added in the session or not
    print_r($_SESSION);  
}    
?> 

</head>
<body>
<form method="post" action="">
Select any Product:
<select name="prod">
<option>Nokia</option>
<option>Reliance</option>
<option>Samsung</option>
<option>LG</option>
</select>
<br><br>
Quantity:<input name="qty"> <br><br>
<input type="submit" name="sub" value="ADD">
</form>
</body>
</html>
<? 
    //it is best way to close the session at the end of the file
    session_close();
?>


<? session_start();?>
<html>
<head><title>Billing.php file</title></head>
<body>
<?php
echo session_id();
echo "<br>";
echo "selected products are:<br>";
 print_r($_SESSION);

foreach($_SESSION as $x=>$y){
    echo "product is $x and Quantity is:$y<br>";
}
?>
</body>
</html>
于 2013-09-19T08:21:45.140 回答
0

** Billing.php **

<?php
session_start();
?>
<html>
<head>

删除您之前在 php 文件中可能有的任何新行或任何类型的输出,这些输出会session_start生成您发布的错误。

于 2013-09-19T07:56:06.050 回答