Strange array_search file problem....?
Posted: Mon Sep 05, 2005 2:23 pm
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:
Everything works fine, except one thing. This part:
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

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>";
?>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>");
}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