Question about $_GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hora
Forum Newbie
Posts: 9
Joined: Mon May 05, 2008 10:17 pm

Question about $_GET

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about $_GET

Post 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.
hora
Forum Newbie
Posts: 9
Joined: Mon May 05, 2008 10:17 pm

Re: Question about $_GET

Post 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).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about $_GET

Post 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.
hora
Forum Newbie
Posts: 9
Joined: Mon May 05, 2008 10:17 pm

Re: Question about $_GET

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about $_GET

Post 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.
hora
Forum Newbie
Posts: 9
Joined: Mon May 05, 2008 10:17 pm

Re: Question about $_GET

Post 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!
Post Reply