-2

I am developing an mobile application using jquery mobile and PHP. I encounter a warning when i submit the form for checking the credentials.

Warning: Cannot modify header information - headers already sent by (output started at /home/u387779804/public_html/walcliff/index.php:6) in /home/u387779804/public_html/walcliff/index.php on line 48

The php code

<?php session_start();?>

    <?php 
     if(isset($_POST['sec']))
     {
     $s=$_POST['sec'];
     $user="u387779804_a";
     $password="arunvenu123";
     $server="mysql.1freehosting.com";
     $con=mysql_connect($server,$user,$password);
     mysql_select_db("u387779804_a",$con);
     $query=mysql_query("select * from details where secretnum='$s'");
     if (!$query) { // add this check.
        die('Invalid query: ' . mysql_error());
    }
     $info=mysql_fetch_array($query);
     if($_POST['sec'] == $info['secretnum'])
     {  
        $_SESSION['fid']=$info['fid'];
        $_SESSION['phone']=$info['phone'];
        $_SESSION['name']=$info['name'];
        $_SESSION['email']=$info['email'];
        $_SESSION['address']=$info['address'];
        $_SESSION['city']=$info['city']; 
      header('Location:list.php'); 
    }
     else
     {
        echo '<script> alert("Wrong User name password")</script>';
     }
    } 
   ?>

I have attached the screen shoot also.Any possible solutions for this problem is welcomed! Thanks in advance! enter image description here

Boaz
  • 19,892
  • 8
  • 62
  • 70
Arun
  • 105
  • 9

1 Answers1

2

The second line is actually your problem here. That's a linebreak, which is considered starting of the body and thus means no-more headers can be sent.
Change the first three lines to the following:

<?php ob_start(); session_start(); ?>
<?php 
 if(isset($_POST['sec']))

Or, better yet:

<?php
 ob_start();
 session_start();
 if(isset($_POST['sec']))
Zoey Mertes
  • 3,139
  • 19
  • 23
  • Not sure how the first "solution" solves it, but the second one where you keep the PHP tags open works. – David Mar 23 '14 at 07:50
  • I have changed as per Zeke suggestion even i get the same warning – Arun Mar 23 '14 at 07:53
  • @Arun Did you try both solutions? – Zoey Mertes Mar 23 '14 at 07:54
  • yes Zeke I have tried both.finally i got the solution to it by adding ob_start() in my php code with session_start(). – Arun Mar 23 '14 at 07:56
  • You cannot send ANY text out to the browser and then attempt to send a new header. The empty line is the villain. I can't tell you how many times I've been burned by a blank line at the end of an include file. – Chris Caviness Mar 23 '14 at 08:14