I made a phonebook, it might be usefull to somebody.
<?php
class filehandler {
function head($h){
include($h); //include the file
return true;
} //header function
function foot($f){
include($f); //include the file
return true;
} //footer function
function create($file) {
if(!file_exists($file)){
$fp=fopen($file, "x");//create the phonebook
$fp=fclose($fp);
return true;
} //if the file doesnt exist, create it
} // function to initially create the phonebook
function add($name, $number, $file){
$fp=fopen($file, "a+"); //open with append attributes
flock($fp, LOCK_EX); //lock the file
fwrite($fp, base64_encode($name.",".$number."\r\n"), 1024);//write the entry
flock($fp, LOCK_UN);//unlock the file
fclose($fp); //close the file
return true;
} //function to add an entry to the phonebook file
function filelist($file){
$link= file_get_contents($file); //read the file into the array $link
$link = base64_decode($link);
$link = explode("\r\n", $link);
$i=1; //set counter var to 0
sort($link); //sort the array to alphabetical order
echo "<table border='1'>"; //echo start of table
while($i<=sizeof($link) - 1){
list($name, $number)=split(",", $link[$i]);//make the variables
echo "<tr>
<td>".$name."</td><td>".$number."</td>
</tr>"; // echo the phonebook entry
$i++; // advance counter var
}//loop to write the contents of the comma delimited file
echo "</table>"; // end of table
return true;
}//function to read the PHONEBOOK
function authorised($testnum, $authnum){
if($testnum == $authnum){
return true; //if you are authed, return true
} else {
return false; //if you are not authed, return false
}
} // function to determine if you are uthorised to view the phonebook
}
define("PHONEBOOK", "phonebook.dat"); //phonebook data file
define("SELF", "phonebook.php"); //this file
define("HEAD", "header.php"); //header file
define("FOOT", "footer.php"); //footer file
$fh = new filehandler(); //initialize the class
$fh->create(PHONEBOOK); //create the phonebook data file if it is not there.
if (isset($_GET['view'])){
if($fh->authorised($_COOKIE['derrr'], "oooop")){ //BE SURE TO CHANGE THESE VALUES TO A COOKIE THINGY TO AUTHORISE VIEWING PRIVLIGES! !!!!!!!!!!SAME WITH LINE 83!!!!!!
$fh->head(HEAD); //include the header
$fh->filelist(PHONEBOOK); //list the contents of the phonebook.dat file in order
?>
<hr />
<form name="bob" action="phonebook.php?write" method="post">
Name:<br />
<input name="name" type="text" /><br />
Phone Number:<br />
<input name = "number" type="text" /><br />
<input type="submit" value="Add to phonebook"/>
</form>
<?php
### above is the form to post a new entry
$fh->foot(FOOT); //include footer
} else {
$fh->head(HEAD);
echo "Sorry, but this service is not open to the public";
$fh->foot(FOOT);
exit;
}
} elseif(isset($_GET['write'])){
if($fh->authorised($_COOKIE['derrr'], "oooop")){ //BE SURE TO CHANGE THESE VALUES TO A COOKIE THINGY TO AUTHORISE VIEWING PRIVLIGES!
$fh->add($_POST['name'], $_POST['number'], "phonebook.dat"); //add the entry
header("Location: [url]http://[/url]".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".SELF."?view"); //redirect
exit;
}
} else {
header("Location: [url]http://[/url]".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".SELF."?view");
exit;
}
?>