marriedman624 Posted December 15, 2009 Share Posted December 15, 2009 Hello, I'm a very new to php and I am trying to put together a form where a password is entered and based on the password, a page of pictures/proofs is loading (different page for different passwords). I have done a lot of research but can't get it to work. I have to following codes for my form and my php page. When I click submit on the form it only loads the first webpage (/farley). If the password is left blank it loads that page and no matter what i type in the password it loads that page. Any help would be appreciated. Also, if you could explain what you changed and what that change does I would appreciate it so i can learn how to do it myself. Thanks <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php if($pass = "farley" || "Farley"){ echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/farley"'; echo '</script>'; } else if($_POST['password'] == "merz" or "Merz"){ echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/merz"'; echo '</script>'; } else{ echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/error.html"'; echo '</script>'; } ?> </body> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="password" method="post" action="test.php"> pass<br /> <input type="password" name="password" size="50"> <p align="left"><input type="submit" name="Submit" value="Submit"></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/185289-formpassword-help/ Share on other sites More sharing options...
mrMarcus Posted December 15, 2009 Share Posted December 15, 2009 you're always getting '/farley' returned because that condition is always going to return true. you want to test the value like you do in the second conditional statement, with the == operator. there are much more efficient ways of doing this, but it seems they might be over your head a little bit, so i'll just correct what you have here without any major changes: <?php if (isset ($_POST['Submit'])) //this checks to make sure the form has been submitted; is the name="Submit" from your submit button in the form; { if (($_POST['password'] == 'farley') || ($_POST['password'] == 'Farley')) { echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/farley"'; echo '</script>'; } elseif (($_POST['password'] == 'merz') || ($_POST['password'] == 'Merz')) { echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/merz"'; echo '</script>'; } else { echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/error.html"'; echo '</script>'; } } ?> like i said, there are simpler ways to do this with switch statements and regex, but this'll work for you for now. if you want a little insight into changing this up, just let anybody know. and i was going to opt for header() instead of a javascript redirection, but then you must change up your HTML so it's not outputted to the browser before the header() is called. Quote Link to comment https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-978108 Share on other sites More sharing options...
mrMarcus Posted December 15, 2009 Share Posted December 15, 2009 i wasn't even thinking .. regex isn't necessary: <?php if (isset ($_POST['Submit'])) //this checks to make sure the form has been submitted; is the name="Submit" from your submit button in the form; { switch (strtolower ($_POST['password'])) { case 'farley': echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/farley"'; echo '</script>'; break; case: 'merz': echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/farley"'; echo '</script>'; break; default: echo '<script language="Javascript">'; echo 'window.location="http://www.crystalevansphotography.com/merz"'; echo '</script>'; break; } } ?> again, i'm too lazy to go through the whole headers thing right now .. take a look at this thread: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-978111 Share on other sites More sharing options...
marriedman624 Posted December 17, 2009 Author Share Posted December 17, 2009 Thanks for the help. It is working great now. I would like to change it up and use header redirect but I don't know how to do that. Can you tell me how? Which way do you recommend and why? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-979440 Share on other sites More sharing options...
mrMarcus Posted December 17, 2009 Share Posted December 17, 2009 the deal with sending headers is that there cannot be any output to the browser before the headers are sent (again, see this: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html). so, you can either enable output buffering, but that's just a lazy solution for now. instead, have all of your logic before any html like so: <?php if (isset ($_POST['Submit'])) //this checks to make sure the form has been submitted; is the name="Submit" from your submit button in the form; { switch (strtolower ($_POST['password'])) { case 'farley': header ('Location: http://www.crystalevansphotography.com/farley'); exit(0); break; case: 'merz': header ('Location: http://www.crystalevansphotography.com/merz'); exit(0); break; default: header ('Location: http://www.crystalevansphotography.com/error.html'); exit(0); break; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="password" method="post" action=""> pass<br /> <input type="password" name="password" size="50"> <p align="left"><input type="submit" name="Submit" value="Submit"></p> </form> </body> </html> just slapped it together. keeps your form with the form handling code on the same page (notice your form action has no script in it anymore [action=""]). give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-979448 Share on other sites More sharing options...
niwa3836 Posted December 19, 2009 Share Posted December 19, 2009 Why not just check $POST['Submit'] and then do a header calling the value of Password (might want a catch screen in your apache to catch people typing something wrong or maybe a file_exist() routing first) Then you wouldnt have to keep changing your script. Quote Link to comment https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-980609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.