Jump to content

Recommended Posts

I have a script where a user can register and then login to my site. It all works fine on WAMP. When I put it on a live server I am having issues. I am getting this error when you try to login. 

 

Warning: Cannot modify header information - headers already sent by (output started at /home/wetdogno/public_html/login_scripts/login.php:12) in /home/wetdogno/public_html/login_scripts/login.phpon line 75

 

I know sessions are not starting becuase normally when you register, it automatically logs you into the account you just created, but its not doing that. It creates the user, because their information gets injected into my DB. Their session just isn't starting. Also the header() function is not working. Here is the code that the error message describes. I really don't think its a white space issue like many of the blogs that I have read indicated. 

f ($password == $dbpass) {
		if ($dbactive == 1) {
			$_SESSION['userid'] = $dbid;
			$_SESSION['username'] = $dbuser;
                        header('Location:/login_scripts/member.php');
			}


the code you posted is where the header() statement is at. that's just the affect of the error. the cause of the problem is the OUTPUT you are sending in login.php on line 12 - (output started at /home/wetdogno/public_html/login_scripts/login.php:12)

the code you posted is where the header() statement is at. that's just the affect of the error. the cause of the problem is the OUTPUT you are sending in login.php on line 12 - (output started at /home/wetdogno/public_html/login_scripts/login.php:12)

Here is all the code. I put line 12 in red so you can see it easier. Thank you

<html>	
<head>
<link rel="stylesheet" type="text/css" href="/style.css" />
<link rel="stylesheet" type="text/css" href="/login_scripts/user_style.css" />
<title>Wet Dog Nose Polls</title>
</head>
	
<body>
<div id="wrapper">
        <div id="headerwrap">
        <div id="header">
            <?php
            include $_SERVER['DOCUMENT_ROOT'] . '/header_source/index.php';
            ?>
            <a href="/index.php">
            <img src="/images/wetdognose.jpg">
            </a>
        </div>
        </div>
        <div id="navigationwrap">
        <div id="navigation">
            <?php
            include $_SERVER['DOCUMENT_ROOT'] . '/menu_source/index.php';
            ?>
        </div>
        </div>
        <div id="loginwrap">
        <div id="login">
            <h2>Welcome back! Sign in here.</h2>
	<?php
		include $_SERVER['DOCUMENT_ROOT'] . '/login_scripts/sessionstart.php';
		if (isset($_POST['loginbtn']) or ($username && $userid)) {} else{echo "";}
		if ($username && $userid) {
			echo "You are already logged in as <b>$username</b>";
			echo "<br/>Not " . $username . "? <a href='/login_scripts/logout.php'>Logout</a>";
		} else {
			$form = "<div id='forms'>
						<form action='/login_scripts/login.php' method='post'>
							<table class='forms'>
								<td><input type='text' name='user' placeholder='Username' class='input_style'/></td>
							</tr>
							<tr>
								<td><input type='password' name='password' placeholder='Password' class='input_style'/></td>	
							</tr>
								<td><input type='submit' name='loginbtn' id='loginbtn' value='Login'/></td>
							</tr>
							<tr>
								<td><a href='/login_scripts/register.php'>Register</a>
								<a href='/login_scripts/forgotpass.php'>Forgot Password?</a></td>
							</tr>
						</table>
					</form>
			</div>";	
		}
		if ($_POST['loginbtn']) {
			$user = $_POST['user'];
			$password = $_POST['password'];
				if ($user) {
					if ($password) {
						include $_SERVER['DOCUMENT_ROOT'] . '/login_scripts/connect.php';
						$password = md5(md5("R4E2M0".$password."R4E2M0"));
                                                $link = mysqli_connect("localhost","wetdogno_ryan","Relztem$3","wetdogno_login") or die ("Cannot connect");
						$query = mysqli_query($link, "SELECT * FROM user WHERE username='$user'") or die (mysqli_error());
						$numrows = mysqli_num_rows($query);
							if ($numrows ==1) {
								$row = mysqli_fetch_assoc($query);
								$dbid= $row['id'];
								$dbuser= $row['username'];
								$dbpass = $row['password'];
								$dbactive = $row['active'];
									if ($password == $dbpass) {
										if ($dbactive == 1) {
											$_SESSION['userid'] = $dbid;
											$_SESSION['username'] = $dbuser;
                                                                                        header('Location:/login_scripts/member.php');
								} else {
									echo "<font color='red'>You must activate your account to login. $form</font>";
								}
						} else {
							echo "<font color='red'>You did not enter the correct password. $form</font>";
						}
					} else {
						echo "<font color='red'>The username you entered was not found. Be sure that you are entering your username and not email.$form</font>";
					}
				} else {
					echo "<font color='red'>You must enter your password . $form</font>";
				}
			} else {
				echo "<font color='red'><p>You must enter your username. Be sure you are entering your username and not your email. $form</font>";
			}
	} else{
		echo $form;
	}
?>
        </div>
        </div>
        <div id="rightcolumnwrap">
        <div id="rightcolumn">
            <?php
            include $_SERVER['DOCUMENT_ROOT'] . '/ad.php';
            ?> 
        </div>
        </div>
        <div id="footerwrap">
        <div id="footer">
            <?php
            include $_SERVER['DOCUMENT_ROOT'] . '/footerlinks.php';
            ?>
        </div>
        </div>
    </div>	

</body>

</html>

Your going to have to post more code than that. Show the code for the whole page.

 

 

the code you posted is where the header() statement is at. that's just the affect of the error. the cause of the problem is the OUTPUT you are sending in login.php on line 12 - (output started at /home/wetdogno/public_html/login_scripts/login.php:12)

 

I have found that the error is always at the line where the first php tag occurs. Right now it is reading an error at line 12 which is the first php tag in the code. If I delete that section (because it does not contribute to the functionality of the code) , the errror reads line 19 which is the next php tag. If I delete that, the error jumps to the next php tag. It doesnt like the first php tag for some reason. 

this is a very common error. if you search the web for it, you will get several million results that tell you what causes it and how to fix what is causing it.

 

you cannot send any (1 or more) character to the browser before you use a header(), session_start(), of setcookie() statement. ALL THE HTML MARKUP you have before the header() statement are characters and cannot be sent to the browser.

 

the way to fix this is to refactor your code and move the majority of your php code to the top of your file and put ALL the html document, starting with the <!DOCTYPE tag near the end of your file. the only php code that should be inside the html document as basic php statements that are concerned with displaying the dynamic portion of the html document. logging a user is has nothing to do with the html document. 

 

if you read the following post for a recommend page layout to follow, your code won't have this problem, because processing post method form data will be near the top of your file and the html document/template will be at the end - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095

You don't seem to understand that HTML markup is in fact output. So you have output all over the place, starting at line 1. The fact that PHP reports the first tag after the markup is just an implementation detail.

 

Long story short: Your code is definitely broken. While it's theoretically possible to circumvent the issue with output buffering (which is what your test server does), I strongly recommend against that.

 

Get rid of this awful spaghetti code and write your scripts properly, that is, separate the logic from the visuals. The best way to enforce clean separation is to use an actual template language like Twig. Or, if you insist on using PHP as a poor man's template engine, put the logic on top and keep the markup at the bottom.

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.