PDA

View Full Version : authenticating a login page then directing to proper URL


JTekNet
03-28-2008, 06:10 PM
I'm creating a login page for my database and I was wondering how I can use an if statement or whatever I need to get to my next page based on the password user and password entered.


if ($_POST['uname'] === 'user' && $_POST['password'] === 'pass')
{
echo "worked";
} else {
echo "Sorry, wrong username / password";
}


Now I know that works. Problem is, I need to change the echo "worked"; to gotourl (userpage1.php); and gotourl obviously isn't a way to do it. I've tried sending headers but it always tells me they have already been sent. I've checked for whitespace and everything because I read that it can be an issue. There is no whitespace at the end or beginning so I'm completely clueless. HELP

Andrew Johnson
03-28-2008, 06:13 PM
Headers will work if you put the code BEFORE any HTML is outputted.

Len Whistler
03-28-2008, 06:17 PM
Output buffering will prevent the header already sent error. And I would use exit() right after the header() function


header code
<?php
ob_start();
?>

footer code
<?php
ob_end_flush();
?>

JTekNet
03-28-2008, 06:18 PM
Headers will work if you put the code BEFORE any HTML is outputted.

Didn't work. My new code:

<?php
if ($_POST['uname'] === 'user' && $_POST['password'] === 'pass')
{
header('http://www.google.com');
} else {
die;
}
?>

<html
<head>
<title>My page</title>
</head>

<body>
</body>
</html>

Did nothing. Just sits on a white page. I passed the uname and pass variables from a form on the previous page.

Len Whistler
03-28-2008, 06:25 PM
header('http://www.google.com');

Should be:

header ("Location: http://www.google.com");

JTekNet
03-28-2008, 06:28 PM
header('http://www.google.com');

Should be:

header ("Location: http://www.google.com");

Thanks! I decided I'd use the following. I found another post about 5 seconds before you posted. Thanks for the help though! Now I can get this crazy script off the ground. Now time to reference some places and make a table displaying prospects. This should be fun... Advice? I didn't get much help in my other thread just got pointed at a tutorial.


<?php
$url = "http://www.google.com";
if ($_POST['uname'] === 'user' && $_POST['password'] === 'pass')
{
header("location: $url");
} else {
die;
}
?>

Andrew Johnson
03-28-2008, 08:05 PM
Ya as stated you need Location, I assumed you had the header code already correct!