...

View Full Version : PHP page for add/delete email address to text file not working



thebrighter
05-21-2008, 03:13 PM
Dear All,
I am new to php and I am trying to write a small page in php for my linux user/s
to add/delete email addresses to/from text file time to time as one column list. (Collecting spammers email address to block them) This page is hoping to put to Linux(Redhat) apache server and I have developed the page according to the information garbed from net and my c knowledge.

I have written this page but I am having problems.
1. I can add emails to the data file "file123" but I cant delete from it.
2. Once I load the page in first time it gives error which I wrote in the function "Invalided email address"
3. If the data file "file123" is empty function returns correct value for write email to "file123" but it email address doesen't add to the file.

Pls go through the code and help me to solve this.


<html>

<head>
<title>Spam Email address entering page</title>
</head>
<body>
<h2 align="center"><b>Please Enter Spam Email Addresses</b></h2>
<form name="form" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Add to Spam list<input type="text" name="AddEmail" /><input type="submit" />
Add to Spam list<input type="text" name="DelEmail" /><input type="submit" />
</form>
<hr>


<?php

//delete email from file123 function==========
function email_del($filename, $DelAddr)
{
if( ($DelAddr == NULL) || ($DelAddr == '') || (preg_match("/ /",$DelAddr)) || !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $DelAddr) )
{
$AnsWer="Invalied Address";
return $AnsWer;
}

$Data_array=file($filename);
$Array_size=count($Data_array);

$HanDle=fopen($filename,"w+");
if ($HanDle == FALSE) //File open check
{
$AnsWer="File open error";
fclose($HanDle);
return $AnsWer;
}
else
for($line=0; $line<=$Array_size; $line++) //check Address already their check
{
$WoRd = "/\b".$DelAddr."\b/";
if ( preg_match($WoRd,$Data_array[$line]) )
fprintf($HanDle,"%s\n",$DelAddr);
else
$AnsWer="Address ".$DelAddr." deleted";
}

fclose($HanDle);
return $AnsWer;
}



//Add email to file123 function=================
function email_add($filename,$AddAddr)
{
if( ($AddAddr == NULL) || ($AddAddr == '') || (preg_match("/ /",$AddAddr)) || !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $AddAddr) )
{
$AnsWer="Invalied Address";
return $AnsWer;
}

$Data_array=file($filename);
$Array_size=count($Data_array);

$HanDle=fopen($filename,"r");
if ($HanDle == FALSE) //File open check
{
$AnsWer="File open error";
fclose($HanDle);
return $AnsWer;
}
else
for($line=0; $line<=$Array_size; $line++) //check Address already their check
{
$WoRd = "/\b".$AddAddr."\b/";
if ( preg_match($WoRd,$Data_array[$line]) )
{
$AnsWer="Address already there ".$line;
fclose($HanDle);
return $AnsWer;
}
}

fclose($HanDle);

$HanDle=fopen($filename,"a+");
if ($HanDle == FALSE) //File open check
{
$AnsWer="File open error";
fclose($HanDle);
return $AnsWer;
}
else
if (fprintf($HanDle,"%s\n",$AddAddr) == FALSE) //write address to end of file
{
$AnsWer = "write failed";
fclose($HanDle);
return $AnsWer;
}
else
$AnsWer="Record added";

fclose($HanDle);
return $AnsWer;
}


//Display email list entered================
function read_file($filename)
{
$Data_array=file($filename);
$Array_size=count($Data_array);

$HanDle=fopen($filename,"r");
if ($HanDle == FALSE) //File open check
{
$AnsWer="File open error";
fclose($HanDle);
return $AnsWer;
}

for($line=0; $line<=($Array_size-1); $line++)
{
printf("%d. %s <br>",($line+1),$Data_array[$line]);
}
fclose($HanDle);
}

echo read_file('file123');

$EmaiL1=$_REQUEST["AddEmail"];
echo email_add('file123',$EmaiL1);

$EmaiL2=$_REQUEST["DelEmail"];
echo email_del('file123',$EmaiL2);

?>
</body>

</html>

syosoft
05-22-2008, 10:16 AM
This is untested code, but should do what you're after (with a couple syntax fixes no doubt). Note: This is code for a full web page.
You may have to switch back to the way you were opening and reading files (and closing) if you cant use file_(get|put)_contents on your server. They require PHP 4 >= 4.3.0, PHP 5. http://php.net/file_get_contents


<?php
function get_email_list() {
if(!$email_list = file_get_contents('file123')) :
return array();
endif;

return explode("\n", trim($email_list));
}

function write_email_list($email_list=array()) {
if(!file_put_contents('file123', implode("\n", $email_list))) :
return false;
endif;

return true;
}

function add_email($email) {
$email = strtolower(trim($email));
$email_list = get_email_list($file_name);

if(in_array($email, $email_list)) :
return true;
endif;

$email_list[] = $email;
return write_email_list($email_list);
}

function delete_email($email) {
$email = strtolower(trim($email));
$email_list = get_email_list($file_name);

if(!$key = array_search($email, $email_list)) :
return true;
endif;

unset($email_list[$key]);
return write_email_list($email_list);
}

# Process post
if($_POST) :
$status = array();

if($_POST['add'] != '') :
if(delete_email($_POST['add'])) :
$status[] = '"'.$_POST['add'].'" was added.';
else :
$status[] = 'Failed to add "'.$_POST['add'].'"';
endif;
endif;

if($_POST['delete'] != '') :
if(delete_email($_POST['delete'])) :
$status[] = '"'.$_POST['delete'].'" was deleted.';
else :
$status[] = 'Failed to delete "'.$_POST['delete'].'"';
endif;
endif;

if(sizeof($status) == 0) :
$status[] = 'No action was taken.';
endif;

$form_status = '<p class="status">'.implode('<br>', $status).'</p>';
endif;
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<h1>Current Spam List</h1>



<?php
# Display email list
echo '<ol>';
foreach(get_email_list() as $email) :
echo '<li>'.$email.'</li>';
endforeach;
echo '</ol>';

# Output form status if available
echo isset($form_status) ? $form_status : '';
?>



<form name="form" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Add to Spam list: <input type="text" name="add" /> <input type="submit" />
Delete from Spam list: <input type="text" name="delete" /> <input type="submit" />
</form>
</body>
</html>

thebrighter
05-22-2008, 11:36 AM
thanks for your help.

thebrighter
05-24-2008, 01:52 PM
Thanks

thebrighter
05-24-2008, 01:54 PM
I have tried the codes you have given and it is working once I change the "delete_emai" to "add_email" in the "if(delete_email($_POST['add']))" in the Process post section.

But my problem is it wont delete the first line of the text file except other lines. If I need to delete first line how can I change the codings.

Pls help me.

Apart form that you have done the entire codings in few, simple steps (that I have done). Thats great work. Thanks for that again.

thebrighter



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum