The following code is part of a form/handler that directs a valid password entered to a valid page or a invalid password to a invalid page. A cookie javascript is also included.
My first ideal was to see if I could php include the $urlList to an external file that saves users passwords. Part of a different registration script.
Code:
<?php
# Set up the password strings and associated URLs. Note that all the elements of
# this hash list but the last MUST be separated by commas. You may have as many
# password => URL combinations as you wish.
$urlList = array ('xyzzy' => 'valid.html',
'abcdef' => 'valid.html',
'123456' => 'valid.html'
);
# Set up the invalid password URL - this should also be set up in the JavaScript
$invalidURL = 'invalid.html';
# The cookie name - this should be the same as in the JavaScript
$cookieName = 'validpassword';
# ******************************************************************************
# If there is nothing in the form's submit field, this is the first time in.
# Simply display the form and await the user's input. Othwerwise, pick up the
# password from the form.
if (isset ($_POST['submit']) && strlen($_POST['password']) > 0) {
# See if the password provided by the user exists in the list.
if (array_key_exists ($_POST['password'], $urlList)) {
# Password found - first set the cookie
setcookie ($cookieName, 0, NULL,'/');
# Now go to the page associated with the password.
header ("Location: " . $urlList[$_POST['password']]);
}
else {
# Password not found - go to the 'invalid password' page
header ("Location: " . $invalidURL);
}
}
?>
Sadly, the following highlighted mod is not allowing the content of the external flat file to behave as a $urlList. But jumps to the $invalidURL page.
I have copied the following code into the passwords.php file
Code:
$urlList = array ('xyzzy' => 'valid.html',
'abcdef' => 'valid.html',
'123456' => 'valid.html'
);
And modified the include call highlighted
Code:
<?php
$urlList = array(
'include'=> array(
'base' => './passwords.php'
)
);
$invalidURL = 'invalid.html';
$cookieName = 'validpassword';
if (isset ($_POST['submit']) && strlen($_POST['password']) > 0) {
# See if the password provided by the user exists in the list.
if (array_key_exists ($_POST['password'], $urlList)) {
# Password found - first set the cookie
setcookie ($cookieName, 0, NULL,'/');
# Now go to the page associated with the password.
header ("Location: " . $urlList[$_POST['password']]);
}
else {
# Password not found - go to the 'invalid password' page
header ("Location: " . $invalidURL);
}
}
?>
<html>
<body>
<form id="myform" action="password.php" method="post">
<p>Try it: (passwords are <span style="color: #ff0000;">xyzzy, abcdef, 123456):</span>
<span style="color: #ff0000; margin-left: 20px;">Enter password:
<input type="password" name="password" maxlength="12" size="12" style="margin-left: 10px;" />
<input type="submit" name="submit" value="submit" style="margin-left: 30px; color: #f00;" />
</span></p>
</form>
</body>
</html>
This is getting a bit complicated and wonder if there is a simpler option that can be used