php Redirect with Header

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ectpoon
Forum Newbie
Posts: 6
Joined: Thu Aug 09, 2007 10:46 am

php Redirect with Header

Post 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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

Not without you posting the code..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ectpoon
Forum Newbie
Posts: 6
Joined: Thu Aug 09, 2007 10:46 am

Post 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]
ectpoon
Forum Newbie
Posts: 6
Joined: Thu Aug 09, 2007 10:46 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ectpoon
Forum Newbie
Posts: 6
Joined: Thu Aug 09, 2007 10:46 am

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ectpoon
Forum Newbie
Posts: 6
Joined: Thu Aug 09, 2007 10:46 am

Post by ectpoon »

i'm using UltraEdit-32
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ectpoon
Forum Newbie
Posts: 6
Joined: Thu Aug 09, 2007 10:46 am

Post 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'
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply