Page 1 of 1

Strange array_search file problem....?

Posted: Mon Sep 05, 2005 2:23 pm
by IceMetalPunk
Anyone who knows me knows that I prefer text files over MySQL for databases. Now, I have started a new project where teachers can input grades for their students and have a "plans for the week" section.

I started on the registration code, and I used this code:

Code: Select all

<body bgcolor="#00aaaa">
<?php
if (!isset($_POST['fname']) || $_POST['fname']=="") {
exit("<b><center>Please <a href='JavaScript:history.go(-1)'>go back</a> and enter your first name.</b></center>");
}
$fname=$_POST['fname'];

if (!isset($_POST['pass']) || $_POST['pass']=="") {
exit("<b><center>Please <a href='JavaScript:history.go(-1)'>go back</a> and enter a password.</b></center>");
}

if (!isset($_POST['pass2']) || $_POST['pass2']=="") {
exit("<b><center>Please <a href='JavaScript:history.go(-1)'>go back</a> and enter a password in the ''Confirm Password'' box.</b></center>");
}

if ($_POST['pass']!=$_POST['pass2']) {
exit("<b><center>The passwords do not match. Please <a href='JavaScript:history.go(-1)'>go back</a> and fix this.</b></center>");
}

if (!isset($_POST['lname']) || $_POST['lname']=="") {
exit("<b><center>Please <a href='JavaScript:history.go(-1)'>go back</a> and enter your last name.</b></center>");
}
$lname=$_POST['lname'];
if ($_POST['school']=="other") {
if ($_POST['oschool']=="") {
exit("<b><center>Please <a href='JavaScript:history.go(-1)'>go back</a> and enter your school's name.</b></center>");
}
else {
$regdschools=file_get_contents("regdschools.txt");
$regdschools=explode("\r\n",$regdschools);
if (array_search($_POST['oschool'],$regdschools)==FALSE) {
$all=file_get_contents("regdschools.txt");
$f=fopen("regdschools.txt","w");
fwrite($f,$all.$_POST['oschool']."\r\n");
fclose($f);
$school=$_POST['oschool'];
}
}
}
else {
$regdschools=file_get_contents("regdschools.txt");
$regdschools=explode("\r\n",$regdschools);
if (array_search($_POST['school'],$regdschools)==FALSE) {
$all=file_get_contents("regdschools.txt");
$f=fopen("regdschools.txt","w");
fwrite($f,$all.$_POST['school']."\r\n");
fclose($f);
}
$school=$_POST['school'];
}
if (file_exists($school.".txt")) {
$all=file_get_contents($school.".txt");
$all=explode("\r\n",$all);
if (array_search($lname.", ".$fname,$all)) {
exit("<b><center>A teacher from that school with that name has already registered. If it was not you, please <a href='MailTo:IceMetalPunk@comcast.net?subject=TeachersPet.tk Fraudulent Account'>E-Mail the TeachersPet.tk supervisor</a> and we will try to get this straigtened out.<br><br><a href='history.go(-1)'><-- Go Back</a></b></center>");
}
}
$all="";
if (file_exists($school.".txt")) { $all=file_get_contents($school.".txt"); }
$f=fopen($school.".txt","w");
fwrite($f,$all.$lname.", ".$fname."\r\n");
fclose($f);
$f=fopen($lname.", ".$fname.".txt","w");
fwrite($f,md5($_POST['pass']));
fclose($f);
echo "<b><center>You have been successfully registered! Thanks for using TeachersPet.tk! Please <a href='login.html'>click here</a> to go to the login page.</b></center>";
?>
Everything works fine, except one thing. This part:

Code: Select all

$all=file_get_contents($school.".txt");
$all=explode("\r\n",$all);
if (array_search($lname.", ".$fname,$all)) {
exit("<b><center>A teacher from that school with that name has already registered. If it was not you, please <a href='MailTo:IceMetalPunk@comcast.net?subject=TeachersPet.tk Fraudulent Account'>E-Mail the TeachersPet.tk supervisor</a> and we will try to get this straigtened out.<br><br><a href='history.go(-1)'><-- Go Back</a></b></center>");
}
It's supposed to check the list of teacher names registered for that school. If the current teacher is already registered there, it's supposed to echo a message.

The problem is that that just doesn't work at all. I can register with the same name at the same school with the same password hundreds of times, and I never get that message. It just adds the name to the list as many times as I register, even though it's all the same name.

Can anyone please help me find what I'm doing wrong?

-IMP ;) :)

Posted: Mon Sep 05, 2005 2:49 pm
by josh
array_search returns a mixed key, if you want to use it as a bool like you are doing, use in_array() instead. also instead of exploding you could just use file() to build an array, and if you enable auto_detect_line_endings, it will increase your portability for instance, so when someone edits your file on a mac system and line endings are \r your script doesn't go crazy. To optomize your code further use fgets, and check each line one at a time, that way you're not reading more data then you need to.

Why may I ask why you don't use DBMS's? Flatfile is very inneficient, and is harder to work with. Something like what you're doing would be best coded using a database.

Posted: Mon Sep 05, 2005 2:52 pm
by IceMetalPunk
I'll try using in_array, although php.net says that array_search returns boolean FALSE if it's not found...

Also, I like textifles better just out of personal preference. To me, they are easier to work with...

-IMP ;) :)

*EDIT* Oh, yeah, and I use explode on file_get_contents instead of using just file() because I don't want to have to go through each array element to remove the line breaks and newline charachters.

Posted: Mon Sep 05, 2005 2:57 pm
by josh
yes, it returns false on failure, so to use array_search you'd need to negate it.

Code: Select all

if ( !array_search() ) {
    //  this teacher did not exist
} else {
    // the teacher did exist
}
seeing as you are not using the key in any way it's pointless to use array_search, use in_array instead

Code: Select all

if ( in_array() ) {
    // teacher did exist
} else {
    // teacher did not exist
}
the only reason you'd need array_search in this script would be if you were doing something like

Code: Select all

if ( !$key=array_search() ) {
    // tecaher did not exist
} else {
    echo ("You are teacher #{$key}");
}
Edit: in response to your edit: rtrim()

for portability if youre not going to use file/fgets and rtrim, you should use preg_split and write a regular expression so you can accept different types of line endings

Posted: Mon Sep 05, 2005 3:01 pm
by IceMetalPunk
Thanks, the code you gave me worked... now I just need to get one more bug out and everything should work.

-IMP ;) :)