Jump to content

how to supress/avoid header related warning


mintoo2cool

Recommended Posts

i have a file connection.php, that contains functions to connect and disconnect from database.

 

the functions are as follows:

<?php
//connection.php file
function connect()
{
  $con = mysql_connect("localhost","abcd","123456");
  if(!$con)
    die ('Error! could not connect to data to add data');
  return $con;
}
function disconnect($con)
{
   mysql_close($con);
}

?>

 

I my intent is to use these functions whenever i would need to connect to database.

 

When i include this file, using include() or require(). I get the warning message, and the page does not redirect to the next page.

 

For example I have created a login page that accepts username and password. The data from this form is "processed" in a login_proc.php file, that redirects the browser to home.php or loginerror.php. But instead of working as I desire, PHP stops with a warning stating the following.

Warning: Cannot modify header information - headers already sent by (output started at /home1/mintoo/public_html/bew_system/login_proc.php:5) in /home1/mintoo/public_html/bew_system/login_proc.php on line 20

 

How do I avoid this warning?

 

This is the code in login_proc.php for your reference

<?php
//login_proc.php
include 'resources/connection.php';
$con= connect();
ob_flush();
$uname = mysql_real_escape_string(strip_tags(substr($_POST['uname'],0,30)));
$pass = mysql_real_escape_string(strip_tags(substr($_POST['pwd'],0,30)));
$query = "select userid from mintoo_db1.tbl_users where userid='$uname' and password='$pass'";
$result = mysql_query($query);
$nofrows = mysql_num_rows($result);
disconnect($con);
if($nofrows == 0)
echo '<h2>Incorrect Username or Password</h2><br/><a href="login.php">Try again</a>';
else if($nofrows == 1)	
header("location: home.php");
else
echo '<h2><u> Error-001:</u>An unexpected error has occured. Please go back and try again after some time or contact system administrator</h2>';
?>

 

 

EDIT: Solved it

 

Made the following changes to login_proc.php

<?php
ob_start();
include 'resources/connection.php';
$con= connect();


$uname = mysql_real_escape_string(strip_tags(substr($_POST['uname'],0,30)));
$pass = mysql_real_escape_string(strip_tags(substr($_POST['pwd'],0,30)));
$query = "select userid from mintoo_db1.tbl_users where userid='$uname' and password='$pass'";

$result = mysql_query($query);
$nofrows = mysql_num_rows($result);
disconnect($con);
if($nofrows == 0)
echo '<h2>Incorrect Username or Password</h2><br/><a href="login.php">Try again</a>';
else if($nofrows == 1)
{

	header("location: home.php");

}
else
echo '<h2><u> Error-001:</u>An unexpected error has occured. Please go back and try again after some time or contact system administrator</h2>';
ob_flush();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.