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 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP script for saving written date into a file

    Hello, guys!

    I'm using a php mail-script with loging system that does the following:

    - User logs in (ex: user1337);
    - User writes something in a for and presses a button;
    - A mail is sent with the user name as the e-mail name and subject (user1337@domain.com).

    Well, I want to make a script that when he presses the send button, the script will create a .txt file containing the data he wrote. Also the file name would be composed from the user's name and current date ( user1337.17.11.2010.txt).

    Even more, the script should make a folder for each month (if it doesn't exist already), and for all the dates coresponding to that month to put them in that dirrectory).

    Then we would have :

    \root
    \january\user1337.15.01.2010.txt


    It's a thing I need to do for work, so if you could help me it would be great. At least a suggestion to a good starting place.

    Here's my existing code for sending the mail and login system:

    index.php
    Code:
     <?php
    session_start(); ?>
    <title>Main Page</title>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form name="form1" method="post" action="checklogin.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="3"><strong>Main login page</strong></td>
    </tr>
    <tr>
    <td width="78">Username</td>
    <td width="6">:</td>
    <td width="294"><input name="myusername" type="text" id="myusername"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="mypassword" type="password" id="mypassword"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Login"></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    <!-- uy7gdr5kmn -->
    checklogin.php
    Code:
    <?php
    session_start();
    ob_start();
    $host="localhost"; // Host name 
    $username="adrian"; // Mysql username 
    $password="cherry"; // Mysql password 
    $db_name="usersandpasses"; // Database name 
    $tbl_name="utilizatori"; // Table name 
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Define $myusername and $mypassword 
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword']; 
    $_SESSION['userul']=$myusername;
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE useri='$myusername' and parole='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword"); 
    
    header("location:login_success.php");
    
    }
    else {
    echo "Wrong Username/Password";
    }
    
    ob_end_flush();
    ?>
    <!-- uy7gdr5kmn -->
    login_success.php

    Code:
     <? 
    session_start();
    if(!session_is_registered(myusername)){
    header("location:index.php");
    }?>
    <html>
    <head><title>Main Login Page - Sendmail</title></head>
    <body>
    <form action="mail.php" method="POST">
    <input type="hidden" name="email" size=40>
    <p><b>Send-Mail</b><br>
    <textarea cols=80 rows=20 name="message"></textarea>
    <p><input type="submit" value=" Send Mail">
    </form>
    </body>
    </html>
    mail.php

    Code:
    <?php
    session_start();
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
      {
      //send email
      $email = "Mailfrom_" . $_SESSION['userul'];
      $subject = "Mailfrom_" . $_SESSION['userul'] ;
      //$subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail( "receiver@yahoo.com;receiver2@yahoo.com", "Subject: $subject",
      $message, "From: $email" );
      echo "Mail Succesfuly sent";
    session_destroy();  
      }
    else
    //if "email" is not filled out, display the form
      {
      echo "<form method='post' action='mail.php'>
      Email: <input name='email' type='text' /><br />
      Subject: <input name='subject' type='text' /><br />
      Message:<br />
      <textarea name='message' rows='15' cols='40'>
      </textarea><br />
      <input type='submit' />
      </form>";
      }
    ?>
    I'm thinking of changing mail.php, keep all existing structure of the script (the users aren't very PC-friendly), and just make all the required modifications there, so when they want to "send the mail" it would create that .txt file.

    Hope you guys can help me....I'm starting to search the web for some info about this.

    Thanks!

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    You can use fopen, fwrite and fclose to write plain text files.

    http://www.php.net/manual/en/function.fopen.php

    Check out the options, it has quite a few.

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I haven't tested this, but see if this does it for you. This is an updated version of mail.php:

    PHP Code:
    <?php
    session_start
    ();
    if (isset(
    $_REQUEST['email']))
    //if "email" is filled out, send email
      
    {
      
    //send email
      
    $email "Mailfrom_" $_SESSION['userul'];
      
    $subject "Mailfrom_" $_SESSION['userul'] ;
      
    //$subject = $_REQUEST['subject'] ;
      
    $message $_REQUEST['message'] ;
      
    mail"receiver@yahoo.com;receiver2@yahoo.com""Subject: $subject",
      
    $message"From: $email" );
      echo 
    "Mail Succesfuly sent";



      
    //save the data to a local file...
      //find the right folder to save to...
      
    $archive_folder='\\root\\'.strtolower(date('F',time()));  //this sets the archive destination as "root\month" - where "month" is the current month's full name in lowercase letters

      //check to see if month folder exists, if not we create it
      
    if(!is_dir($archive_folder)){
        
    mkdir($archive_folder);
      }

      
    //check to see if the archive file exists, if not we create it
      
    $filename=$_SESSION['userul'].".".date('d',time()).".".date('m',time()).".".date('Y',time()).".txt";
      if(!
    is_file($archive_folder.'\\'.$filename)){
        
    $content=$message;
        
    $handle fopen($filename'a+');
        
    fwrite($handle$content);
        
    fclose($handle);
      }




    session_destroy();  
      }
    else
    //if "email" is not filled out, display the form
      
    {
      echo 
    "<form method='post' action='mail.php'>
      Email: <input name='email' type='text' /><br />
      Subject: <input name='subject' type='text' /><br />
      Message:<br />
      <textarea name='message' rows='15' cols='40'>
      </textarea><br />
      <input type='submit' />
      </form>"
    ;
      }
    ?>
    One strong word of caution though... You really REALLY should validate your user input before doing anything like this at all (even sending e-mails). Never trust your users!!
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

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