go to other page if password is correct

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ma5ect
Forum Commoner
Posts: 35
Joined: Wed Jun 25, 2008 8:38 am

go to other page if password is correct

Post 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
 
}
 
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: go to other page if password is correct

Post 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');".
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: go to other page if password is correct

Post by Mark Baker »

Alternatively, you could do a header redirect
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: go to other page if password is correct

Post 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.
Citrate
Forum Newbie
Posts: 5
Joined: Sat Nov 22, 2008 3:26 pm

Re: go to other page if password is correct

Post by Citrate »

I would personally use a javascript redirect to avoid any header errors:

<script>window.location = "http://www.yourdomain.com/yourpage.php";</script>
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: go to other page if password is correct

Post by Syntac »

If the client has JavaScript disabled, that fails spectacularly.

A surefire way of avoiding "headers already sent" errors is output buffering.
Citrate
Forum Newbie
Posts: 5
Joined: Sat Nov 22, 2008 3:26 pm

Re: go to other page if password is correct

Post by Citrate »

Syntac, perhaps an example script would help the op?
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: go to other page if password is correct

Post 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.
Citrate
Forum Newbie
Posts: 5
Joined: Sat Nov 22, 2008 3:26 pm

Re: go to other page if password is correct

Post by Citrate »

Looks good to me! Nice one Syntac.
Post Reply