...

View Full Version : Resolved Cookies



zDawg32
06-18-2010, 01: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

Rowsdower!
06-18-2010, 02:20 PM
From what I see you aren't outputting anything before the cookie header is sent. Any chance it's the same problem as this:

http://www.phpbuilder.com/board/archive/index.php/t-10330991.html

Check for white space before the <?php tag. If not that, then I don't think you need ANSI encoding, but you might need UTF-8 without BOM or something. I can't think of anything else that should create that problem.

zDawg32
06-18-2010, 02:51 PM
Well, ANSI made the error go away, but now it doesn't set the cookie. - scratch that. It was because I used _GET instead of _POST.
This is my PHP so far:

<?php
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);
$user = false;
while ($row = mysql_fetch_array($query)) {
$user = true;
if ($row['password'] == md5($_POST['password'])) {
//valid
setcookie('magloggedin', $_POST['username']);
$result = "Welcome, ".$_POST["username"];
}else{
$result = "Invalid PASSWORD";
}
}
if ($user==false) {
$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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum