Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 12th, 2009, 08:21 AM
Registered User
 
Join Date: Jan 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Password navigation

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Password site navigation phpnukes Beginning PHP 0 January 6th, 2009 09:03 PM
Navigation sg48 ASP.NET 2.0 Basics 2 February 13th, 2008 12:18 PM
Navigation volsha Dreamweaver (all versions) 1 July 20th, 2007 03:19 PM
navigation nidheeshkayal ASP.NET 2.0 Basics 0 March 16th, 2006 07:05 AM
Navigation JonnyRPI HTML Code Clinic 1 July 6th, 2003 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.