Page 2 of 2
Posted: Sun Jun 06, 2004 1:25 pm
by tim
Code: Select all
<b>Log In</b>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<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>
<?
$password = "bnx4life";
if (isset($_POST['txtPassword'])) {
if($_POST['txtPassword'] != $password) {
// tell them to log-in again
} else {
// login is good
}
?>
/edit - this isnt what you want, but you should be able to correct your code with it
Posted: Sun Jun 06, 2004 1:37 pm
by bawla
This is what I got, but I keep getting an error.
Code: Select all
<?php
$password = "bnx4life";
if ($_POST['txtPassword'] != $password) {
?>
<b>Log In</b>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<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>BnX members please log in.</p>
<?php
}
?>
<?php
if (isset($_POST['txtPassword'])) {
if($_POST['txtPassword'] != $password) {
// tell them to log-in again
} else {
// login is good
}
?>
Posted: Sun Jun 06, 2004 3:22 pm
by tim
i hope I didnt confuse you.
Code: Select all
<?php
if (isset($_POST['Submit'])) {
$password = "bnx4life";
$pass = $_POST['txtPassword'];
if ($password == $pass) {
// login was correct
} else {
// login was incorrect
} else {
?>
<b>Log In</b>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<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>
this should work. if not, if you get an error please post the error. It'll help us try to help you
Posted: Sun Jun 06, 2004 5:57 pm
by bawla
Parse error: parse error in /home/krunk/public_html/BnX/index.php on line 14
Posted: Sun Jun 06, 2004 6:00 pm
by tim
add this to the end.
<?
} // to end the else statement
?>
Posted: Sun Jun 06, 2004 9:13 pm
by bawla
Same error again.
Posted: Sun Jun 06, 2004 9:30 pm
by launchcode
You cannot have two else statements following each other, tim probably meant to use elseif () instead for the middle one (or some other kind of code sequence / order). Two } else { blocks would cause a parser error.
Posted: Sun Jun 06, 2004 9:36 pm
by d3ad1ysp0rk
Yes you can. He opened two if statements.

Posted: Mon Jun 07, 2004 2:50 am
by patrikG
Try
Code: Select all
<?php
$password = "bnx4life";
if ($_POST['txtPassword'] != $password) {
echo '<b>Log In</b>
<form name="form" method="post" action="'.$_SERVER["PHP_SELF"].'">
<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>';
}
else {
header("Location: http://forums.devnetwork.net");
}
?>
Posted: Mon Jun 07, 2004 5:32 am
by launchcode
Yes you can. He opened two if statements.
So he did

Damn that's a good argument for indenting your code!
Posted: Mon Jun 07, 2004 2:23 pm
by bawla
Thanks alot! Works great.
Posted: Mon Jun 07, 2004 3:37 pm
by patrikG
Great! What I'd advise: NEVER(!) open an IF-statement, then "end" PHP with ?> write some HTML and the "open" PHP again with <?php
The parser will be somewhat confused if you do that, your code becomes extremely untidy and it's hard for others to understand what's going on. Try working towards solutions where you actually have an HTML page and only show the results of your PHP-functions. That's a first step towards seperating "logic" and view (your HTML).
Posted: Mon Jun 07, 2004 6:11 pm
by tim
yeah I did open two if statements
i'm not that dumb... I meant to show him a way to do it, i wouldnt end my code like that either patrik.
I would echo out the html form if it was me
=]
glad he got it working tho