suttercain Posted April 18, 2007 Share Posted April 18, 2007 Hi guys, this is straight from a book I am working from: <?php session_start(); ?> <?php require ('get_connected.php'); if (isset($_POST['user']) && isset($_POST['password'])) { $sql = "SELECT * FROM users"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); if ($user == $row['user'] && $password == $row['password']) { $_SESSION['Approved'] = 1; } else { $_SESSION['Approved'] = 0; } header("Location: admin_canada.php"); } } } if (isset($_GET['logout'])) { session_destroy(); header("Location: canada_login.php"); } ?> Is this code incorrect? Becuase I am getting this error: Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\ARB\pio\get_connected.php:7) in C:\wamp\www\ARB\pio\logged_in.php on line 15 Quote Link to comment https://forums.phpfreaks.com/topic/47631-cannot-modify-header-information-headers-already-sent-by/ Share on other sites More sharing options...
MadTechie Posted April 18, 2007 Share Posted April 18, 2007 looking at the error do you want to post the code for get_connected.php then ? Quote Link to comment https://forums.phpfreaks.com/topic/47631-cannot-modify-header-information-headers-already-sent-by/#findComment-232570 Share on other sites More sharing options...
AndyB Posted April 18, 2007 Share Posted April 18, 2007 get_connected.php probably has some output to the browser. Even 'white space' counts as output. Quote Link to comment https://forums.phpfreaks.com/topic/47631-cannot-modify-header-information-headers-already-sent-by/#findComment-232571 Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 he doesn't have to post the included file. basically you've just printed something out to the browser before you called the header function. you can't do that. http://www.phpfreaks.com/forums/index.php/topic,37442.0.html the alternative would be to replace your header() with a meta-redirect: http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm Quote Link to comment https://forums.phpfreaks.com/topic/47631-cannot-modify-header-information-headers-already-sent-by/#findComment-232573 Share on other sites More sharing options...
DrDre Posted April 18, 2007 Share Posted April 18, 2007 It all depends on what your intending to do with that include. Is it a form to login? If so, you can use output buffering to make your current file work without errors, but there is a better way to do it than what your doing if that is whats in get_connected.php use this as a last resort to make it work: <?php session_start(); ob_start(); require ('get_connected.php'); $data=ob_get_contents(); ob_end_clean(); if (isset($_POST['user']) && isset($_POST['password'])) { $sql = "SELECT * FROM users"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); if ($user == $row['user'] && $password == $row['password']) { $_SESSION['Approved'] = 1; } else { $_SESSION['Approved'] = 0; } header("Location: admin_canada.php"); } } } if (isset($_GET['logout'])) { session_destroy(); header("Location: canada_login.php"); } echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47631-cannot-modify-header-information-headers-already-sent-by/#findComment-232615 Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 docta dre is right about using output buffering. but i think that uses more resources than necessary and can cause problems to arise elsewhere in the future. meta-redirect does the exact same thing as header() for what you want to do. Quote Link to comment https://forums.phpfreaks.com/topic/47631-cannot-modify-header-information-headers-already-sent-by/#findComment-232625 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.