zDawg32
06-18-2010, 12:50 PM
Hi. My name is Zack, and I'm having some serious problems with PHP. I have a simple login script, but it does not work. It finds the user in the MySQL database just fine, but it won't set the cookie.
I'm having literally the same problem as this guy: http://www.codingforums.com/showthread.php?t=134503 . I did everything he did to get it working, but no luck.
<?php
$result = "";
if ($_POST["action"] == "login")
{
$link = mysql_connect('localhost', 'root', '*****');
if (!$link) {
die('A MySQL error occured. You could not be logged in.');
}
mysql_select_db("magellis") or die( "A MySQL error occured! You could not be logged in.");
$sql = "SELECT * FROM `users` WHERE `username` = '".$_POST["username"]."'";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
if ($row["username"] == $_POST["username"])
{
if ($row["password"] == md5($_POST['password'])) {
//valid
setcookie("magloggedin", $row["username"], 2592000);
$result = "Welcome, ".$row["username"];
}else{
$result = "Invalid Password.";
}
}
else {
$result = "Invalid Username";
}
}
mysql_close($link);
}
?>
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Magellis Networks</title>
</head>
<style type="text/css">
.minititle
{
font-size: medium;
}
a:link, a:visited
{
color: Black;
text-decoration: none;
}
a:hover, a:active
{
color: Black;
text-decoration: underline;
}
</style>
<body style="background-image: url(bggradient.png); font-family: 'Segoe UI'; font-size:small;">
<center>
<br /><br /><br />
<?php echo $result; ?>
</center>
</body>
</html>
When I use the right credentials, it gives me this:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\magellis.net\control.php:1) in C:\xampp\htdocs\magellis.net\control.php on line 16
Welcome, zDawg32
I checked my cookies, none from localhost.
Oh, and please don't tell me about how everything inside my while statement is done wrongly, I know.
EDIT: Line 16 is the setcookie
I'm having literally the same problem as this guy: http://www.codingforums.com/showthread.php?t=134503 . I did everything he did to get it working, but no luck.
<?php
$result = "";
if ($_POST["action"] == "login")
{
$link = mysql_connect('localhost', 'root', '*****');
if (!$link) {
die('A MySQL error occured. You could not be logged in.');
}
mysql_select_db("magellis") or die( "A MySQL error occured! You could not be logged in.");
$sql = "SELECT * FROM `users` WHERE `username` = '".$_POST["username"]."'";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
if ($row["username"] == $_POST["username"])
{
if ($row["password"] == md5($_POST['password'])) {
//valid
setcookie("magloggedin", $row["username"], 2592000);
$result = "Welcome, ".$row["username"];
}else{
$result = "Invalid Password.";
}
}
else {
$result = "Invalid Username";
}
}
mysql_close($link);
}
?>
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Magellis Networks</title>
</head>
<style type="text/css">
.minititle
{
font-size: medium;
}
a:link, a:visited
{
color: Black;
text-decoration: none;
}
a:hover, a:active
{
color: Black;
text-decoration: underline;
}
</style>
<body style="background-image: url(bggradient.png); font-family: 'Segoe UI'; font-size:small;">
<center>
<br /><br /><br />
<?php echo $result; ?>
</center>
</body>
</html>
When I use the right credentials, it gives me this:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\magellis.net\control.php:1) in C:\xampp\htdocs\magellis.net\control.php on line 16
Welcome, zDawg32
I checked my cookies, none from localhost.
Oh, and please don't tell me about how everything inside my while statement is done wrongly, I know.
EDIT: Line 16 is the setcookie