A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Little help with PHP...

  1. #1
    www.stylefrogcreative.com
    Join Date
    Apr 2001
    Posts
    100

    Little help with PHP...

    Hi all,

    I have built a message board in Flash which uses the following code to send, write and read:

    Code:
    if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Input required for all fields.");
    exit;
    }
    
    $email = strtolower($email);
    
    addentry($name, $email, $message);
    
    function addentry($name, $email, $message) {
    
    $posted = strftime("%D %I:%M %p");
    
    $message = stripslashes($message);
    
    $file = fopen('entry.txt', 'a+');
    
    if (!$file) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 766.");
    exit;
    }
    
    fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#000000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
    fclose($file);
    
    // Send admin an email when new entry occurs
    mailAdmin($name, $email, $message);
    }
    
    function mailAdmin($name, $email, $message) {
    	$mailTo = "my@mail.co.uk";
    	$mailFrom = "From: <my@mail.co.uk>";
    	$mailSubject = "New Guestbook Entry";
    	$mailBody = "A visitor to your site has left the following information in your guestbook:\n
    	Name: $name
    	Email: $email
    	The visitor commented:
    	------------------------------
    	$message 
    	------------------------------
    	You can view the message at:
    	http://www.yoursite.com";
    	mail($mailTo, $mailSubject, $mailBody, $mailFrom);
    }
    
    print "&result=okay";
    exit;
    
    ?>
    Basically I need to write to the beginning of the file each time, so that the most recent message is shown at the top.

    I've tried fiddling around with the read/write attributes (a+,w,r+... etc) but to no avail!!!

    Any help is much appreciated,

    Thanks in advance.
    Stylefrog

    matt@stylefrogcreative.com
    http://www.stylefrog.net
    http://www.stylefrogcreative.com

  2. #2
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    You should use:
    PHP Code:
    fseek($file0); 
    or
    PHP Code:
    rewind($file); 
    right before your
    PHP Code:
    fputs($file"blablabla"); 
    By the way, I don't understand why "fputs" is used instead of "fwrite".. it's a sinonym but it's much less known... anyway..

    For further info you should visit http://www.php.net/. Just type "fwrite" in the search box, you will be pointer to file handling functions documentation.

    Cheers
    Altruism does not exist. Sustainability must be made profitable.

  3. #3
    www.stylefrogcreative.com
    Join Date
    Apr 2001
    Posts
    100
    Hi there,

    Thanks for the reply but neither fseek or rewind made any difference - the new posts are still being written at the end of the file!

    Any other ideas?

    Thanks again!

    M
    Stylefrog

    matt@stylefrogcreative.com
    http://www.stylefrog.net
    http://www.stylefrogcreative.com

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi

    short info on file modes (see documentation about fopen on www.php.net or elsewhere)
    r - open for read
    w - open for write
    a - open for append (new info will appear at the end)
    r+ - open for read followed by write (with an intermediate fseek)
    w+ - open for write first, followed by read
    There is no way to insert new data before existing one, so your script has to do it
    - fopen file in r+ mode
    - flock() the file
    - read entire file into a variable
    - fseek to beginning of file
    - write new data
    - write old data from variable
    Note the flock() here - you certainly do not want this to happen in two simultaneous processes at the same time

    Musicman

  5. #5
    www.stylefrogcreative.com
    Join Date
    Apr 2001
    Posts
    100
    Hi Musicman,

    Many thanks for the reply, so is this correct?:

    Code:
    if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Input required for all fields.");
    exit;
    }
    
    $email = strtolower($email);
    
    addentry($name, $email, $message);
    
    function addentry($name, $email, $message) {
    
    $posted = strftime("%D %I:%M %p");
    
    $message = stripslashes($message);
    
    $file = fopen('entry.txt', 'r+');
    flock($file);
    fseek($file, 0);
    
    if (!$file) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 766.");
    exit;
    }
    
    fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#000000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
    fclose($file);
    
    // Send admin an email when new entry occurs
    mailAdmin($name, $email, $message);
    }
    
    function mailAdmin($name, $email, $message) {
    	$mailTo = "my@mail.co.uk";
    	$mailFrom = "From: <my@mail.co.uk>";
    	$mailSubject = "New Guestbook Entry";
    	$mailBody = "A visitor to your site has left the following information in your guestbook:\n
    	Name: $name
    	Email: $email
    	The visitor commented:
    	------------------------------
    	$message 
    	------------------------------
    	You can view the message at:
    	http://www.yoursite.com";
    	mail($mailTo, $mailSubject, $mailBody, $mailFrom);
    }
    
    print "&result=okay";
    exit;
    Stylefrog

    matt@stylefrogcreative.com
    http://www.stylefrog.net
    http://www.stylefrogcreative.com

  6. #6
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    this is just a snippet from your code, with changes added:
    Code:
    $file = fopen('entry.txt', 'r+');
    flock($file, LOCK_EX);  // tell flock what it should do
    $old = fread($file, filesize($file));  // read in old data first
    fseek($file, 0);
    
    if (!$file) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 666.");   // 766 makes no sense for text files 
    exit;
    }
    
    fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#000000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
    fputs($file, $old);  // write back old data
    // flock($file, LOCK_UN); // this would unlock the file after use
    fclose($file);  // but closing it will unlock it anyway
    Musicman

  7. #7
    www.stylefrogcreative.com
    Join Date
    Apr 2001
    Posts
    100
    Hi Musicman,

    Thanks so much for following this up for me - I think I'm nearly there, I realised that I needed to keep the first 21 characters contained in entry.txt for the flash to display everything properly (&result=okay&entries=). So basically we need to open the file, seek to the 22nd character, copy that info to a variable, write the new data then paste in the old stuff underneath.

    This is where I'm at - not sure if I've addressed seek correctly by writing it as fseek($file, 21);

    Here's how the script is looking - it seems to write the data over itself, so only maintains one entry rather than writing after:

    Code:
    <?
    
    if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Input required for all fields.");
    exit;
    }
    
    $email = strtolower($email);
    
    addentry($name, $email, $message);
    
    function addentry($name, $email, $message) {
    
    $posted = strftime("%D %I:%M %p");
    
    $message = stripslashes($message);
    
    $file = fopen('entry.txt', 'r+');
    flock($file, LOCK_EX);  // tell flock what it should do
    $old = fread($file, filesize($file));  // read in old data first
    fseek($file, 21);
    
    if (!$file) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 666.");   // 766 makes no sense for text files 
    exit;
    }
    
    fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#000000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
    fputs($file, $old);  // write back old data
    // flock($file, LOCK_UN); // this would unlock the file after use
    fclose($file);  // but closing it will unlock it anyway
    
    // Send admin an email when new entry occurs
    mailAdmin($name, $email, $message);
    }
    
    function mailAdmin($name, $email, $message) {
    	$mailTo = "my@mail.co.uk";
    	$mailFrom = "From: <my@mail.co.uk>";
    	$mailSubject = "New Guestbook Entry";
    	$mailBody = "A visitor to your site has left the following information in your guestbook:\n
    	Name: $name
    	Email: $email
    	The visitor commented:
    	------------------------------
    	$message 
    	------------------------------
    	You can view the message at:
    	http://www.mysite.com";
    	mail($mailTo, $mailSubject, $mailBody, $mailFrom);
    }
    
    print "&result=okay";
    exit;
    
    ?>
    Thanks so much for persisting with me - I appreciate that it's probably infuriating and that I'm making rudimentry mistakes but I'm still learning

    Matt
    Stylefrog

    matt@stylefrogcreative.com
    http://www.stylefrog.net
    http://www.stylefrogcreative.com

  8. #8
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    this should be
    fseek($file, 21, 0);
    .... and should have been fseek($file, 0, 0) in my previous reply, too. The 0 signifies that the value is measured relative to the start of the file, other possibilities are relative to current position or relative to the end of file

    Musicman

  9. #9
    www.stylefrogcreative.com
    Join Date
    Apr 2001
    Posts
    100
    Hi Musicman,

    I've updated the fseek as directed but it still doesn't seem to be writing the full version of previous messages after a new submission.

    To see what I'm banging on about go to the 'Comments' page at http://www.stylefrogcreative.com/theinvention/ and submit a few new entries.

    Thanks again!!!
    Stylefrog

    matt@stylefrogcreative.com
    http://www.stylefrog.net
    http://www.stylefrogcreative.com

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center