
July 1st, 2009, 03:57 PM
|
|
Contributing User
|
|
Join Date: Jul 2008
Location: Cleveland, Ohio, USA
Posts: 400
Time spent in forums: 4 Days 15 h 59 m 8 sec
Reputation Power: 2
|
|
PHP Code:
<?php
//Start the session for this page....
session_start();
//Create a username, probably already done somewhere else in the application...
$_SESSION['username'] = 'captain_man';
//Point to file
$filename = "usernames.txt";
//Opens the file and assigns reference point
$file = fopen($filename, "r");
//Reads the reference point
$string = fread($file,filesize($filename));
//Creates an array, one element for each new line
$each_line = explode("\n",$string);
//Not sure if this part is just for windows or not (I used notepad to create the usernames.txt file), but it removes the \r character(s) from the array
$each_line = str_replace("\r","",$each_line);
//Sets a variable to 0 to tell whether a user was found or not...
$yeah = 0;
//Splits the array into each element
foreach($each_line as $key => $value) {
//Checks to see if the array element is the same as the session variable 'username'
if($_SESSION['username'] == $value) {
//If it finds a match, echos "You are a User", sets yeah to 1 and stops the foreach loop
echo "You are a user!";
$yeah = 1;
break;
}
}
//If no usernames are matched to the session variable, echo 'not a user'
if($yeah!=1) {
echo "You are not a user...";
}
?>
Using the text file in the same directory as the php file:
Code:
jamestrowbridge
billy_bob
captain_man
|