[SOLVED]Help

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

User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post 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 

} 
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

Parse error: parse error in /home/krunk/public_html/BnX/index.php on line 14
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

add this to the end.

<?
} // to end the else statement

?>
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

Same error again.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Yes you can. He opened two if statements. :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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");
}
?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Yes you can. He opened two if statements.
So he did :) Damn that's a good argument for indenting your code!
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

Thanks alot! Works great.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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).
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
Post Reply