Page 1 of 1
search within a file
Posted: Fri May 18, 2007 10:04 pm
by Maluendaster
ok, i might be waaay off here, but i have this code
Code: Select all
<?php
$bus= "$_POST[email]";
$email= "$_POST[email]\n";
$redireccion ="http://www.google.cl"; // donde redireccionar si todo sale OK
// Si no hay entrada, imprimir esto
if(empty($_POST['email'])){
echo "No has ingresado ningun correo, haz <a href=javascript:history.back()>click aqui</a> e intentalo nuevamente.";
exit;
}
//Verifica si el correo existe en la BD
$gestor = fopen("email.db","a");
if ($bus = fscanf($gestor, "%[ -~]")) {
echo "El correo $bus ya existe";
fclose($gestor);
exit;
}
//Si no existe, agregarlo
else{
$file = 'email.db'; // Fila a Escribir
$fh = fopen($file, 'a') or die('No se pudo abrir la fila!'); // Abrir Fila
fwrite($fh, $email) or die('No se pudo escribir en la fila'); // Escribir en Fila
fclose($fh); // Cerrar Fila
}
@header("location:$redireccion"); // Redireccionar
exit;
?>
I want it to check if the email already exists in the email.db file, if exists, returns a message, if its not, it continues with the script...
Any help?
thank you in advance.
Posted: Fri May 18, 2007 10:23 pm
by bdlang
So what does the script do / not do?
Did you read the PHP manual section on
fscanf()?
Code: Select all
$bus= "$_POST[email]";
$email= "$_POST[email]\n";
What is the purpose of these two lines? You assign $bus to an unchecked value of $_POST['email'], then do
Code: Select all
$gestor = fopen("email.db","a");
if ($bus = fscanf($gestor, "%[ -~]")) {
echo "El correo $bus ya existe";
fclose($gestor);
exit;
}
Where do you attempt to match the value returned from fscanf() with $_POST['email']? You overwrite $bus in that second assignment.
You have no error handling in the first call to fopen().
Your script logic is all over the place. Start with checking for $_POST['email'],
then validate / assign it to a variable and check against the file contents.
Are you sure you want to use a flat file for this? Why not use SQLite or MySQL?
Posted: Sat May 19, 2007 3:43 am
by Ollie Saunders
fscanf is like the predecessor to regular expressions, basically not as powerful. So I've never bothered to learn how to use it and I would use preg_match() instead.
The else you have in your code is redundant because of the exit above it.
and there is a problem here
Code: Select all
$bus= "$_POST[email]";
$email= "$_POST[email]\n";
are you aware that $bus will output
and email obviously the same with a carriage return. You should write
Code: Select all
$bus = $_POST['email'];
$email = $_POST['email'] . "\n";
or
Read
the PHP manual on strings.
Posted: Sat May 19, 2007 4:36 am
by stereofrog
Code: Select all
preg_grep($regexp, file($filename));
returns all lines from $filename that match $regexp. In your case
Code: Select all
$email = preg_quote(@$_POST['email']);
if(count(preg_grep("/$email/", file('email.db'))))
email exists
else
email doesn't exist
hope this helps.
Posted: Sat May 19, 2007 1:26 pm
by Maluendaster
stereofrog wrote:Code: Select all
preg_grep($regexp, file($filename));
returns all lines from $filename that match $regexp. In your case
Code: Select all
$email = preg_quote(@$_POST['email']);
if(count(preg_grep("/$email/", file('email.db'))))
email exists
else
email doesn't exist
hope this helps.
yeah, it did, thanks, but now for some reason, all the entries is stored like this:
email1@mail.comemail2@
mail.comemail3@mail.com
and not like this:
email1@mail.com
email2@mail.com
email3@mail.com
any ideas??
here's the code so far.
Code: Select all
<?php
$email = $_POST['email'] . "\n";
$redireccion ="gracias.php"; // donde redireccionar si todo sale OK
// Si no hay entrada, imprimir esto
if(empty($_POST['email'])){
echo "<body bgcolor=#290000><font color=white face=Verdana><br><br><br><br><br><br><br><br><br><br><center>No has ingresado ningun correo, haz <a href=javascript:history.back()>click aqui</a> e intentalo nuevamente.</center></font></body>";
exit;
}
//Verifica si el correo existe en la BD
$email = preg_quote(@$_POST['email']);
if(count(preg_grep("/$email/", file('email.db'))))
//Si existe imprimir lo siguiente
echo "<body bgcolor=#290000><font color=white face=Verdana><br><br><br><br><br><br><br><br><br><br><center>El correo ingresado ya existe en nuestra base de Datos, haz <a href=javascript:history.back()>click aqui</a> e intentalo nuevamente.</center></font></body>";
//Si no existe, agregarlo
else{
$file = 'email.db'; // Fila a Escribir
$fh = fopen($file, 'a') or die('No se pudo abrir la fila!'); // Abrir Fila
fwrite($fh, $email) or die('No se pudo escribir en la fila'); // Escribir en Fila
fclose($fh); // Cerrar Fila
}
@header("location:$redireccion"); // Redireccionar
exit;
?>
Posted: Sat May 19, 2007 7:11 pm
by stereofrog
You forgot the newline
fwrite($fh, $email) should be fwrite($fp, "$email\n")