Sim1980
asked on
Cannot modify header information - headers already sent by in PHP
Hi all.
I'm new to php so please bare with me. I created a login page that when successful, will send the user to DataEntry.php.
But I keep getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/12/11231812/ html/check login.php: 6) in /home/content/12/11231812/ html/check login.php on line 33
What's going on? Thank you in advance.
Below is the code:
I'm new to php so please bare with me. I created a login page that when successful, will send the user to DataEntry.php.
But I keep getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/12/11231812/
What's going on? Thank you in advance.
Below is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Insert</title>
</head>
<body>
<?php $hostname="blahblah"; // Host name
$username="blah"; // Mysql username
$password="blah"; // Mysql password
$db_name="blah"; // Database name
$tbl_name="blah"; // Table name
// Connect to server and select database.
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Username and password sent from form
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
// To protect 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 Login='$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");
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:DataEntry.php");
}
else {
echo "Wrong Username or Password";
}
?>
<br>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Not sure we can help you with your text editor.
Regarding this:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
Please see HTTP Headers Must Come First, Period
Please take some time to read the articles and understand them. There is no fault in being new to PHP, but it takes time to learn the language. Nobody would expect you to write German poetry if you did not know German. Nobody expects you to write PHP client authentication scripts when you do not yet know PHP. Give yourself a break and take advantages of the learning examples and opportunities here.
You might be able to make something like this work, but you also might want to read the online man pages. MySQL is being removed from PHP. And you don't want to use session_register() (see the large red warning block).
Regarding this:
all my code between <php and ?> before the <!DOC line of codeThere may be invisible whitespace somewhere. This specific issue is discussed in the article here:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
Please see HTTP Headers Must Come First, Period
Please take some time to read the articles and understand them. There is no fault in being new to PHP, but it takes time to learn the language. Nobody would expect you to write German poetry if you did not know German. Nobody expects you to write PHP client authentication scripts when you do not yet know PHP. Give yourself a break and take advantages of the learning examples and opportunities here.
You might be able to make something like this work, but you also might want to read the online man pages. MySQL is being removed from PHP. And you don't want to use session_register() (see the large red warning block).
<?php
error_reporting(E_ALL);
$msg = "Welcome";
$hostname="blahblah"; // Host name
$username="blah"; // Mysql username
$password="blah"; // Mysql password
$db_name="blah"; // Database name
$tbl_name="blah"; // Table name
// Connect to server and select database.
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Username and password sent from form
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
// To protect 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 Login='$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");
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:DataEntry.php");
exit;
}
else {
$msg = "Wrong Username or Password";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Insert</title>
</head>
<body>
<?php echo $msg; ?>
<br>
</body>
</html>
You should be using a code editor like PSPad http://www.pspad.com/ or Notepad++ http://notepad-plus-plus.org/ . Never use a word processor or Office program to edit web pages. You will be very unhappy with the results.
+1 for what DaveBaldwin said about editors. I use TextPad and like it; it works well for my habits. I've used NotePad++ which has the advantage of "code folding." Whenever you find yourself using code folding, you can be pretty sure it's time to abstract the functionality into a separate class method. I've used BBEdit and found it OK, but not as well suited to my work habits.
Avoid Dreamweaver. If you rely on Dreamweaver you are likely to find that it generates some of the worst PHP code ever written. Unfortunately, you will not find this until your code begins to fail in run-time and you're forced to refactor code that you really did not understand in the first place. That's a terrible place to be, so just don't go there in the first place! Write your own code and understand every line, one line at a time. It might seem like a lot of work at first, but it's the best approach and it will build a knowledge base for you.
Avoid Dreamweaver. If you rely on Dreamweaver you are likely to find that it generates some of the worst PHP code ever written. Unfortunately, you will not find this until your code begins to fail in run-time and you're forced to refactor code that you really did not understand in the first place. That's a terrible place to be, so just don't go there in the first place! Write your own code and understand every line, one line at a time. It might seem like a lot of work at first, but it's the best approach and it will build a knowledge base for you.
I also use Text Wrangler, the baby brother of BBedit, on Mac and Bluefish on both Mac and Linux. My primary editor on Windows is HTML-Kit which I've been using for at least 15 years now.
ASKER
Thank you for all help and suggestions!
I'm doing some PHP reading and hands on work with my small project, a combination of the 2, trial and error.
I don't have the budget to hire a professional/freelancer, so I'm taking this head as kind of a pet project.
I was using Komposer, and that was causing my code to rearrange itself every time I uploaded to my website, so I switched to Coda 2 and it all works fine now!
Thanks again. I will be posting other basic questions so I look forward to your suggestions in the future!
I'm doing some PHP reading and hands on work with my small project, a combination of the 2, trial and error.
I don't have the budget to hire a professional/freelancer, so I'm taking this head as kind of a pet project.
I was using Komposer, and that was causing my code to rearrange itself every time I uploaded to my website, so I switched to Coda 2 and it all works fine now!
Thanks again. I will be posting other basic questions so I look forward to your suggestions in the future!
Thanks for the points. Ask lots of questions -- we will be here to help, ~Ray
ASKER
I placed all my code between <php and ?> before the <!DOC line of code and it simply disappeared from the editor and when I went to test it on the website, the website wouldn't do anything.
Also, where would I put session_start() in the current page if I have to put everything before the <!DOC line of code? I also added session_start() to DataEntry.php page in the very first line and it also disappeared when I open the editor.
My web hosting plan is set to PHP version 5.3