Better PHP/HTML Layout to Handle Refreshes

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
JonOfAllTrades
Forum Newbie
Posts: 6
Joined: Mon Nov 07, 2005 3:47 pm
Location: Clarksville, TN

Better PHP/HTML Layout to Handle Refreshes

Post by JonOfAllTrades »

Hello and good morning, peeps.
I'm new to PHP, and I've been cheerfully whipping out login and admin pages, so far so good. When a user submits an action with a link or button, the page is reloaded with a $_GET['Action'] value. The start of each page checks for this and handles it. This lets me keep all my display code and my action code in the same files. HOWEVER, a user that hits Refresh can duplicate an action. In practice, most actions are safely repeatable (there's a technical term for that), because submitting the same data twice will bump into a unique key in MySQL and get ignored, but still. It's inelegant and potentially unsafe.
Would anyone like to show this newbie how it's done? I don't want to get too complicated. Would it make sense to put action code in a separate file, and then have it include the main page code?
Thank you!
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Hmm.
Once the form has been actioned, have

Code: Select all

header ("thispage.php");
exit;
bounce them back to the same page again, but without any values (except maybe one to say 'yay, your login is accepted, or something).

This is the wrong forum for this sort of stuff, by the way.
JonOfAllTrades
Forum Newbie
Posts: 6
Joined: Mon Nov 07, 2005 3:47 pm
Location: Clarksville, TN

Post by JonOfAllTrades »

Grim... wrote:Hmm.
Once the form has been actioned, have

Code: Select all

header ("thispage.php");
exit;
bounce them back to the same page again, but without any values (except maybe one to say 'yay, your login is accepted, or something).

This is the wrong forum for this sort of stuff, by the way.
Sorry. Interface design forum? Can I move it?
That won't work in my case; I should explain further.
The only page anyone accesses on my server is Blueprint.php, which lays out a title, links, a table layout, handles some javascript, etc. It include()'s the actual document requested, as specified in a GET and verified from a list of valid Docs. By the time an individual document is opened, quite a bit of HTML has already been posted.

On a slightly separate note, is there a simple way to break out of a file? Whenever a user requests a secure page, the first thing it does is check for a valid session, and if not, it include()'s the appropriate login page instead. As a result, the whole file is in an else bracket.

UserAdminHome.php

Code: Select all

if (array_key_exists('User' , $_SESSION) == false)  // I know, this needs to be expanded
	{
	echo("<p class='Warning'>You are not logged in.</p>\n");
	include("StaffLogin.php");
	}
else
	{
	...entire document lies in a bracket
	}
Of course, it gets even worse when there are several things to check before continuing into the meat of the page.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You can use header() to jump back into the same page, but with different variables.
So it will work for the first problem.
As for the break thing, I think you're looking for exit()
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Grim... wrote: As for the break thing, I think you're looking for exit()
Alternatively, you can use die() if you want to exit with a message.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Exit can exit with a message, too.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Grim... wrote:Exit can exit with a message, too.
It can? Never tried it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

exit() is die(), die() is exit().
JonOfAllTrades
Forum Newbie
Posts: 6
Joined: Mon Nov 07, 2005 3:47 pm
Location: Clarksville, TN

Post by JonOfAllTrades »

Grim... wrote:You can use header() to jump back into the same page, but with different variables.
So it will work for the first problem.
As for the break thing, I think you're looking for exit()
Hmm. I thought you could not use header() once you'd outputted any part of the document?
Exit() drops out of PHP, cutting the document off. That won't work with the architecture I have:

GET: Blueprint.php?Doc=StaffLogin

Blueprint.php:

Code: Select all

<?php session_start(); ?>
...header stuff, confirms $_GET['Doc'] is valid, output title
...javascript functions
<table ...>
	...various layout elements
	...title, random subtitle, etc
	include($ContentFiles[$Document]);
	...random picture, links from separate txt file, boilerplate, etc.
</table>
If I exit() while in StaffLogin.php, which is being include()d in the middle of Blueprint.php, the document will end there, and part of the layout will be cut off.
The layout is reasonably liquid (you can see it at 209.159.34.53 if you like, comments welcome), but I need the table layout to make it work.
I suppose I could break the Blueprint.php template into two parts, and have every document call include("Blueprint1.php") and include("Blueprint2.php") with the page-specific code in the middle, but I would still need to enclose the whole meat of the file in a block:

UserAdminHome.php:

Code: Select all

include("Blueprint1.php");

if(/* logged in */)
	{
	// Display fun stuff
	}
else
	{
	include("StaffLogin.php");  // Oops, this file will include() Blueprint1 and 2 again; maybe include_once()?
	}

include("Blueprint2.php");
So there's no command that will drop out of the current file but resume PHP parsing? Maybe some way to trick the parser into thinking it's hit an EOF in the middle of the file? I guess I'm asking for something that PHP isn't really meant to do.
I appreciate the suggestions! Any ideas on how to rearrange things without having to have my layout code in every file?
JonOfAllTrades
Forum Newbie
Posts: 6
Joined: Mon Nov 07, 2005 3:47 pm
Location: Clarksville, TN

Post by JonOfAllTrades »

Aha! Thank you, Feyd. Your post made me curious to see if there was ANY difference between exit() and die(). There's not, but there's a note in die() that *return* will exit the current file. Perfect! Guess I shoulda RTFM... :oops:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

it's actually header('Location:http://full.url.here');

you should put the full url for compatibility reasons. older browsers could choke on a relative url

edit: heh this isnt the first time people've got confused by alias functions, there's quite a few of them and I've seen people arguing over which one to use (count() and sizeof())
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

jshpro2 wrote:it's actually header('Location:http://full.url.here');
So it is.
Post Reply