Jump to content

[SOLVED] Redirect


Ashoar

Recommended Posts

Ok, please do not link me to the sticky about this, i have already read it and tried what was said and nothing worked.

 

This login form worked perfectly until i started running it on php 5.2.9.

 

When i login i get this error message:

Warning: Cannot modify header information - headers already sent by (output started at /home/asilentd/public_html/forum/login.php:5) in /home/asilentd/public_html/forum/login.php on line 33

 

As said, i tried what the sticky said about these header errors but nothing has worked, and it has only started doing this since i upgraded to php 5.2.9.

 

I moved the session start right up the top of the page as that was causing another error, and it fixed after moving it there.

 

Here is the code:

<?php 
session_start(); 
?>
<div id="wrapper">
<?php
include "header.php";
include 'config.php';

if(isset($_POST['login']))
{

$username = trim(addslashes($_POST['username']));
$password = md5(trim($_POST['password']));

$query = mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password' LIMIT 1") or die(mysql_error());

$row = mysql_fetch_array($query);

// now we check if they are activated

if(mysql_num_rows($query) > 0)
{

if($row['Activated'] > 0)
{

$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name'];	

$insertuser="INSERT INTO online(username) values('$username')";
mysql_query($insertuser) or die("Could not login insert user");
header("Location: index.php");
} 
else 
{

echo '
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">

</head>

<body>
<div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p>
<p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div>
</body>
</html>
';

}

} else {

echo '
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p>
<p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p>

</div>
</body>
</html>
';

}

} else {

?>

<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="wrapper">

<table class='maintable'>
<tr class='headline'><td width=100%>Login</td></tr>
  
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
  <tr class='mainrow'><td><p>Username:<br>
  <input name="username" type="text" class="textBox" id="username">
  
  <tr class='mainrow'><td><p>Password:<br>
    <input name="password" type="password" class="textBox" id="password">
</p>
  <p>
    <input name="login" type="submit" class="textBox" id="login" value="Submit">
  </p>
  </form>
  <p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p>
  <p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p>
</div>
</table>
</div>

</body>
</html>

<? } mysql_close($l); ?>

<?php
include "footer.php";
?>

Link to comment
Share on other sites

do you understand how headers work and how php handles them?

headers are sent at the top of a HTTP response.

so you cannot send headers after any output has been sent because they had to come first

now if you read the error output is started on line 5 "<div id="wrapper">"

looking at your code i don't quite see how this line of code works with the rest of the script

you need. anyway to i would recommend moving the login checker part of the script to the top on the lines after session_start()

that way you would have no problems

 

Scott.

Link to comment
Share on other sites

The wrapper is just adding everything of the page inside of a centered alignment with a certain width.

 

Well i do rat, as this entire code worked perfect until i upgraded to a higher version of php which is why it has confused me since it did work fine before.

 

Anyway i have it working now, it seems to work just by adding the session start to the header.php page that is included at the top of each page.

Link to comment
Share on other sites

get rid of the <div id="wrapper"> on the top of the page.

 

As i noticed u have another <div id="wrapper"> in the HTML little down in the script. I really don't understand the need of the 1st div.

 

And if this page is wrapped in another one, then put that div in the wrapping page.

Link to comment
Share on other sites

not changed then it should fail in older versions of php.

 

@ratcateme has alluded to the problems your are encountering but without refactoring your code the simplest solution is to use the ob_start() / ob_end_flush() solution I suggested above.

 

alternatively you can go through all your code - ensure all your datachecks/validation redirects etc are done BEFORE you start the output - as soon as you start outputting markup (including whitespace) all headers must have been sent.

Link to comment
Share on other sites

The main problem was as Rat said.

When i had originally swapped them around they were not right with the session start info.

I have gone through now and done that, also with this version of php it did require session start to be at the very top of any page.

 

It is all fixed now.

Thanks.

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.