php Redirect with Header
Moderator: General Moderators
php Redirect with Header
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
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
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Everah | Please use
db_connect.php
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]
Here are the code..
login_sc.phpCode: 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;
}
?>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]- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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'
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'
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA