Include Login.php In Index.php Error

Discussion in 'Programming' started by sabo, Feb 19, 2013.

  1. #1
    Hello. I created a login form in php. And i have problem, after introducing login data, and i shows me the following message.
    Warning: Cannot modify header information - headers already sent by (output started at /home/graphic/public_html/loginform/index.php:31) in /home/graphic/public_html/loginform/login.php on line 31

    Index.php
    <?php ob_start(); ?>
    <html>
     
     
        <head>
            <title>SD Previous Project</title>
            <link rel="stylesheet" title="default" href="../css/style.css" type="text/css" />
        </head>
    <body>
    <div class="content">
     
    <?php
    include 'login.php';
    ?>
     
    <div>
     
    </body>
    </html>
    <? ob_flush(); ?>
    Code (markup):
    login.php
    <?php
     
    include 'dbc.php';
     
    $user_email = mysql_real_escape_string($_POST['email']);
     
    if ($_POST['Submit']=='Login')
    {
    $md5pass = md5($_POST['pwd']);
    $sql = "SELECT id,user_email FROM users WHERE
                user_email = '$user_email' AND
                user_pwd = '$md5pass' AND user_activated='1'";
           
    $result = mysql_query($sql) or die (mysql_error());
    $num = mysql_num_rows($result);
     
        if ( $num != 0 ) {
     
      $_SESSION['user']= $user_name;
      $_SESSION['uid'] = $assoc['id'];
     
          list($user_id,$user_name) = mysql_fetch_row($result);
     
     
       
          if (isset($_GET['ret']) && !empty($_GET['ret']))
          {
          header("Location: $_GET[ret]");
          } else
          {
          header("Location: myaccount.php");
          }
          //echo "Logged in...";
          exit();
        }
     
    header("Location: login.php?msg=Invalid Login");
    //echo "Error:";
    exit(); 
    }
     
    ?>
     
    <link href="styles.css" rel="stylesheet" type="text/css">
     
    <?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>
     
     
    <p>&nbsp;</p><table width="40%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td bgcolor="#d5e8f9" class="mnuheader" >
    <div align="center"><font size="5"><strong>Login
            Members</strong></font></div></td>
      </tr>
      <tr>
        <td bgcolor="#e5ecf9" class="mnubody"><form name="form1" method="post" action="">
            <p>&nbsp;</p>
            <p align="center">Your Email
              <input name="email" type="text" id="email">
            </p>
            <p align="center"> Password:
              <input name="pwd" type="password" id="pwd">
            </p>
            <p align="center">
              <input type="submit" name="Submit" value="Login">
            </p>
            <p align="center"><a href="register.php">Register</a> | <a href="forgot.php">Forgot</a></p>
          </form></td>
      </tr>
    </table>
     
    
    Code (markup):
    myaccount.php
    <?php
    if (!isset($_SESSION['user']))
    {
    die ("Access Denied");
    }
    ?>
    <h2>My Account </h2>
    <?php if (isset($_SESSION['user'])) { ?>
    <p>Logged as <?php echo $_SESSION['user']; ?> | <a href="settings.php">Settings</a>
      | <a href="logout.php">Logout</a> </p>
    <?php } ?>
    
    Code (markup):
    Where i wrong?
     
    Last edited: Feb 19, 2013
    sabo, Feb 19, 2013 IP
  2. tyteen4a03

    tyteen4a03 Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #2
    From the PHP manual:

    I am guessing that the HTML you included already triggers some header outputs, causing this error. (I never hard-encode HTML into my PHP files, so I don't really know)

    Your current code design is very ugly. You should use a templating engine (Twig), or just $html = 'HTML HERE' would do. Just make sure you don't hard-encode HTML ever again.
     
    tyteen4a03, Feb 19, 2013 IP
  3. sabo

    sabo Member

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    Error not comes from html.From what I know comes from redirect.
    if (isset($_GET['ret']) && !empty($_GET['ret']))
          {
          header("Location: $_GET[ret]");
          } else
          {
          header("Location: myaccount.php");
          }
          //echo "Logged in...";
          exit();
        }
    Code (markup):
    But i don't see some error in programing.
     
    sabo, Feb 19, 2013 IP
  4. tyteen4a03

    tyteen4a03 Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #4
    The error does not come from the HTML - it comes from how you hard-encode HTML. This part:
    <?php ob_start(); ?>
    <html>
        <head>
            <title>SD Previous Project</title>
            <link rel="stylesheet" title="default" href="../css/style.css" type="text/css" />
        </head>
    <body>
    <div class="content">
    
    Code (markup):
    By doing this you are sending out the header to the browser, and therefore all subsequent header() calls will give you a warning.
     
    tyteen4a03, Feb 19, 2013 IP
  5. sabo

    sabo Member

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    Not is this problem. I modify and nothing.
     
    sabo, Feb 19, 2013 IP
  6. tyteen4a03

    tyteen4a03 Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #6
    You must remove ALL hard-coded HTML from the script, and manually echo them instead.
     
    tyteen4a03, Feb 19, 2013 IP