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
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"?

Link to comment
Share on other sites

<?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);
}
?>

Link to comment
Share on other sites

<?php

if($loggedIn) {
echo "Logged in!";
$loc = $_SESSION["User"];
header("Location: http://www.doman.tld/$loc");
exit;
} else {
echo "Not logged in!";
}

?>

 

Run that a second to check the if is working.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)";
		}
	}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.