Page 1 of 1

Question about $_GET

Posted: Mon May 05, 2008 10:33 pm
by hora
Hi everyone,

I'm new here, I hope I'm posting this in the right place.

I'm making a Facebook application in PHP, and since I've never really coded much in PHP, I'm just building my experience now, I've run into a bit of a problem.

For a Facebook app, when someone just adds it you can link the user to a specific url, mine would be apps.facebook.com/(MYAPP)/index.php?status=new

My index.php file checks to see if status is set, and if it equals "new", then it displays a slightly different page. More importantly, it initializes some stuff for the user in my MySQL database. Basically it adds the user to the database and sets a bunch of other fields equal to 0. Now my problem is that the rest of my app works using $_POST, so if the user does some stuff, and the index.php page reloads with a bunch of POST information, the URL is still index.php?status=new and it tries to add the user to the database again. This time it throws an error, cause the entry already exists...

My question then, is this: once the user has been added to the database, so basically everything in ?status=new, how can I make it that if the page reloads again that the URL won't be ?status=new anymore? I'm also worried about what would happen if the user clicked back on their browser, because that would still be there...

Are there any ways of doing this without making it so that when a user adds the application it goes to a completely different URL that doesn't use $_POST?

Thanks!

Re: Question about $_GET

Posted: Mon May 05, 2008 11:28 pm
by John Cartwright
Issue a header() redirect after you have inserted the row in the database. As a failsafe, you should check if the user exists already in the database before inserting, otherwise you can let the query die silently.

Re: Question about $_GET

Posted: Tue May 06, 2008 5:45 pm
by hora
Thanks, what I've done is just check to see if the user exists. Still being new to PHP, I'm not really sure how header works, and it seemed to me like it would have to refresh the page (something I don't want).

Re: Question about $_GET

Posted: Tue May 06, 2008 6:38 pm
by John Cartwright
Why wouldn't you want to redirect? It will clear the last request and avoid multiple submissions. This is commonly done after an insertion to a database for this very same reason.

Re: Question about $_GET

Posted: Tue May 06, 2008 6:42 pm
by hora
Jcart wrote:Why wouldn't you want to redirect? It will clear the last request and avoid multiple submissions. This is commonly done after an insertion to a database for this very same reason.
Wouldn't a redirect be annoying for the user? I'm a big noob when it comes to PHP, so I'm assuming a redirect would reload the page, wouldn't it?

Re: Question about $_GET

Posted: Tue May 06, 2008 6:55 pm
by John Cartwright
The redirect is issued before any content is outputted, and is transparent. Hense, a header redirect. I believe what you are imagining is a meta redirect.

Re: Question about $_GET

Posted: Tue May 06, 2008 7:32 pm
by hora
Jcart wrote:The redirect is issued before any content is outputted, and is transparent. Hense, a header redirect. I believe what you are imagining is a meta redirect.
Yeah, I implemented that and it works like it should, thanks for the help!