Page 1 of 1

go to other page if password is correct

Posted: Sat Nov 22, 2008 11:08 am
by ma5ect
Hi all,

I have the following code..the username and password works fine but i want the script to go to another webpage if the username and password is enetered correctly.

how do i code the if statement??

Code: Select all

<?php
 
 
$username = "someuser";
$password = "somepassword";
 
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
 
?>
 
<h1>Login</h1>
 
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>
 
    <p><label for="txtpassword">Password:</label>
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>
 
    <p><input type="submit" name="Submit" value="Login" /></p>
 
</form>
 
<?php
 
}
else { 
 
 
?>
 
<p>This is the protected page. Your private content goes here.</p>
 
<?php
 
}
 
?>

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 12:07 pm
by califdon
The simplest way is to just put the "other" page HTML where you now have "<p>This is the protected page. Your private content goes here.</p>". You could also do "include('protectedpage.php');".

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 12:21 pm
by Mark Baker
Alternatively, you could do a header redirect

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 3:06 pm
by califdon
Mark Baker wrote:Alternatively, you could do a header redirect
Yes, being careful that you haven't sent anything to the browser prior to the header.

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 3:30 pm
by Citrate
I would personally use a javascript redirect to avoid any header errors:

<script>window.location = "http://www.yourdomain.com/yourpage.php";</script>

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 3:32 pm
by Syntac
If the client has JavaScript disabled, that fails spectacularly.

A surefire way of avoiding "headers already sent" errors is output buffering.

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 3:37 pm
by Citrate
Syntac, perhaps an example script would help the op?

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 3:39 pm
by Syntac
Sure thing.

Code: Select all

// This doesn't work.
echo "Some text goes here.";
header("Location: http://www.example.org/");
 
// This works.
ob_start();
echo "Some text goes here.";
header("Location: http://www.example.org/");
ob_end_flush(); // You don't have to call this at the end, but you should.

Re: go to other page if password is correct

Posted: Sat Nov 22, 2008 3:56 pm
by Citrate
Looks good to me! Nice one Syntac.