Page 1 of 1

Open file based on user input - follow up question

Posted: Fri May 30, 2008 2:40 pm
by javafool
Using the following code works for most of my scenarios but I've run into an issue. I have two users named Mary, each with a different ticket number. Using the following code, only the 2nd one can retrieve her files. I assume it is a case of last in resetting the variable $users and that is why Mary 111111 fails the test.

The file name is the user's name and ticket number, so the only issue is the logic to check more than the name. Possibly create a variable that is the first name and ticket?

Suggestions on how to code for this condition?

Code: Select all

<?
 
if($_POST['username'])
{
$users['mary']='111111';
$users['mary']='222222';
$users['dick']='555555';
$users['harry']='777777';
 
$file['mary']='mary111111.htm';
$file['mary']='mary222222.htm';
$file['dick']='dick.htm';
$file['harry']='harry.htm';
 
 if($users[$_POST['username']]==$_POST['pincode'])
    {
     print "SUCCESS";
     print file_get_contents($file[$_POST['username']]);
     die();
    }
}
 
?>
<form method="post">
  <input name="username" value="first name" size="25" maxlength="12">
  <input name="pincode" value="ticket" size="25" maxlength="6">
  <input name="Search" type="submit" id="Search" value="Search">
</form>

Re: Open file based on user input - follow up question

Posted: Fri May 30, 2008 2:56 pm
by Bal
an array key can only have a single value. so, the array name[mary] can only carry a single value and it takes the most recent value written to it.

If this is being generated from a DB then I would instead assign a unique number (UserID, or what-have-you). then the array key could be the userID and you wouldn't need to code around the problem.

The only other solution that rolls off the top of my head is the undesirable approach of creating unique keys by dropping it in a loop and appending the loop counter to the end, so you would end up with monstrosities like 'mary1', 'mary2' and no real way of identifying one from the other.

Re: Open file based on user input - follow up question

Posted: Fri May 30, 2008 3:33 pm
by javafool
Nope, not pulling from a database. Taking user input, verifying the name and pin are correct and then displaying a file.

Working on some other files and had an epiphany. While the name may not be unique, the pin should be. I've just switched the variables around and will test it in a few. Should work fine, tho.

Was thinking about handling it a different way but this is a quick and dirty to get something out tomorrow.