Login Issues [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Login Issues [Resolved]

Post by Epiales »

Okay few files uploaded to the new server. I am getting this error when I try and login...

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/gameplay/public_html/login.php:123) in /home/gameplay/public_html/login.php on line 173
When you enter username and pass, it should take you to the index.php, but it doesn't. It keeps it on the login page, but with that error above. It works fine on my localhost, but not working online grrr :evil:

This is my login information:

FORM:

Code: Select all

<?php
<!-- Login Starts Here -->
            <div id="loginContainer">
                <a href="login.php" id="loginButton"><span>Login</span><em></em></a>
                <div style="clear:both"></div>
                <div id="loginBox">                
                    <form id="loginForm" method="post">
                        <fieldset id="body">
                            <fieldset>
                                <label for="email">Username</label>
                                <input type="text" name="username" id="username" />
                            </fieldset>
                            <fieldset>
                                <label for="password">Password</label>
                                <input type="password" name="password" id="password" />
                            </fieldset>
                            <input type="submit" name="login" id="login" value="Sign in" />
                            <label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
                        </fieldset>
                        <span><a href="#">Forgot your password?</a></span>
                    </form>

            </div>
            <!-- Login Ends Here -->
 

Code: Select all

<?php
if(isset($_POST['login'])){
    if(isset($_SESSION['uid'])){
        echo "You are already logged in!";
    }else{
        $username = protect($_POST['username']);
        $password = protect($_POST['password']);
        
        $login_check = mysql_query("SELECT `id` FROM `user` WHERE `username`='$username' AND `password`='".md5($password)."'") or die(mysql_error());
        if(mysql_num_rows($login_check) == 0){
            echo "Invalid Username/Password Combination!";
        }else{
            $get_id = mysql_fetch_assoc($login_check);
            $_SESSION['uid'] = $get_id['id'];
            header("Location: index.php");

 
Last edited by Epiales on Thu Sep 26, 2013 6:55 am, edited 5 times in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Login Issues

Post by Winawer »

The error message says you're generating output on line 123. What's around line 123?
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Login Issues

Post by Epiales »

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/gameplay/public_html/login.php:28) in /home/gameplay/public_html/login.php on line 42
Starting with line 28 is the <?php:

Code: Select all

<?php
if(isset($_POST['login'])){
    if(isset($_SESSION['uid'])){
        echo "You are already logged in!";
    }else{
        $username = protect($_POST['username']);
        $password = protect($_POST['password']);
        
        $login_check = mysql_query("SELECT `id` FROM `user` WHERE `username`='$username' AND `password`='".md5($password)."'") or die(mysql_error());
        if(mysql_num_rows($login_check) == 0){
            echo "Invalid Username/Password Combination!";
        }else{
            $get_id = mysql_fetch_assoc($login_check);
            $_SESSION['uid'] = $get_id['id'];
            header("Location: index.php");

Line 42 is the header info:

Code: Select all

            header("Location: index.php");
And the include header and include safe do not have a redirect to index or anywhere, so I don't see where the problem is. I still just gives the above error and leaves you sitting on login.php when it's supposed to take you to index.php :evil: :evil:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Login Issues

Post by Winawer »

What's before the <?php ?
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Login Issues

Post by Epiales »

Winawer wrote:What's before the <?php ?
Not to worry! I've gone another route! Thanks for helping :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Login Issues [Resolved]

Post by Epiales »

Naw, I guess I still need to fix this:

Here's what I have total on the login:

Code: Select all


<?php
session_start();
include("functions.php");
connect();



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js?ver=1.4.2"></script>
<script src="js/login.js"></script>
<script src="js/register.js"></script>

<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/bootstrap-theme.min.css" />
    <title></title>

</head>
<body>




<?php
if(isset($_POST['login'])){
    if(isset($_SESSION['uid'])){
        echo "You are already logged in!";
    }else{
        $username = protect($_POST['username']);
        $password = protect($_POST['password']);
        
        $login_check = mysql_query("SELECT `id` FROM `user` WHERE `username`='$username' AND `password`='".md5($password)."'") or die(mysql_error());
        if(mysql_num_rows($login_check) == 0){
            echo "Invalid Username/Password Combination!";
        }else{
            $get_id = mysql_fetch_assoc($login_check);
            $_SESSION['uid'] = $get_id['id'];
            header("Location: index.php");
        }
}


}
?>
    <div style="margin-left:50px;">
        <ul id="menu">
            <li class="logo">
                <img style="float:left;" alt="" src="images/menu_left.png"/>
               
                <ul id="help">
            
                </ul>
            </li>    
          
            <li>

        <div id="container">
            <!-- Login Starts Here -->
            <div id="loginContainer">
                <a href="login.php" id="loginButton"><span>Login</span><em></em></a>
                <div style="clear:both"></div>
                <div id="loginBox">                
                    <form id="loginForm" method="post">
                        <fieldset id="body">
                            <fieldset>
                                <label for="email">Username</label>
                                <input type="text" name="username" id="username" />
                            </fieldset>
                            <fieldset>
                                <label for="password">Password</label>
                                <input type="password" name="password" id="password" />
                            </fieldset>
                            <input type="submit" name="login" id="login" value="Sign in" />
                            <label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
                        </fieldset>
                        <span><a href="#">Forgot your password?</a></span>
                    </form>

            </div>
            <!-- Login Ends Here -->
   
            <!-- Register Starts Here -->
               
          </li>    
          
<li>

        <div id="container">
            <!-- Login Starts Here -->
            <div id="registerContainer">
                <a href="login.php" id="registerButton"><span>Register</span><em></em></a>
                <div style="clear:both"></div>
                <div id="registerBox">                
                    <form id="registerForm" method="post">
                        <fieldset id="body">
                            <fieldset>
                                <label for="username">Username</label>
                                <input type="text" name="username" id="username" />
                            </fieldset>
                            <fieldset>
                                <label for="email">Email</label>
                                <input type="text" name="email" id="email" />
                            </fieldset>
                            <fieldset>
                                <label for="password">Password</label>
                                <input type="password" name="password" id="password" />
                            </fieldset>
                            <input type="submit" name="register" id="register" value="Register" />
                        </fieldset>
                    </form>

            </div>
            <!-- Login Ends Here -->
   
            <!-- Register Starts Here -->
               
          </li>                

            <!-- Register Ends Here -->        

          
        </ul>
    </div>
    <div style="float:none; clear:both;"></div>

    <?PHP
    

$ip = $_SERVER['REMOTE_ADDR'];
$long = ip2long($ip);

if(isset($_POST['register'])){
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);
    $email = protect($_POST['email']);
    if($username == "" || $password == "" || $email == ""){
        echo "Please supply all fields!";
    }elseif(strlen($username) > 20){
        echo "Username must be less than 20 characters!";
    }elseif(strlen($email) > 100){
        echo "E-mail must be less than 100 characters!";
    }else{
        $register1 = mysql_query("SELECT `id` FROM `user` WHERE `username`='$username'") or die(mysql_error());
        $register2 = mysql_query("SELECT `id` FROM `user` WHERE `email`='$email'") or die(mysql_error());
        if(mysql_num_rows($register1) > 0){
            echo "<big>That username is already in use!</big>";
        }elseif(mysql_num_rows($register2) > 0){
            echo "<big>That e-mail address is already in use!</big>";
        }else{
            $ins1 = mysql_query("INSERT INTO `stats` (`id`,`gold`,`attack`,`defense`,`food`,`income`,`farming`,`turns`,`maxturns`,`bank`,`level`,`exper`,`hpoints`,`maxhp`,`location`) VALUES (0,500,8,8,500,10,10,500,500,0,1,0,15,15,'Las Vegas')") or die(mysql_error());
            $ins2 = mysql_query("INSERT INTO `unit` (`worker`,`farmer`,`henchmen`,`guards`) VALUES (5,5,5,5)") or die(mysql_error());
            $ins3 = mysql_query("INSERT INTO `user` (`username`,`password`,`email`, `ip`) VALUES ('$username','".md5($password)."','$email', '$ip')") or die(mysql_error());
            $ins4 = mysql_query("INSERT INTO `weapon` (`gun`,`vest`) VALUES (0,0)") or die(mysql_error());
            $ins5 = mysql_query("INSERT INTO `ranking` (`attack`,`defense`,`overall`) VALUES(0,0,0)") or die(mysql_error());
            $ins6 = mysql_query("INSERT INTO `business` (`fruit`,`tire`,`parts`,`chop`) VALUES(10,0,0,0)") or die(mysql_error());
            $ins7 = mysql_query("INSERT INTO `farms` (`grain`,`wheat`,`hops`,`corn`) VALUES(10,0,0,0)") or die(mysql_error());
            echo "<big><br><br><br>You have registered!</big>";
        }
    }
}
?>


</div>
</body>
</html>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Login Issues

Post by MikuzA »

You can't write stuff before using header("Location: index.php").
So your <html> etc messes it up.

put the if before the html-stuff.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Login Issues

Post by Winawer »

The headers get sent first when you send any content (they are headers, after all). That means that you must manipulate headers before anything gets sent to the user. You basically have two options:
1) Do what MikuzA said, move the if before the HTML.
2) Add an ob_start() to the beginning of the page.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Login Issues

Post by Epiales »

MikuzA wrote:You can't write stuff before using header("Location: index.php").
So your <html> etc messes it up.

put the if before the html-stuff.
Ah...gotcha! Thank you :oops: :oops:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Login Issues

Post by Epiales »

Winawer wrote:The headers get sent first when you send any content (they are headers, after all). That means that you must manipulate headers before anything gets sent to the user. You basically have two options:
1) Do what MikuzA said, move the if before the HTML.
2) Add an ob_start() to the beginning of the page.
Okay, so now I upload to the server. I still get this:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/gameplay/public_html/includes/functions.php:49) in /home/gameplay/public_html/login.php on line 23
Every single time I try to put it online, I get this stupid thing. I have the 'if's ' before the stupid html. So now what's wrong? :evil: :evil: :evil:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”