Page 1 of 1

php Redirect with Header

Posted: Thu Aug 09, 2007 10:55 am
by ectpoon
Hi,

I am having this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/webgotes/public_html/script/login_sc.php:1) in /home/webgotes/public_html/script/login_sc.php on line 52

in login_sc.php line one... it is:

require 'db_connect.php';

and before line 52... it is all php code accessing mysql database to check the login username and password

the header function is not working with the require

in the db_connect.php file, it is only one function.
if i put the function directly in login_sc.php then the header works prefectly. however, the same function will be used throughout the application, I need to figure out why header will not work if i do require...

can anyone give me a hand?

Thanks

Posted: Thu Aug 09, 2007 11:10 am
by iknownothing
Not without you posting the code..

Posted: Thu Aug 09, 2007 11:16 am
by RobertGonzalez
Search. Please.

This is the most common problem posted about on these forums. This same question is asked about 5 times a week. In fact, I answer this question about once a week and have already done so this week.

Just try searching. If you really can't find what you are looking for, post back.

Posted: Thu Aug 09, 2007 11:18 am
by ectpoon
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here are the code..


login_sc.php

Code: Select all

<?php

require 'db_connect.php';
// database connect script.

session_id(strip_tags($_GET['sid']));
session_start();

//if($logged_in == 1) {
//	die('You are already logged in, '.$_SESSION['username'].'.');
//}
if (isset($_POST['submit'])) { // if form has been submitted
	/* check they filled in what they were supposed to and authenticate */

	if(!$_POST['uname'] | !$_POST['passwd']) {
		$_SESSION['errmsg'] = "You did not fill in a required field.";
		header("Location: http://www.webgomedia.com/~webgotes/login.php?sid=".session_id());
		//die('You did not fill in a required field.');
	} else {
		// authenticate.
		//if (!get_magic_quotes_gpc()) {
		//	$_POST['uname'] = addslashes($_POST['uname']);
		//}
	
		$check = execute_sql("SELECT username, password FROM user WHERE username = '".$_POST['uname']."'");
		if ( count($check) == 0 ) {
			$_SESSION['errmsg'] = "That username does not exist.";
			header("Location: http://www.webgomedia.com/~webgotes/login.php");
			//die('That username does not exist.');
		} else {
			// check passwords match
			//$_POST['passwd'] = stripslashes($_POST['passwd']);
			$pwd = stripslashes($check[0]['password']);
			//$_POST['passwd'] = md5($_POST['passwd']);
		
			if ($_POST['passwd'] != $pwd) {
				$SESSION['errmsg'] = "Incorrect password, please try again.";
				header("Location: http://www.webgomedia.com/~webgotes/login.php");
				//die('Incorrect password, please try again.');
			} else {
				// if we get here username and password are correct, 
				//register session variables and set last login time.
				$_SESSION['username'] = $_POST['uname'];
				$_SESSION['password'] = $_POST['passwd'];
				$_SESSION['blogin'] = 1;
				unset($_SESSION['errmsg']);

				//redirect to the main index page
				$loc = "Location: http://www.webgomedia.com/~webgotes/search.php?sid=".session_id();
				header($loc);
			}
		}
	}
}
?>

db_connect.php

Code: Select all

<?php
function execute_sql($query) {
		// fetch query from server
		$link = mysql_connect($DBADDR, $DBUSR, $DBPWD);
		if (!$link) die('Could not connect: ' . mysql_error());
		
		$db_selected = mysql_select_db($DB, $link);
		
		if (!$db_selected) {
		    die('Could not select database');
		}		

		// Performing SQL query
		$result = mysql_query($query) or die("Query failed: ".mysql_error()."\n$query");
		
		// Printing results in HTML
		// header("Content-Type: application/xml; charset=utf-8");   // this must be the first line of output
		
		$i = 0;
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		   foreach ($line as $col_name => $col_value) {
		        // TODO: escape $col_value characters like < and > and other special XML characters
		        $row[$col_name] = $col_value;
		   }
		   $rs[$i] = $row;
		   $i++;
		}
		
		// cleanup
		mysql_free_result($result);
		mysql_close($link);
		
		return $rs;
}
?>
Thanks a lot


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Aug 09, 2007 12:32 pm
by ectpoon
i couldn't find post about the header problem..
and the search engine doesn't work quite well...

please if you don't mind helping me out here.

thanks

Posted: Thu Aug 09, 2007 12:39 pm
by RobertGonzalez
I searched these forums (using the Search link at the top of the page) for the term 'headers already sent' and got back 669 results. What did you search for?

The thing to remember with sending response headers (like when using header(), setcookie() or session_start()) is that all output to the browser needs to come after the calls to those functions. Even a single while space (like opening the PHP tag after a single space in the file) sends the headers, so you really need to watch out for how your code is laid out on the page.

Posted: Thu Aug 09, 2007 1:19 pm
by ectpoon
i've been trying removing white line and space and such...
but no luck..

is there a problem with php open tag inside a php open tag?
is there any other problems with my code above?

Posted: Thu Aug 09, 2007 1:21 pm
by RobertGonzalez
What editor are you using? I remember something about a particular editor or file type that would add an invisible character to the beginning of the file that would cause this to happen.

Posted: Thu Aug 09, 2007 1:24 pm
by ectpoon
i'm using UltraEdit-32

Posted: Thu Aug 09, 2007 1:33 pm
by RobertGonzalez
Try opening your file in notepad, then saving it as the file again from notepad. I doubt this will fix it, but it might at least eliminate a potential problem.

Posted: Thu Aug 09, 2007 1:41 pm
by ectpoon
just tried... and still getting this warning..

Warning: Cannot modify header information - headers already sent by (output started at /home/webgotes/public_html/script/login_sc.php:1) in /home/webgotes/public_html/script/login_sc.php on line 53

there is nothing on line 1 in login_sc.php
except the require 'db_connect.php'

Posted: Thu Aug 09, 2007 3:51 pm
by RobertGonzalez
Well, I am not sure what to tell you then. Somewhere PHP is outputting to the browser before the call to session start.