Open file based on user input - what's the best way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
javafool
Forum Newbie
Posts: 5
Joined: Wed May 28, 2008 10:58 am

Open file based on user input - what's the best way?

Post by javafool »

Scenario:

A user has a 6 digit number (ex: 123456) and I know the user's name is Bob. 2 other users have numbers (ex: Mary - 552233 and Ron - 987654). I want the user to enter his first name and number to open a web page. There are only going to be 3 users and each should have a unique static page based upon their number and name. Since the number of users is small, I don't want to use a database. I was thinking either inline PHP logic or an external file to store the user's name and number. If someone doesn't know the name and number, they should be presented the "login" page again.

What is the best quick and dirty way to do this? It doesn't have to be pretty. Your suggestions or examples would be greatly appreciated.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Open file based on user input - what's the best way?

Post by panic! »

Hi,

Welcome to the forum,

Code: Select all

SEE BELOW

try that, dont have time to actually run/test it but give it a go.
Last edited by panic! on Wed May 28, 2008 11:31 am, edited 1 time in total.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Open file based on user input - what's the best way?

Post by panic! »

actually try:

Code: Select all

<?
 
 
if($_POST['username'])
{
$users['Mary']='552233';
$users['Ron']='987654';
$users['Bob']='12345';
 
$file['Mary']='mary.html';
$file['Ron']='Ron.html';
$file['Bob']='Bob.html';
 
 if($users[$_POST['username']]==$_POST['pincode'])
{
print "SUCCESS";
print file_get_contents($file[$_POST['username']]);
die();
}
 
}
 
?>
<form method="post">
<input name="username"> <input name="pincode"> <input type="submit">
</form>
javafool
Forum Newbie
Posts: 5
Joined: Wed May 28, 2008 10:58 am

Re: Open file based on user input - what's the best way?

Post by javafool »

Panic,

Very cool! I'll play around with this tonight.

Next I'll try to figure out how to move the users to a flat file and see if I can make that work. I know it would be easier to create a MySQL table to store the info in and retrieve it but I want to make something portable. If this was something that would be utilized a lot, that would be the way to go for ease of administration, as well as speed.

Thanks again!
javafool
Forum Newbie
Posts: 5
Joined: Wed May 28, 2008 10:58 am

Re: Open file based on user input - what's the best way?

Post by javafool »

Quick follow-up question. Using this example, what is the best way to force the username to be all lower case? Should be as simple as running the username through strtolower, right?

Thanks for both your assistance and being patient with a newb!
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Open file based on user input - what's the best way?

Post by panic! »

replace

if($users[$_POST['username']]==$_POST['pincode'])

with

if($users[strtolower($_POST['username'])]==$_POST['pincode'])

no problems.
Post Reply