oliverj777 Posted November 13, 2011 Share Posted November 13, 2011 Hello, I have a simple login script that basically, when if you go to a page and you are NOT logged in (saved in cookie) then you get redirected to the login page, else the content is shown. It works, but I currently have my database connection details on the same page, I like to have them on a separate PHP and use require 'connet.php'. So I've gone ahead and done that, but now I get an error saying "Warning: Cannot modify header information - headers already sent". This only happens if I'm NOT logged in. It has something to do with my require "connect.php"; because if I comment that out, it works fine. Here is the script: <?php //require "connect.php"; // < causes header error // Connects to your Database mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); //checks cookies to make sure they are logged in if(isset($_COOKIE['ID_my_site'])) { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )){ //if the cookie has the wrong password, they are taken to the login page if ($pass != $info['password']){ header("Location: index.php"); } //otherwise they are shown the admin area else{ ?> - content - <a href=logout.php>Logout</a> <? } } } //if the cookie does not exist, they are taken to the login screen else{ header("Location: index.php"); } ?> My connect.php page is just: <? // Connects to your Database mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); ?> Why is the require causing this error, and how do I fix it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/251058-modify-header-error-caused-by-require/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 13, 2011 Share Posted November 13, 2011 The error message, that you didn't post all of, tells you where the output is occurring at. You would need to read the error message and find and eliminate that output. This sticky post might help - http://www.phpfreaks.com/forums/index.php?topic=37442.0 Quote Link to comment https://forums.phpfreaks.com/topic/251058-modify-header-error-caused-by-require/#findComment-1287826 Share on other sites More sharing options...
jotorres1 Posted November 13, 2011 Share Posted November 13, 2011 Try to add exit after every header. This avoids for the rest of the code to execute after header. <?php header("Location: index.php"); exit; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/251058-modify-header-error-caused-by-require/#findComment-1287829 Share on other sites More sharing options...
oliverj777 Posted November 13, 2011 Author Share Posted November 13, 2011 I figured it out. It was to do with white spacing ... that was all! I had to make sure that there was no white spacing before or after the PHP opening/closing tags. Thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/251058-modify-header-error-caused-by-require/#findComment-1287836 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.