Jump to content

[SOLVED] header function not sending


soycharliente

Recommended Posts

I've always used Javascript to do a redirect, but I wanted to try this way and it isn't working. Any help?

 

 

 

<?php
if(isset($_POST["submit_login"])) {
$u = $_POST["username"];
$p = $_POST["password"];
connect();
$query = "SELECT * FROM users WHERE username='$u' AND password=MD5('$p')";
$result = mysql_query($query) OR DIE ("ERROR: login - " . mysql_error());
close();
if($result) {
	while($r = mysql_fetch_array($result)){
		$user = $r["username"];
		$pass = $r["password"];
		if($u == $user && $p == $pass) {
			$_SESSION["User"] = $u;
			$_SESSION["Role"] = $r["role"];
			$loggedIn = TRUE;
			$loginError = FALSE;
		}
	}
} else {
	$loginError = TRUE;
}
}

if($loggedIn) {
$loc = $_SESSION["User"];
header("Location: http://www.doman.tld/$loc");
exit;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/51073-solved-header-function-not-sending/
Share on other sites

That code is the very first thing in the file.

 

Nothing happens. When I use an invalid username or password, it displays error text. So I know it's a valid login.

 

link=topic=140345.msg597032#msg597032 date=1178988733]

is this written before any outputs? the header() function wont work if you have allready had some outputs

What do you mean "outputs"?

<?php
function connect() {
//Connect To Database
$hostname = "asdf";
$username = "asdf";
$password = "asdf";
$dbname = "asdf";
mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect! Please try again.");
mysql_select_db($dbname);
}
?>

<?php
if(isset($_POST["submit_login"])) {
$u = $_POST["username"];
$p = $_POST["password"];
connect();
$query = "SELECT * FROM users WHERE username='$u' AND password=MD5('$p')";
$result = mysql_query($query) OR DIE ("ERROR: login - " . mysql_error());
close();
if($result) {
	while($r = mysql_fetch_array($result)){
		$user = $r["username"];
		$pass = $r["password"];
		if($u == $user && $p == $pass) {
			$_SESSION["User"] = $u;
			$_SESSION["Role"] = $r["role"];
			$loggedIn = true;
			$loginError = false;
			echo "Logged in (should be set to true)";
		}
	}
} else {
	$loginError = TRUE;
}
}

if($loggedIn) {
$loc = $_SESSION["User"];
header("Location: http://www.doman.tld/$loc");
exit;
}
?>

 

Try that.

I figured it out.

 

The problem was my second check inside the while loop. I called MD5 using SQL and it was using the POST password, which was not encrypted. Therefore, it was making it inside the while loop and not getting any errors, but failing the if statement and not setting $loggedIn to TRUE.

 

When I was first learning PHP, a friend helped me write some basic login code and I've used that exact "template" ever since. Is this a best-practice way or is this overkill/bad?

I guess I should have been more specific.

 

I was more talking about the second comparison inside the while loop. Is that really needed? I just have never questioned it because it did what I wanted it to do.

 

<?php
$u = $_POST["username"];
$p = $_POST["password"];
connect();
$query = "SELECT * FROM users WHERE username='$u' AND password=MD5('$p')";
$result = mysql_query($query) OR DIE ("ERROR: login - " . mysql_error());
close();
if($result) {
	while($r = mysql_fetch_array($result)){
		$user = $r["username"];
		$pass = $r["password"];
		if($u == $user && $p == $pass) {
			$_SESSION["User"] = $u;
			$_SESSION["Role"] = $r["role"];
			$loggedIn = true;
			$loginError = false;
			echo "Logged in (should be set to true)";
		}
	}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.