Jump to content

Simple login script error help please.


hodgey87

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.