Helpful Information
 
 
Category: Coding tips & tutorials threads
Unique file() function -- PHP

Well, all the credits from this piece of snippet goes to Twey(it's his function with some minor changes).



<?php
function newfile($filename, $sep){
$lines = file($filename);
$config = array();
for($i = 0; $i < count($lines); ++$i)
$config[substr($lines[$i], 0, strpos($lines[$i], $sep))] = substr($lines[$i], strpos($lines[$i], $sep) + 1, -1); // -2 for a Windows host
return $config;
}
print_r(newfile('newfile.txt'));
?>




test1|test2
test3|test4
test5|test6


This is an extremely useful function in PHP file handling(it makes things a heck lot easier). Hope you find it useful. Kudos to Twey.

Erm... what does it do? Posting a description with either comments or some sort of example, etc. would be helpful for people who might find this script useful.

drj33: It reads the file like this(in this case the newfile.txt)



Array("test1" => "test2", "test3" => "test4", "test5" => "test6");


It is so extremely useful in creating authentication systems. I am sure there is a version I developed somewhere around. I have also created a more complex version.

Sorry for the lack of any examples or anything. :(

Could you show the original txt file, and by what parameters the function is called?

Nevermind, I posted when you posted, answering my question :)

It should be noted that this was created for a special situation (what exactly, I can't remember now), and in most cases parse_ini_file (http://www.php.net/parse_ini_file)() is both easier and more flexible.

I'm not sure this thread is in the right forum.

Twey: How come, it's not in the right forum?

Everyone who sees this post: If you want to know how to use it of why to use it, let me show you a demonstration:

Suppose, you're creating a login system and you need a flat-file text database in the registration you simply append the data to the file users.txt, something like this:



<?php exit();?>
//userid generated from uniqid() or some other techniques
someuser:userid,password
someotheruser:hisid,password


the userdata.txt file:



<?php exit();?>
userid:email,someotherdata
hisid:email,someotherdata


You've the following code to read the user data/login the user:



<?php
session_start();
$userlist = newfile("users.txt", ":");
$userdata = newfile("userdata.txt", ":");
list($id, $password) = explode(",",$userlist[$_POST["username"]);
if(isset($userlist[$_POST["username"]]) && $password == $_POST["password"]){
echo "Hello ".$_POST["username"]."<br>";
echo "Your email is $userdata[$id]";
$_SESSION["user"] = $id;
}
?>


In this way you can use only the id to find out anything about the user(something similar to relational databases) but it also gives you more power to update fields, I'll write about updating fields later(I am in school now and I even live coded the above scripts so they might not work, I will post a working version as soon as I get home(hopefully)).

Twey: How come, it's not in the right forum?Well, it's just a function, not a tip or tutorial particularly.

Twey: So, how is a tip supposed to be? I am sorry but I am a newbie in this forum.

A method as to how to accomplish something, usually -- such as "how to use opacity in scripts cross-browser" or something along those lines.

How about it now? I will be posting the whole system soon.

I think the main point is that anything in here should be helpful to nearly everyone.
General things that there are a lot of questions on, such as opacity in all browsers, why you can't protect source code, how to make flash not be the top layer, how to make an email form... etc.
This script is fine... but... doesn't seem so widely applicable.

I think that is basically what Twey is saying.

I still barely get what it does, so, if I don't understand it, and I'm a regular here and I know a good bit about PHP, then I don't think it will be helpful to that many people (though it might be VERY helpful to a few, but just not to many).










privacy (GDPR)