Page 1 of 1
Better PHP/HTML Layout to Handle Refreshes
Posted: Tue Nov 08, 2005 11:40 am
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!
Posted: Tue Nov 08, 2005 11:54 am
by Grim...
Hmm.
Once the form has been actioned, have
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.
Posted: Tue Nov 08, 2005 12:28 pm
by JonOfAllTrades
Grim... wrote:Hmm.
Once the form has been actioned, have
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.
Posted: Wed Nov 09, 2005 3:28 am
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()
Posted: Wed Nov 09, 2005 6:32 am
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.
Posted: Wed Nov 09, 2005 7:08 am
by Grim...
Exit can exit with a message, too.
Posted: Wed Nov 09, 2005 7:25 am
by foobar
Grim... wrote:Exit can exit with a message, too.
It can? Never tried it.
Posted: Wed Nov 09, 2005 7:37 am
by feyd
exit() is die(), die() is exit().
Posted: Wed Nov 09, 2005 7:58 am
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?
Posted: Wed Nov 09, 2005 8:03 am
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...

Posted: Wed Nov 09, 2005 9:58 am
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())
Posted: Wed Nov 09, 2005 10:12 am
by Grim...