hodgey87 Posted April 5, 2011 Share Posted April 5, 2011 Hi Everyone, Just wondered if someone could quickly help me out, im building a simple login system for my website but having a little bit of trouble, the error i keep getting is: Cannot modify header information - headers already sent by (output started at /home/sites/cuju8.com/public_html/include.php:18) in /home/sites/cuju8.com/public_html/login.php on line 12 I have done some research but cant find the answer to this, my login script is as follows: <?php require_once('include.php'); $error = ''; $form = $_POST['submit']; $email = $_POST['email']; $password = $_POST['password']; if( isset($form) ) { if( isset($email) && isset($password) && $email !== '' && $password !== '' ) { $sql = mysql_query("SELECT * FROM `usersystem` WHERE email='$email' and password='$password';"); if( mysql_num_rows($sql) != 0 ) { //success $_SESSION['logged-in'] = true; [b]header('Location: members.php');[/b] exit; } else { $error = "Incorrect login info"; } } else { $error = 'All information is not filled out correctly';} } ?> I think its the header location code thats causing the problem but im not sure where to move it too. If anyone could help i would really appreciate it. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/ Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 You receive this error when you try to set headers after any output has been sent back to the browser; this could be a line of text or a single white-space character. Check that before the opening <?php tag (including any files that have been included) there isn't any output. Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197114 Share on other sites More sharing options...
jmcc Posted April 5, 2011 Share Posted April 5, 2011 Hi It's kinds cheating to do this but if you already have the header set use this javascript echo's by php to redirect instead of: header('location:member.php') use: function js_redirect($url, $seconds=1) { echo "<script language=\"JavaScript\">\n"; echo "<!-- hide code from displaying on browsers with JS turned off\n\n"; echo "function redirect() {\n"; echo "window.location = \"" . $url . "\";\n"; echo "}\n\n"; echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n"; echo "-->\n"; echo "</script>\n"; return true; } js_redirect("member.php",1); Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197117 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 Hi It's kinds cheating to do this but if you already have the header set use this javascript echo's by php to redirect instead of: header('location:member.php') use: function js_redirect($url, $seconds=1) { echo "<script language=\"JavaScript\">\n"; echo "<!-- hide code from displaying on browsers with JS turned off\n\n"; echo "function redirect() {\n"; echo "window.location = \"" . $url . "\";\n"; echo "}\n\n"; echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n"; echo "-->\n"; echo "</script>\n"; return true; } js_redirect("member.php",1); Ignore this advice. Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197123 Share on other sites More sharing options...
jmcc Posted April 5, 2011 Share Posted April 5, 2011 provide solution Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197124 Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2011 Share Posted April 5, 2011 In programming, it's always better to find and fix what is causing an error, than to throw a bunch of code at it and hope the 'fix' works at all or works in all situations. The solution to this problem is to read the error message and find and fix what is causing the output that is preventing the header from working - output started at /home/sites/cuju8.com/public_html/include.php:18 (line 18) hodgey87, what is the code in your include.php file? I'll guess line 18 is either that last line in the file and you have a blank line after the ?> tag or you are echoing something on line 18 in your code. Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197126 Share on other sites More sharing options...
hodgey87 Posted April 5, 2011 Author Share Posted April 5, 2011 Hi It's kinds cheating to do this but if you already have the header set use this javascript echo's by php to redirect instead of: header('location:member.php') use: function js_redirect($url, $seconds=1) { echo "<script language=\"JavaScript\">\n"; echo "<!-- hide code from displaying on browsers with JS turned off\n\n"; echo "function redirect() {\n"; echo "window.location = \"" . $url . "\";\n"; echo "}\n\n"; echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n"; echo "-->\n"; echo "</script>\n"; return true; } js_redirect("member.php",1); Cheers that worked for me ill just stick with that Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197127 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 Cheers that worked for me ill just stick with that I urge you not to. This code is an ugly, JavaScript-reliant hack that breaks every principle of modern web development. As PFMaBiSmAd said, it's better to spend time trying to fix the cause of the error, than to fudge it and move on. Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197133 Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2011 Share Posted April 5, 2011 One other reason you would want to simply fix this problem, would be that you apparently intend to include/require the include.php file on all your pages and it will currently cause an error with every session_start(), header(), and setcookie() statement that you put in your code after it. Being able to use these three statements would be kind of important since you are working on a login script. Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197140 Share on other sites More sharing options...
jmcc Posted April 5, 2011 Share Posted April 5, 2011 yes listen to these guys Quote Link to comment https://forums.phpfreaks.com/topic/232737-simple-login-script-error-help-please/#findComment-1197155 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.