会话无法在我的Web服务器上运行

时间:2014-03-22 17:43:27

标签: php session

显示错误

  

警告:session_start()[function.session-start]:无法发送会话cookie -   已经发送的标题(输出开始于   /home/content/82/11942882/html/praveen/Allyface/index.php:8)in   第11行/home/content/82/11942882/html/praveen/Allyface/index.php

     

警告:session_start()[function.session-start]:无法发送会话缓存限制器 - >已发送的标头(输出始于> / home / content / 82/11942882 / html / praveen / Allyface / index。 php:8)在第11行的> /home/content/82/11942882/html/praveen/Allyface/index.php

的index.php

<?php
//simple PHP login script using Session
//start the session * this is important
session_start();

//login script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'login'){

//give your login credentials here
if($_REQUEST['uname'] != '' && $_REQUEST['pass'] != '')
{
    $email=$_REQUEST['uname'];
    $pass=$_REQUEST['pass'];
    //echo $email.$pass;
    include("db.php");
mysql_query("select * from users where email='$email' and pass='$pass' ");
if(mysql_affected_rows()>0)
{

$_SESSION['login_user'] = $email;
}
else
{
    $_SESSION['login_msg'] = 1;

}
}
else
$_SESSION['login_msg'] = 1;
}

//get the page name where to redirect
if(isset($_REQUEST['pagename']))
$pagename = $_REQUEST['pagename'];

//logout script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'logout'){
unset($_SESSION['login_user']);
header('Location:index.php');
}
if(isset($_SESSION['login_user'])){
if(isset($_REQUEST['pagename']))
{
?>
//header('Location:'.$pagename.'.php');
<script type="text/javascript">
self.location='<?php echo $pagename.'.php';  ?>';
 </script>
<?php
}
else
{
$email=$_REQUEST['uname'];
?>
   <script type="text/javascript">
    self.location='<?php echo 'home.php';  ?>';
     </script>
    <?php
    }
//header('Location:home.php');
}else{
?>


  <form id="" name="form1" method="post" action="">
  <table width="452" border="0">
    <tr>
      <td width="172">  <input name="uname" type="text" style="margin-top:15px;     width:150px; height:22px;   border:1px solid #CCCCCC; padding-left:30px;" placeholder="Email or Phone" />
</td>
      <td width="177">    <input name="pass" type="password" style=" margin-top:15px;   width:150px; height:22px;   border:1px solid #CCCCCC; padding-left:30px;"   placeholder="Password" />
</td>
      <td width="81"><input name="login" type="submit" style=" margin-top:15px; background-color:#FFFF00;background-color: #fdd922; margin-left:30px;
border: 1px solid #e0bc27; font-weight:bold;
border-radius: 2px 2px 2px 2px; height:25px; font-family:Verdana, Arial, Helvetica,       sans-serif; font-size:12px;  color: #565656;" placeholder="login" value="Login" /></td>
    </tr>
    <tr>
      <td colspan="3"><?php
//display the error msg if the login credentials are wrong!
if(isset($_SESSION['login_msg'])){
echo '<div style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;      color:#F00;">Wrong username and password !</div>';
unset($_SESSION['login_msg']);
}
?>    </td>
      </tr>
  </table>



  <input type="hidden" name="ch" value="login">
  </form>
  </div>
  <?php } 

?>

home.php

<?php 
session_start();

//check logged in or not!
if(!isset($_SESSION['login_user'])){
    ?>
//header('Location:index.php?pagename='.basename($_SERVER['PHP_SELF'], ".php"));

<script type="text/javascript">
self.location='<?php echo 'index.php?pagename='.basename($_SERVER['PHP_SELF'],".php"); ?    >';
 </script>
<?php
}
else
{
    include("db.php");
$uname=$_SESSION['login_user'];

$q=mysql_query("select * from users where email = '$uname'");
while($r=mysql_fetch_array($q))
{
$uid=$r['fname'];

?>

请帮帮我 提前致谢

3 个答案:

答案 0 :(得分:0)

确保在处理Sessions之前服务器没有发送数据。 这包括空格,空行或任何类型的数据。

答案 1 :(得分:0)

输出开始的位置显示在错误消息中:

output started at /home/content/82/11942882/html/praveen/Allyface/index.php:8

好像你有一个&#34;调度员&#34; index.php,它加载其他php文件(如login.php)。出现此错误的原因是在第8行的index.php中打印出来。

如果你在index.php上看不到任何内容,那么文件末尾可能会有换行符(如果你使用的是Vi,它会在每个文件上留下一个尾随的新行)

答案 2 :(得分:0)

启动输出后无法设置cookie(或发送任何其他标头)。你可以添加

ob_start()

在第1行缓冲输出。