Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile PHP page for add/delete email address to text file not working

    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.

    PHP Code:
    <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>

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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 Code:
    <?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;
    ?>
    Code:
    <!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 Code:
    <?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 ''
    ?>
    Code:
    		<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>
    Last edited by syosoft; 05-22-2008 at 10:18 AM.
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for your help.

  • #4
    New to the CF scene
    Join Date
    May 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks
    Last edited by thebrighter; 05-24-2008 at 01:55 PM.

  • #5
    New to the CF scene
    Join Date
    May 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •