Forum Moderators: coopster

Message Too Old, No Replies

Save Form Data (emails) to CSV

         

mirozake

5:01 am on May 5, 2008 (gmt 0)

10+ Year Member



Hi everyone,

I'm totally new to PHP and already stuck. I'm trying to simply have a form where someone can enter their email. Then I would like to save all emails entered into a CSV file. I thought I would find hundreds of examples of doing this but maybe I'm just not searching for the right thing. Can someone either help or point me in the right direction please. One additional function I would like to have is to check to make sure a certain IP can submit only once, which I gather it's possible but how. And any other simple methods to make sure spammers don't abuse it.

Thank you so much.

Miro

Otaku

10:51 am on May 5, 2008 (gmt 0)

10+ Year Member



Take a look at PHP's fwrite [php.net] function. and related stuff (follow links if needed). What you need is to APPEND data to files.

If you wanna check IPs, you need to append to two different files then - emails to one, and senders' IP addresses to another. Then you'll know who has already submitted stuff.

Then add a checking routine before writing to files: read the IPs file ( file_get_contents [php.net] may be a good thing for that: ) and look for the current IP ( for example, with strpos [php.net] ). Proceed only if you don't find it.

Hope you'll find your way :) PHP is a good thing, you'll like it :)

[edited by: Otaku at 10:56 am (utc) on May 5, 2008]

mirozake

3:15 pm on May 5, 2008 (gmt 0)

10+ Year Member



Thanks Otaku. That's really helpful. I'll start checking these out. To speed things for the time being where's a good place to see examples of something like this. It seems like an email collecting form must be one of the most common use and there must be a ton of examples somewhere, only if i could find it. :)

jatar_k

6:42 pm on May 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the steps are pretty simple

get data from form
test/clean data

fopen file for append
fwrite data to file
fclose file

show them some kind of thank you page

mirozake

8:55 pm on May 5, 2008 (gmt 0)

10+ Year Member



I see the simplicity there. THanks.

One thing I wonder. What happens when more than one person is handling the form? It doesn't cause conflict? I thought maybe it would since fopen, fclose happening at the same time for different things to write. (sorry i'm not asking this question more pro but i hope you get what i'm saying) :)

'Had

d40sithui

9:27 pm on May 5, 2008 (gmt 0)

10+ Year Member



well jatar is all about simplicity. also hes some sort of php god around here lol.
thats a good question you asked. i wonder that myself. i would think that php would have some kind of locking mechanism so that the same file will not be able to write to at the same time.
i think however, that using a database to store this information would be more practical. you can always pull it out to a csv file and u wont have to worry about this kind of issue =p. plus all the cool kids are doing it!

jatar_k

12:05 am on May 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



contemplate this

via the web you can't actually have the same script executed/called at the exact same time, so to have a collision would be difficult

open and close would be per execution, wouldn't cause someone else's pointer to be destroyed

mirozake

3:06 am on May 7, 2008 (gmt 0)

10+ Year Member



That's interesting. Good to know. Raises a question for me though. Why is it that in most CMS there's a check out / check in procedure written? Second person shouldn't be able to write to a file that's in use anyways, right? I'm just exploring further.. Thanks for all the comments.. And, btw, I got the original problem worked out. Now, I gotta fight spammers. :)

Otaku

9:46 am on May 7, 2008 (gmt 0)

10+ Year Member



At the PHP Manual page for the fwrite function that I gave the link to above there are comments about that issue with simultaneous writing, and also there are tons of user comments. READ THE MANUAL!