Jump to content

Form/Password help


marriedman624

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/185289-formpassword-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-978108
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-978111
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/185289-formpassword-help/#findComment-979448
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.