SOLVED: header() function in PHP not working...

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Hopefully someone can help out with this. I've tried most of the solutions posted throughout the internet, but to no avail.

Code:
    <?php
        $host="localhost"; // Host name
        $username="root"; // Mysql username
        $password=""; // Mysql password
        $db_name="funktrek"; // Database name
        $tbl_name="users"; // Table name
        // Connect to server and select databse.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name");
        // username and password sent from form
        $myusername=$_POST['username'];
        $mypassword=$_POST['password'];
        // To protect MySQL injection (more detail about MySQL injection)
        $myusername = stripslashes($myusername);
        $mypassword = stripslashes($mypassword);
        $myusername = mysql_real_escape_string($myusername);
        $mypassword = mysql_real_escape_string($mypassword);
        $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
        $result=mysql_query($sql);
        // Mysql_num_row is counting table row
        $count=mysql_num_rows($result);
        // If result matched $myusername and $mypassword, table row must be 1 row
        if($count==1)
        {
            // Register $myusername, $mypassword and redirect to file "login_success.php"
            //session_register("myusername");
            //session_register("mypassword");
            header("location:login_success.php");
        }
        else { echo '<div align="center">Wrong Username or Password</div><br><div style="font-size: 10px;" align="center"><a href="login.php">Retry</a></div>'; }
    ?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Funk Trek</title>
</head>
<body>
</body>
</html>
Error message returned:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\funktrek\logincheck.php:1) in C:\wamp\www\funktrek\logincheck.php on line 28
I got rid of all the whitespace in it, because apparently that causes the scripts to break that call header(). No idea how to fix this though...any help appreciated, thanks!
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
In "got rid of all the whitespace in it", did you include the spaces before "<?php"?
 

Ka0t1x

Golden Member
Jan 23, 2004
1,724
0
71
header(); cannot be used if you have.. ANYTHING sent to the browser... this means included files/functions, etc.

Whitespace (line breaks between functions,vars,etc) between PHP tags should be fine, as it is just 'code'.


It looks like Ken has it.. if you have a blank line before your php tag, it should kill it.
C: \wamp\www\funktrek\logincheck.php:1
- that file, line #1.
 
Last edited: